Showing posts with label date. Show all posts
Showing posts with label date. Show all posts

Thursday, March 22, 2012

A CLR SVF works on my DB instance but not on the other one

Hello,

I have scalar valued function that simply convert a date from UTC to LET by using the .NET functions..

The code is very simple:

PartialPublicClass UserFunction

<Microsoft.SqlServer.Server.SqlFunction(isDeterministic:=True, Name:="ConvertLETDatetoUTC")> _

PublicSharedFunction ConvertLETDatetoUTC(ByVal DateLETFormat As SqlDateTime) As DateTime

' Add your code here

Dim DateLETFormatToConvert AsDate

DateLETFormatToConvert = DateLETFormat.Value

Return DateLETFormatToConvert.ToUniversalTime

EndFunction
End Class

select ConvertLETDatetoUTC('2007-06-25 10:00:00')

Now the problem is that the function works properly on my sql server instanc but in the moment I deploy the same code on the sql server instance on a remote machine the conversion doesn't work that means that date I pass is not converted.

On the SQl server machine there the .net framework 1.1 and 2.0 ...

I really don't know what to check to solve the problem .. some idea?

Thank you

Can you debug the procedure ? I would use something like the Pipe.Send command to send some debbuging information if you are not able to debug the procedure right on the server. Its hard to tell where the problem is based on, so debugging using SQLPipe should be a good alternative for you.

Jens K. Suessmeyer.

http://www.sqlserver2005.de
|||As Jens says, you probably should debuf the function - however I don't believe you can use SqlPipe in the function as it is a UDF. But, are you getting any errors, or is it just the wrong data coming back? If it is wrong data, what collation and default regional settings does the remote server have?

Niels
|||

Hi,

thank you for the reply...

I was able from my development environment to debug and the statement:

DateLETFormatToConvert.ToUniversalTime

doesn't change a comma to my date!!

It is really weird but I don't know which settings (windows or sql server) I should check..

Thankx

Marina

|||Check the Windows settings.

Niels
|||

Hello,

I checked the window settings and I found out that the PC where the SQL server instance that gives me problem, use as Date Time zone: (GMT) Casablanca,Monrovia

while my Date time zome is Amsterdam, Rome...

The DBA told met that all the server in test and production has this setting and it cannot be changed.

Could be this the reason of the difference?

If yes hor an I make my .net code run as it should?

Thank you

Marina B.

|||No, I don't think that's the issue. Do you know what the regional setting for dates etc is, not the actual timezone?

Niels
|||

On both server the Regional and Language Options use the English(United Kingdom) formats and the

sQL collation is:

SQL_Latin1_General_CP1_CI_AS

something to do maybe with the .Net framework installed?

ThankX

Marina B.

A CLR SVF works on my DB instance but not on the other one

Hello,

I have scalar valued function that simply convert a date from UTC to LET by using the .NET functions..

The code is very simple:

Partial Public Class UserFunction

<Microsoft.SqlServer.Server.SqlFunction(isDeterministic:=True, Name:="ConvertLETDatetoUTC")> _

Public Shared Function ConvertLETDatetoUTC(ByVal DateLETFormat As SqlDateTime) As DateTime

' Add your code here

Dim DateLETFormatToConvert As Date

DateLETFormatToConvert = DateLETFormat.Value

Return DateLETFormatToConvert.ToUniversalTime

End Function
End Class

select ConvertLETDatetoUTC('2007-06-25 10:00:00')

Now the problem is that the function works properly on my sql server instanc but in the moment I deploy the same code on the sql server instance on a remote machine the conversion doesn't work that means that date I pass is not converted.

On the SQl server machine there the .net framework 1.1 and 2.0 ...

I really don't know what to check to solve the problem .. some idea?

Thank you

Can you debug the procedure ? I would use something like the Pipe.Send command to send some debbuging information if you are not able to debug the procedure right on the server. Its hard to tell where the problem is based on, so debugging using SQLPipe should be a good alternative for you.

Jens K. Suessmeyer.

http://www.sqlserver2005.de
|||As Jens says, you probably should debuf the function - however I don't believe you can use SqlPipe in the function as it is a UDF. But, are you getting any errors, or is it just the wrong data coming back? If it is wrong data, what collation and default regional settings does the remote server have?

Niels
|||

Hi,

thank you for the reply...

I was able from my development environment to debug and the statement:

DateLETFormatToConvert.ToUniversalTime

doesn't change a comma to my date!!

It is really weird but I don't know which settings (windows or sql server) I should check..

Thankx

Marina

|||Check the Windows settings.

Niels
|||

Hello,

I checked the window settings and I found out that the PC where the SQL server instance that gives me problem, use as Date Time zone: (GMT) Casablanca,Monrovia

while my Date time zome is Amsterdam, Rome...

The DBA told met that all the server in test and production has this setting and it cannot be changed.

Could be this the reason of the difference?

If yes hor an I make my .net code run as it should?

Thank you

Marina B.

|||No, I don't think that's the issue. Do you know what the regional setting for dates etc is, not the actual timezone?

Niels
|||

On both server the Regional and Language Options use the English(United Kingdom) formats and the

sQL collation is:

SQL_Latin1_General_CP1_CI_AS

something to do maybe with the .Net framework installed?

ThankX

Marina B.

Tuesday, March 20, 2012

A challenging CUBE related question

Hi,

I have data coming in from our OLTP's view which has the following sales related information:

1) Date of Sale
2) Product Sold
3) Customer to which the Product was sold
4) QTy. sold
5) Total Sale Amount

I am bringing in this information into my "staging area" and from there I am building my Customer, Product, and Time Dimension and obviously my FACT table. The measures in my fact table were TotalSalesand Total QtySold and everything was working fine and I was able to analyze, slice, dice my cube in many ways...

Now the problem:

I also have planning related data available from another OLTP based view which contains the following information:

1) Plan Year
2) Plan Month (using Plan Year and Plan month,I "derived" a date which for any month was the 1st of that month)
3) Planned Product
4) Planned Sale (Amount)
5) Planned Qty (for sale)

You would have noticed that planning is NOT done at "Customer Level" and the planning data is only for product and time level (and that too only upto month level).

After populating my FAct table from the Sales data, I appended the planning related data into my fact table hoping that as long as I do not select any thing in the Customer dimesion and only go as low as the month level on my Time dimension, I will be able to see all of the following (measures) for any/all products:

Planned Sale, Actual Sale, Planned Qty., Actual Qty,

However I am unable to get anything displayed in the Planned Sale and Planned Qty despite applying all of the little knowledge that Ihave so far in the world of BI and making data marts
...

I really hope that my questions and requirement is clear.Can someone please help me get to

the solution.

Many many TIA.I hope I understand you correctly. If I did, I would have thought of doing the following (thinking from OLAP standpoint):

Dimensions:
- Create shared dimension for Time to support Date of Sale and Plan Month, Year
- Create shared dimension for Product

Cubes/facts:
- Create two separate cubes one for Actual and one for Planned using the shared dimensions. The Actual can have the Customer as a Private Dimension (it may help to design the customer also as a shared dimension).
- Create a virtual cube on top of the Actual and Planned.
- Create calculated measures for differentials between Actual and Planned.

Murthy

Monday, March 19, 2012

A basic question: Removing duplicate results from Max function

Hi,

Say I have a table Job with columns name, date, salary . I want to get
the name ,date and salary for the date when that person earned maximum
salary. I am using something like

SELECT X.name,X.date,X.salary
FROM job X
WHERE X.salary IN
(SELECT MAX(Y.salary) FROM job Y where Y.name= X.name);

The problem is ; if a person earns maximum salary on two dates, both of
the dates are printed. I just want to get any one of those two rows.
I tried

SELECT X.name,Min(X.date),X.salary
FROM job X
WHERE X.salary IN
(SELECT MAX(Y.salary) FROM job Y where Y.name= X.name);

but it gives error.
Can anybody please suggest a solution?

Regards,
Aamir(aamircheema@.gmail.com) writes:
> Say I have a table Job with columns name, date, salary . I want to get
> the name ,date and salary for the date when that person earned maximum
> salary. I am using something like
>
> SELECT X.name,X.date,X.salary
> FROM job X
> WHERE X.salary IN
> (SELECT MAX(Y.salary) FROM job Y where Y.name= X.name);
>
> The problem is ; if a person earns maximum salary on two dates, both of
> the dates are printed. I just want to get any one of those two rows.
> I tried
> SELECT X.name,Min(X.date),X.salary
> FROM job X
> WHERE X.salary IN
> (SELECT MAX(Y.salary) FROM job Y where Y.name= X.name);
> but it gives error.

SELECT a.name, a.date, a.salary
FROM tbl a
JOIN (SELECT b.name, date = MAX(b.date)
FROM tbl b
JOIN (SELECT name, salary = MAX(salary)
FROM tbl
GROUP BY name) c ON c.name = b.name
AND c.salary = b.salary
GROUP BY a1.name) b ON a.name = b.name
AND a.date = b.date

This presumes that (name, date) is unique, and a person does not have
two salaries the same day.

The inner selects are derived tables - sort of virtual temp tables within
the query. A very powerful tool to write complex queries. A derived table
is independent of the outer query, and this why the alias b can be reused.
Note that they are not necessarily computed in whole - the optimizer often
recast computation order for a very very effceient query plan.

--
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pr...oads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodin...ions/books.mspx|||aamircheema@.gmail.com wrote:
> Hi,
> Say I have a table Job with columns name, date, salary . I want to get
> the name ,date and salary for the date when that person earned maximum
> salary. I am using something like
>
> SELECT X.name,X.date,X.salary
> FROM job X
> WHERE X.salary IN
> (SELECT MAX(Y.salary) FROM job Y where Y.name= X.name);
>
> The problem is ; if a person earns maximum salary on two dates, both of
> the dates are printed. I just want to get any one of those two rows.
> I tried
> SELECT X.name,Min(X.date),X.salary
> FROM job X
> WHERE X.salary IN
> (SELECT MAX(Y.salary) FROM job Y where Y.name= X.name);
> but it gives error.
> Can anybody please suggest a solution?
> Regards,
> Aamir

In SQL Server 2005:

WITH j AS
(SELECT name, date, salary,
RANK() OVER (PARTITION BY name ORDER BY salary DESC, date DESC) rnk
FROM job)
SELECT name, date, salary
FROM j
WHERE rnk = 1 ;

That assumes the combination of (name,salary,date) is unique. If it
isn't then just add other columns to the ORDER BY specification to make
a key.

--
David Portas, SQL Server MVP

Whenever possible please post enough code to reproduce your problem.
Including CREATE TABLE and INSERT statements usually helps.
State what version of SQL Server you are using and specify the content
of any error messages.

SQL Server Books Online:
http://msdn2.microsoft.com/library/...US,SQL.90).aspx
--|||On 10 Jun 2006 07:48:38 -0700, aamircheema@.gmail.com wrote:

(snip)
>I tried
>SELECT X.name,Min(X.date),X.salary
>FROM job X
>WHERE X.salary IN
> (SELECT MAX(Y.salary) FROM job Y where Y.name= X.name);
>but it gives error.
>Can anybody please suggest a solution?

Hi Aamir,

Here's a third suggestion:

SELECT X.name,Min(X.date),X.salary
FROM job X
WHERE X.salary IN
(SELECT MAX(Y.salary) FROM job Y where Y.name= X.name)
GROUP BY X.name, X.salary;

(Untested - see www.aspfaq.com/5006 if you prefer a tested reply)

--
Hugo Kornelis, SQL Server MVP|||
Thanks Everybody. That helped a lot

Sunday, February 19, 2012

6.5 server dates not showing dates with ss:fff

When i view a date on sql 6.5 it is showing as '22 Nov 2004 14:50', if
however i convert it to char 109 it gives me the full date ie 'Nov 22 2004
2:50:56:477PM'. Is there a setting on either the pc or sql that is causing
this ?
Thanks in advance.What application are you using to view the date? This is a pure client appli
cation issue, so this is
where you should look for such a setting. More info on datetime:
http://www.karaszi.com/SQLServer/info_datetime.asp
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
http://www.sqlug.se/
"billu" <billu@.discussions.microsoft.com> wrote in message
news:AC7E65A4-3F5C-4BF8-92BB-9EC9F8D17AE6@.microsoft.com...
> When i view a date on sql 6.5 it is showing as '22 Nov 2004 14:50', if
> however i convert it to char 109 it gives me the full date ie 'Nov 22 2004
> 2:50:56:477PM'. Is there a setting on either the pc or sql that is causing
> this ?
> Thanks in advance.|||i am using isql to view results. For some reason it only returns back a smal
l
part of the time field, it is only when i convert it that i get the full dat
e.
I have looked at the site but am still none the wiser ?
"Tibor Karaszi" wrote:

> What application are you using to view the date? This is a pure client app
lication issue, so this is
> where you should look for such a setting. More info on datetime:
> http://www.karaszi.com/SQLServer/info_datetime.asp
> --
> Tibor Karaszi, SQL Server MVP
> http://www.karaszi.com/sqlserver/default.asp
> http://www.solidqualitylearning.com/
> http://www.sqlug.se/
>
> "billu" <billu@.discussions.microsoft.com> wrote in message
> news:AC7E65A4-3F5C-4BF8-92BB-9EC9F8D17AE6@.microsoft.com...
>
>|||OK, so the programmers who wrote ISQL decided to display the datetime in tha
t particular way. I
suggest that you use CONVERT to get the desired display format for your date
time values.
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
http://www.sqlug.se/
"billu" <billu@.discussions.microsoft.com> wrote in message
news:BA62CD8F-2512-427F-B078-ABB245743D4B@.microsoft.com...[vbcol=seagreen]
>i am using isql to view results. For some reason it only returns back a sma
ll
> part of the time field, it is only when i convert it that i get the full d
ate.
> I have looked at the site but am still none the wiser ?
> "Tibor Karaszi" wrote:
>|||Take a look at your workstation's Regional Settings from the Control Panel.
Also, take a look at the ISQL(/w) settings to see if you've got it set up to
default to your workstation's Regional Settings.
Sincerely,
Anthony Thomas
"Tibor Karaszi" <tibor_please.no.email_karaszi@.hotmail.nomail.com> wrote in
message news:uy$0Q317EHA.3828@.TK2MSFTNGP09.phx.gbl...
OK, so the programmers who wrote ISQL decided to display the datetime in
that particular way. I
suggest that you use CONVERT to get the desired display format for your
datetime values.
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
http://www.sqlug.se/
"billu" <billu@.discussions.microsoft.com> wrote in message
news:BA62CD8F-2512-427F-B078-ABB245743D4B@.microsoft.com...
>i am using isql to view results. For some reason it only returns back a
small
> part of the time field, it is only when i convert it that i get the full
date.[vbcol=seagreen]
> I have looked at the site but am still none the wiser ?
> "Tibor Karaszi" wrote:
>
application issue, so this[vbcol=seagreen]
2004[vbcol=seagreen]
causing[vbcol=seagreen]

6.5 server dates not showing dates with ss:fff

When i view a date on sql 6.5 it is showing as '22 Nov 2004 14:50', if
however i convert it to char 109 it gives me the full date ie 'Nov 22 2004
2:50:56:477PM'. Is there a setting on either the pc or sql that is causing
this ?
Thanks in advance.
What application are you using to view the date? This is a pure client application issue, so this is
where you should look for such a setting. More info on datetime:
http://www.karaszi.com/SQLServer/info_datetime.asp
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
http://www.sqlug.se/
"billu" <billu@.discussions.microsoft.com> wrote in message
news:AC7E65A4-3F5C-4BF8-92BB-9EC9F8D17AE6@.microsoft.com...
> When i view a date on sql 6.5 it is showing as '22 Nov 2004 14:50', if
> however i convert it to char 109 it gives me the full date ie 'Nov 22 2004
> 2:50:56:477PM'. Is there a setting on either the pc or sql that is causing
> this ?
> Thanks in advance.
|||i am using isql to view results. For some reason it only returns back a small
part of the time field, it is only when i convert it that i get the full date.
I have looked at the site but am still none the wiser ?
"Tibor Karaszi" wrote:

> What application are you using to view the date? This is a pure client application issue, so this is
> where you should look for such a setting. More info on datetime:
> http://www.karaszi.com/SQLServer/info_datetime.asp
> --
> Tibor Karaszi, SQL Server MVP
> http://www.karaszi.com/sqlserver/default.asp
> http://www.solidqualitylearning.com/
> http://www.sqlug.se/
>
> "billu" <billu@.discussions.microsoft.com> wrote in message
> news:AC7E65A4-3F5C-4BF8-92BB-9EC9F8D17AE6@.microsoft.com...
>
>
|||OK, so the programmers who wrote ISQL decided to display the datetime in that particular way. I
suggest that you use CONVERT to get the desired display format for your datetime values.
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
http://www.sqlug.se/
"billu" <billu@.discussions.microsoft.com> wrote in message
news:BA62CD8F-2512-427F-B078-ABB245743D4B@.microsoft.com...[vbcol=seagreen]
>i am using isql to view results. For some reason it only returns back a small
> part of the time field, it is only when i convert it that i get the full date.
> I have looked at the site but am still none the wiser ?
> "Tibor Karaszi" wrote:
|||Take a look at your workstation's Regional Settings from the Control Panel.
Also, take a look at the ISQL(/w) settings to see if you've got it set up to
default to your workstation's Regional Settings.
Sincerely,
Anthony Thomas

"Tibor Karaszi" <tibor_please.no.email_karaszi@.hotmail.nomail.com> wrote in
message news:uy$0Q317EHA.3828@.TK2MSFTNGP09.phx.gbl...
OK, so the programmers who wrote ISQL decided to display the datetime in
that particular way. I
suggest that you use CONVERT to get the desired display format for your
datetime values.
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
http://www.sqlug.se/
"billu" <billu@.discussions.microsoft.com> wrote in message
news:BA62CD8F-2512-427F-B078-ABB245743D4B@.microsoft.com...
>i am using isql to view results. For some reason it only returns back a
small
> part of the time field, it is only when i convert it that i get the full
date.[vbcol=seagreen]
> I have looked at the site but am still none the wiser ?
> "Tibor Karaszi" wrote:
application issue, so this[vbcol=seagreen]
2004[vbcol=seagreen]
causing[vbcol=seagreen]

6.5 server dates not showing dates with ss:fff

When i view a date on sql 6.5 it is showing as '22 Nov 2004 14:50', if
however i convert it to char 109 it gives me the full date ie 'Nov 22 2004
2:50:56:477PM'. Is there a setting on either the pc or sql that is causing
this ?
Thanks in advance.What application are you using to view the date? This is a pure client application issue, so this is
where you should look for such a setting. More info on datetime:
http://www.karaszi.com/SQLServer/info_datetime.asp
--
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
http://www.sqlug.se/
"billu" <billu@.discussions.microsoft.com> wrote in message
news:AC7E65A4-3F5C-4BF8-92BB-9EC9F8D17AE6@.microsoft.com...
> When i view a date on sql 6.5 it is showing as '22 Nov 2004 14:50', if
> however i convert it to char 109 it gives me the full date ie 'Nov 22 2004
> 2:50:56:477PM'. Is there a setting on either the pc or sql that is causing
> this ?
> Thanks in advance.|||i am using isql to view results. For some reason it only returns back a small
part of the time field, it is only when i convert it that i get the full date.
I have looked at the site but am still none the wiser ?
"Tibor Karaszi" wrote:
> What application are you using to view the date? This is a pure client application issue, so this is
> where you should look for such a setting. More info on datetime:
> http://www.karaszi.com/SQLServer/info_datetime.asp
> --
> Tibor Karaszi, SQL Server MVP
> http://www.karaszi.com/sqlserver/default.asp
> http://www.solidqualitylearning.com/
> http://www.sqlug.se/
>
> "billu" <billu@.discussions.microsoft.com> wrote in message
> news:AC7E65A4-3F5C-4BF8-92BB-9EC9F8D17AE6@.microsoft.com...
> > When i view a date on sql 6.5 it is showing as '22 Nov 2004 14:50', if
> > however i convert it to char 109 it gives me the full date ie 'Nov 22 2004
> > 2:50:56:477PM'. Is there a setting on either the pc or sql that is causing
> > this ?
> > Thanks in advance.
>
>|||OK, so the programmers who wrote ISQL decided to display the datetime in that particular way. I
suggest that you use CONVERT to get the desired display format for your datetime values.
--
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
http://www.sqlug.se/
"billu" <billu@.discussions.microsoft.com> wrote in message
news:BA62CD8F-2512-427F-B078-ABB245743D4B@.microsoft.com...
>i am using isql to view results. For some reason it only returns back a small
> part of the time field, it is only when i convert it that i get the full date.
> I have looked at the site but am still none the wiser ?
> "Tibor Karaszi" wrote:
>> What application are you using to view the date? This is a pure client application issue, so this
>> is
>> where you should look for such a setting. More info on datetime:
>> http://www.karaszi.com/SQLServer/info_datetime.asp
>> --
>> Tibor Karaszi, SQL Server MVP
>> http://www.karaszi.com/sqlserver/default.asp
>> http://www.solidqualitylearning.com/
>> http://www.sqlug.se/
>>
>> "billu" <billu@.discussions.microsoft.com> wrote in message
>> news:AC7E65A4-3F5C-4BF8-92BB-9EC9F8D17AE6@.microsoft.com...
>> > When i view a date on sql 6.5 it is showing as '22 Nov 2004 14:50', if
>> > however i convert it to char 109 it gives me the full date ie 'Nov 22 2004
>> > 2:50:56:477PM'. Is there a setting on either the pc or sql that is causing
>> > this ?
>> > Thanks in advance.
>>|||Take a look at your workstation's Regional Settings from the Control Panel.
Also, take a look at the ISQL(/w) settings to see if you've got it set up to
default to your workstation's Regional Settings.
Sincerely,
Anthony Thomas
"Tibor Karaszi" <tibor_please.no.email_karaszi@.hotmail.nomail.com> wrote in
message news:uy$0Q317EHA.3828@.TK2MSFTNGP09.phx.gbl...
OK, so the programmers who wrote ISQL decided to display the datetime in
that particular way. I
suggest that you use CONVERT to get the desired display format for your
datetime values.
--
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
http://www.sqlug.se/
"billu" <billu@.discussions.microsoft.com> wrote in message
news:BA62CD8F-2512-427F-B078-ABB245743D4B@.microsoft.com...
>i am using isql to view results. For some reason it only returns back a
small
> part of the time field, it is only when i convert it that i get the full
date.
> I have looked at the site but am still none the wiser ?
> "Tibor Karaszi" wrote:
>> What application are you using to view the date? This is a pure client
application issue, so this
>> is
>> where you should look for such a setting. More info on datetime:
>> http://www.karaszi.com/SQLServer/info_datetime.asp
>> --
>> Tibor Karaszi, SQL Server MVP
>> http://www.karaszi.com/sqlserver/default.asp
>> http://www.solidqualitylearning.com/
>> http://www.sqlug.se/
>>
>> "billu" <billu@.discussions.microsoft.com> wrote in message
>> news:AC7E65A4-3F5C-4BF8-92BB-9EC9F8D17AE6@.microsoft.com...
>> > When i view a date on sql 6.5 it is showing as '22 Nov 2004 14:50', if
>> > however i convert it to char 109 it gives me the full date ie 'Nov 22
2004
>> > 2:50:56:477PM'. Is there a setting on either the pc or sql that is
causing
>> > this ?
>> > Thanks in advance.
>>

6 weekday time frame

I was wondering how I can do a date query. I have a table with order_quant and date. What I would like to do is get an output of the last 6 weeks on a specific day of the week. Last 6 orders for Tuesdays. Or, last 6 orders for Friday. I just can't get it narrowed down to just a weekday, just all order for 6 weeks. Any ideas?

WHERE datepart( dw, MyDateColumn ) = 3

is Tuesday, for default US settings where the first day of the week is Sunday. However, you can change that by using the DateFirst setting.

To verify your setting:


SELECT @.@.DATEFIRST

To change your setting: (In this situation, change to Monday as the first day of the week.)

SET DATEFIRST 1

|||

You could use DATEPART function for determine week day and DATEDIFF for difference

Code Snippet

createtable #orders

(

order_quant int,

date datetime

)

insertinto #orders values(10,'2007-06-05')

insertinto #orders values(20,'2007-05-30')

insertinto #orders values(30,'2007-05-23')

insertinto #orders values(40,'2007-05-22')

insertinto #orders values(50,'2007-06-03')

insertinto #orders values(60,'2007-04-10')

select*from #orders wheredatepart(weekday,date)=3 --Tuesdays

select*from #orders wheredatepart(weekday,date)=3 anddatediff(week,date,getdate())<=6 --Tuesdays, 6 weeks or later

|||That did it for me it looks like, thanks. I just need to remember to deal with week '0' but shouldn't be a problem for me.

Monday, February 13, 2012

4/7/2003 vs 04/07/2003

Hi,
I have a parameter query in SQL. The Query selects records in a date range based on the parameters.

my problem is that if I put the dates in the format of 4/7/2003 it won't work and I have to insert the date like 04/07/2003.

I don't know how to fix this. Its important since the parameters are coming froman Access ADP front end through the calendar object which saves the date in the 4/7/2003 format.

Thanksplease post an example or your query!|||Thanks for your time Paul.

Here is my query:
-----------------------
SELECT
dbo.tblComIssue.ComID AS ID,
CONVERT(Varchar(10), dbo.tblComIssue.ComDate, 101) AS Date, ISNULL(dbo.tblContact.FirstName, '') + ' '
+ ISNULL(dbo.tblContact.LastName, '') AS Caller

FROM dbo.tblComIssue LEFT OUTER JOIN
dbo.tblContact ON
dbo.tblComIssue.ContactID = dbo.tblContact.ContactID

WHERE (CONVERT(Varchar(10), dbo.tblComIssue.ComDate, 101) BETWEEN @.Date1 AND @.Date2)
ORDER BY dbo.tblComIssue.ComDate DESC

-----------
I also tried this instead of the "BETWEEN" function:

WHERE (CONVERT(Varchar(10), dbo.tblComIssue.ComDate, 101) >= @.Date1) AND (CONVERT(Varchar(10), dbo.tblComIssue.ComDate, 101) <= @.Date2)
ORDER BY dbo.tblComIssue.ComDate DESC
-----------

The wired part is that I have another query for "Date" not Date Range and it works fine:

SELECT dbo.tblComIssue.ComID, CONVERT(Varchar(10), dbo.tblComIssue.ComDate, 101) AS Date, ISNULL(dbo.tblContact.FirstName, '')
+ ' ' + ISNULL(dbo.tblContact.LastName, '') AS Caller
FROM dbo.tblComIssue LEFT OUTER JOIN
dbo.tblContact ON dbo.tblComIssue.ContactID = dbo.tblContact.ContactID
WHERE (CONVERT(Varchar(10), dbo.tblComIssue.ComDate, 101) = @.Date)
ORDER BY dbo.tblComIssue.ComDate DESC

------

Thanks|||It looks like your problem is comparing strings not dates. I would convert @.Date1 & @.Date2 to datetime datatype and compare dates.|||Thanks,
Yes that was the problem.
I used Enterprise manages to define the data types.

Thanks paul

Thursday, February 9, 2012

365 databases a year

Would it be possible to make a daily database version to avoid adding a data
date to every piece of data in the system. For example, if I have a
database named MyDatabase, at the end of processing for the day of 4/16/04,
I make a backup, and restore the database as MyDatabase-2004-16-04. This
process would continue every day we run processing. We would probably only
need real time access to 18 months of data, and would delete the oldest
ones. Our database is probably not going to get very large, one or two
dozen tables, a million or so rows at most.
What things do you think I would need to worry about in doing something like
this? The "old" databases would not need to be referenced all that often,
and could even be stored on a second server so they don't interfere with the
"current" database's performance. I am concerned that SQL Server might have
a problem with having 550 databases defined.
Thanks,
-PaulAll that just to avoid adding a DATETIME column? Boy you have more time on
your hands than anyone I know<g>.
--
Andrew J. Kelly SQL MVP
"Paul" <a@.b.com> wrote in message
news:ueJzsB$IEHA.1388@.TK2MSFTNGP09.phx.gbl...
> Would it be possible to make a daily database version to avoid adding a
data
> date to every piece of data in the system. For example, if I have a
> database named MyDatabase, at the end of processing for the day of
4/16/04,
> I make a backup, and restore the database as MyDatabase-2004-16-04. This
> process would continue every day we run processing. We would probably
only
> need real time access to 18 months of data, and would delete the oldest
> ones. Our database is probably not going to get very large, one or two
> dozen tables, a million or so rows at most.
> What things do you think I would need to worry about in doing something
like
> this? The "old" databases would not need to be referenced all that often,
> and could even be stored on a second server so they don't interfere with
the
> "current" database's performance. I am concerned that SQL Server might
have
> a problem with having 550 databases defined.
> Thanks,
> -Paul
>|||Why?
What's wrong with just storing a timestamp in a single database?
This scheme will give you an incredible maintenance and development
nightmare, especially should you choose to change a column definition (want
to do that across 500 databases? I don't)... Or if you ever wish to pull
historical aggregate data, please get a book because you'll need some
entertainment while waiting for your query to pull everything together from
500 tables across 500 databases. And I don't know who's going to write that
query :)
Anyway, the point is, unless you have a REALLY good reason, you should not
do this... And a million rows of historical data will not affect your
performance in the slightest bit if you tune your indexes properly.
"Paul" <a@.b.com> wrote in message
news:ueJzsB$IEHA.1388@.TK2MSFTNGP09.phx.gbl...
> Would it be possible to make a daily database version to avoid adding a
data
> date to every piece of data in the system. For example, if I have a
> database named MyDatabase, at the end of processing for the day of
4/16/04,
> I make a backup, and restore the database as MyDatabase-2004-16-04. This
> process would continue every day we run processing. We would probably
only
> need real time access to 18 months of data, and would delete the oldest
> ones. Our database is probably not going to get very large, one or two
> dozen tables, a million or so rows at most.
> What things do you think I would need to worry about in doing something
like
> this? The "old" databases would not need to be referenced all that often,
> and could even be stored on a second server so they don't interfere with
the
> "current" database's performance. I am concerned that SQL Server might
have
> a problem with having 550 databases defined.
> Thanks,
> -Paul
>|||Actually, time-wise this is a lay-up. Every night the backup/restore
process runs, and now I have a fixed view of the universe at that moment.
We can go back and see it without any fear of manipulation of the data. The
hard work I was trying to avoid was that to look at the data for a given, I
would have to do a Max(date) on all the data I was interested in prior to a
date, and then select based on that value (some data elements won't have
data for any given day). This would have made joins pretty ugly.
So in essence, yes, all that just to avoid adding a DATETIME column. The
application code is quite simple to write and maintain at that point. And
disk space is cheap now. Frankly, I think it is quite an elegant solution.
But the question still remains. What problems is SQL server going to give
me?
Thanks,
-Paul
"Andrew J. Kelly" <sqlmvpnoooospam@.shadhawk.com> wrote in message
news:%23OYNMY$IEHA.3216@.tk2msftngp13.phx.gbl...
> All that just to avoid adding a DATETIME column? Boy you have more time
on
> your hands than anyone I know<g>.
> --
> Andrew J. Kelly SQL MVP
>
> "Paul" <a@.b.com> wrote in message
> news:ueJzsB$IEHA.1388@.TK2MSFTNGP09.phx.gbl...
> > Would it be possible to make a daily database version to avoid adding a
> data
> > date to every piece of data in the system. For example, if I have a
> > database named MyDatabase, at the end of processing for the day of
> 4/16/04,
> > I make a backup, and restore the database as MyDatabase-2004-16-04.
This
> > process would continue every day we run processing. We would probably
> only
> > need real time access to 18 months of data, and would delete the oldest
> > ones. Our database is probably not going to get very large, one or two
> > dozen tables, a million or so rows at most.
> >
> > What things do you think I would need to worry about in doing something
> like
> > this? The "old" databases would not need to be referenced all that
often,
> > and could even be stored on a second server so they don't interfere with
> the
> > "current" database's performance. I am concerned that SQL Server might
> have
> > a problem with having 550 databases defined.
> >
> > Thanks,
> >
> > -Paul
> >
> >
>|||Will SQL Server handle it OK, probably. Will you regret it later, probably.
--
Andrew J. Kelly SQL MVP
"Paul" <a@.b.com> wrote in message
news:uTlfjk$IEHA.3576@.tk2msftngp13.phx.gbl...
> Actually, time-wise this is a lay-up. Every night the backup/restore
> process runs, and now I have a fixed view of the universe at that moment.
> We can go back and see it without any fear of manipulation of the data.
The
> hard work I was trying to avoid was that to look at the data for a given,
I
> would have to do a Max(date) on all the data I was interested in prior to
a
> date, and then select based on that value (some data elements won't have
> data for any given day). This would have made joins pretty ugly.
> So in essence, yes, all that just to avoid adding a DATETIME column. The
> application code is quite simple to write and maintain at that point. And
> disk space is cheap now. Frankly, I think it is quite an elegant
solution.
> But the question still remains. What problems is SQL server going to give
> me?
> Thanks,
> -Paul
>
> "Andrew J. Kelly" <sqlmvpnoooospam@.shadhawk.com> wrote in message
> news:%23OYNMY$IEHA.3216@.tk2msftngp13.phx.gbl...
> > All that just to avoid adding a DATETIME column? Boy you have more time
> on
> > your hands than anyone I know<g>.
> >
> > --
> > Andrew J. Kelly SQL MVP
> >
> >
> > "Paul" <a@.b.com> wrote in message
> > news:ueJzsB$IEHA.1388@.TK2MSFTNGP09.phx.gbl...
> > > Would it be possible to make a daily database version to avoid adding
a
> > data
> > > date to every piece of data in the system. For example, if I have a
> > > database named MyDatabase, at the end of processing for the day of
> > 4/16/04,
> > > I make a backup, and restore the database as MyDatabase-2004-16-04.
> This
> > > process would continue every day we run processing. We would probably
> > only
> > > need real time access to 18 months of data, and would delete the
oldest
> > > ones. Our database is probably not going to get very large, one or
two
> > > dozen tables, a million or so rows at most.
> > >
> > > What things do you think I would need to worry about in doing
something
> > like
> > > this? The "old" databases would not need to be referenced all that
> often,
> > > and could even be stored on a second server so they don't interfere
with
> > the
> > > "current" database's performance. I am concerned that SQL Server
might
> > have
> > > a problem with having 550 databases defined.
> > >
> > > Thanks,
> > >
> > > -Paul
> > >
> > >
> >
> >
>|||Okay, please be patient with me while I digest. I am an application
developer, not a DBA, and so I may not have all the right angles here (which
is why I am writing here). Can you clarify "storing a timestamp in a single
database"? I assume you mean storing a timestamp in every applicable table?
Just about every row of data in my database needs to be timestamped. So
when a datum is changed, it needs to be done with an insert of a new row
with the current date (intra-day changes overwrite). So, if I add an
employee record with the name column as "Paul" on 1/1/04 (id=1), and another
with a name of "Fred" on 2/1/04 (id=2), and two months later we want to
change Paul's name to "Pablo", I would insert a new row with the name column
as "Pablo" on 3/1/04 (id=1 still). Now, my boss asks me to get the names of
all the employees on 2/1/04. I SELECT id, Max(date) as max_date FROM
employees where date <= 2/1/04 and then re-select where the id and max_date
match.
This is not _terrible_ in the simple case, above, but now my boss wants me
to join on the positions table, and perhaps a few other tables that are time
stamped as well. It is starting to get ugly.
So, I figure, hey, I can do away with the timestamp altogether if I just
back up the database every time it changes. If I am careful about using
stored procedures to access data, and source control my application with
corresponding database changes, then all my selects and joins are just the
simple case, which is 99.9% of my needs (I will rarely need to go back to a
view of the database, but when I do, I want to do it quickly, adn not have
to have someone call me up to do a restore for a particular date on a
separate system). I was figuring that we were doing a backup of the
database everyday anyway, why not mount it as a new database with the name
as a date.
If, however, there is a simpler way to do this that I am not aware of,
please, feel free to let me know!
Thanks,
-Paul
"Adam Machanic" <amachanic@.air-worldwide.nospamallowed.com> wrote in message
news:egfxIk$IEHA.3308@.TK2MSFTNGP10.phx.gbl...
> Why?
> What's wrong with just storing a timestamp in a single database?
> This scheme will give you an incredible maintenance and development
> nightmare, especially should you choose to change a column definition
(want
> to do that across 500 databases? I don't)... Or if you ever wish to pull
> historical aggregate data, please get a book because you'll need some
> entertainment while waiting for your query to pull everything together
from
> 500 tables across 500 databases. And I don't know who's going to write
that
> query :)
> Anyway, the point is, unless you have a REALLY good reason, you should not
> do this... And a million rows of historical data will not affect your
> performance in the slightest bit if you tune your indexes properly.
>
> "Paul" <a@.b.com> wrote in message
> news:ueJzsB$IEHA.1388@.TK2MSFTNGP09.phx.gbl...
> > Would it be possible to make a daily database version to avoid adding a
> data
> > date to every piece of data in the system. For example, if I have a
> > database named MyDatabase, at the end of processing for the day of
> 4/16/04,
> > I make a backup, and restore the database as MyDatabase-2004-16-04.
This
> > process would continue every day we run processing. We would probably
> only
> > need real time access to 18 months of data, and would delete the oldest
> > ones. Our database is probably not going to get very large, one or two
> > dozen tables, a million or so rows at most.
> >
> > What things do you think I would need to worry about in doing something
> like
> > this? The "old" databases would not need to be referenced all that
often,
> > and could even be stored on a second server so they don't interfere with
> the
> > "current" database's performance. I am concerned that SQL Server might
> have
> > a problem with having 550 databases defined.
> >
> > Thanks,
> >
> > -Paul
> >
> >
>|||Okay, please be patient with me while I digest. I am an application
developer, not a DBA, and so I may not have all the right angles here (which
is why I am writing here).
Just about every row of data in my database needs to be timestamped. So
when a datum is changed, it needs to be done with an insert of a new row
with the current date (intra-day changes overwrite). So, if I add an
employee record with the name column as "Paul" on 1/1/04 (id=1), and another
with a name of "Fred" on 2/1/04 (id=2), and two months later we want to
change Paul's name to "Pablo", I would insert a new row with the name column
as "Pablo" on 3/1/04 (id=1 still). Now, my boss asks me to get the names of
all the employees on 2/1/04. I SELECT id, Max(date) as max_date FROM
employees where date <= 2/1/04 and then re-select where the id and max_date
match.
This is not _terrible_ in the simple case, above, but now my boss wants me
to join on the positions table, and perhaps a few other tables that are time
stamped as well. It is starting to get ugly.
So, I figure, hey, I can do away with the timestamp altogether if I just
back up the database every time it changes. If I am careful about using
stored procedures to access data, and source control my application with
corresponding database changes, then all my selects and joins are just the
simple case, which is 99.9% of my needs (I will rarely need to go back to a
view of the database, but when I do, I want to do it quickly, adn not have
to have someone call me up to do a restore for a particular date on a
separate system). I was figuring that we were doing a backup of the
database everyday anyway, why not mount it as a new database with the name
as a date.
If, however, there is a simpler way to do this that I am not aware of,
please, feel free to let me know!
Thanks,
-Paul
"Andrew J. Kelly" <sqlmvpnoooospam@.shadhawk.com> wrote in message
news:OlCq$OAJEHA.3216@.tk2msftngp13.phx.gbl...
> Will SQL Server handle it OK, probably. Will you regret it later,
probably.
> --
> Andrew J. Kelly SQL MVP
>
> "Paul" <a@.b.com> wrote in message
> news:uTlfjk$IEHA.3576@.tk2msftngp13.phx.gbl...
> > Actually, time-wise this is a lay-up. Every night the backup/restore
> > process runs, and now I have a fixed view of the universe at that
moment.
> > We can go back and see it without any fear of manipulation of the data.
> The
> > hard work I was trying to avoid was that to look at the data for a
given,
> I
> > would have to do a Max(date) on all the data I was interested in prior
to
> a
> > date, and then select based on that value (some data elements won't have
> > data for any given day). This would have made joins pretty ugly.
> >
> > So in essence, yes, all that just to avoid adding a DATETIME column.
The
> > application code is quite simple to write and maintain at that point.
And
> > disk space is cheap now. Frankly, I think it is quite an elegant
> solution.
> >
> > But the question still remains. What problems is SQL server going to
give
> > me?
> >
> > Thanks,
> >
> > -Paul
> >
> >
> > "Andrew J. Kelly" <sqlmvpnoooospam@.shadhawk.com> wrote in message
> > news:%23OYNMY$IEHA.3216@.tk2msftngp13.phx.gbl...
> > > All that just to avoid adding a DATETIME column? Boy you have more
time
> > on
> > > your hands than anyone I know<g>.
> > >
> > > --
> > > Andrew J. Kelly SQL MVP
> > >
> > >
> > > "Paul" <a@.b.com> wrote in message
> > > news:ueJzsB$IEHA.1388@.TK2MSFTNGP09.phx.gbl...
> > > > Would it be possible to make a daily database version to avoid
adding
> a
> > > data
> > > > date to every piece of data in the system. For example, if I have a
> > > > database named MyDatabase, at the end of processing for the day of
> > > 4/16/04,
> > > > I make a backup, and restore the database as MyDatabase-2004-16-04.
> > This
> > > > process would continue every day we run processing. We would
probably
> > > only
> > > > need real time access to 18 months of data, and would delete the
> oldest
> > > > ones. Our database is probably not going to get very large, one or
> two
> > > > dozen tables, a million or so rows at most.
> > > >
> > > > What things do you think I would need to worry about in doing
> something
> > > like
> > > > this? The "old" databases would not need to be referenced all that
> > often,
> > > > and could even be stored on a second server so they don't interfere
> with
> > > the
> > > > "current" database's performance. I am concerned that SQL Server
> might
> > > have
> > > > a problem with having 550 databases defined.
> > > >
> > > > Thanks,
> > > >
> > > > -Paul
> > > >
> > > >
> > >
> > >
> >
> >
>|||Paul,
It's pretty much imposable to design a database in a newsgroup post due
mainly to lack of all the details but it sounds like you need archive
tables. Essentially each table that needs this "versioning" will have to
have an almost identical table that will server as a history table. It will
have all the same columns but not the same constraints such as PK
constraint. Then you add triggers to each of the main tables that will
insert a copy of the current row before any change is made with the datetime
of the event. Then your app will address the main tables as normal and can
address the history tables when needed. You would have to filter the
queries on the datetime range that your after.
--
Andrew J. Kelly SQL MVP
"Paul" <a@.b.com> wrote in message
news:ORe90ECJEHA.232@.TK2MSFTNGP12.phx.gbl...
> Okay, please be patient with me while I digest. I am an application
> developer, not a DBA, and so I may not have all the right angles here
(which
> is why I am writing here).
> Just about every row of data in my database needs to be timestamped. So
> when a datum is changed, it needs to be done with an insert of a new row
> with the current date (intra-day changes overwrite). So, if I add an
> employee record with the name column as "Paul" on 1/1/04 (id=1), and
another
> with a name of "Fred" on 2/1/04 (id=2), and two months later we want to
> change Paul's name to "Pablo", I would insert a new row with the name
column
> as "Pablo" on 3/1/04 (id=1 still). Now, my boss asks me to get the names
of
> all the employees on 2/1/04. I SELECT id, Max(date) as max_date FROM
> employees where date <= 2/1/04 and then re-select where the id and
max_date
> match.
> This is not _terrible_ in the simple case, above, but now my boss wants me
> to join on the positions table, and perhaps a few other tables that are
time
> stamped as well. It is starting to get ugly.
> So, I figure, hey, I can do away with the timestamp altogether if I just
> back up the database every time it changes. If I am careful about using
> stored procedures to access data, and source control my application with
> corresponding database changes, then all my selects and joins are just the
> simple case, which is 99.9% of my needs (I will rarely need to go back to
a
> view of the database, but when I do, I want to do it quickly, adn not have
> to have someone call me up to do a restore for a particular date on a
> separate system). I was figuring that we were doing a backup of the
> database everyday anyway, why not mount it as a new database with the name
> as a date.
> If, however, there is a simpler way to do this that I am not aware of,
> please, feel free to let me know!
> Thanks,
> -Paul
>
> "Andrew J. Kelly" <sqlmvpnoooospam@.shadhawk.com> wrote in message
> news:OlCq$OAJEHA.3216@.tk2msftngp13.phx.gbl...
> > Will SQL Server handle it OK, probably. Will you regret it later,
> probably.
> >
> > --
> > Andrew J. Kelly SQL MVP
> >
> >
> > "Paul" <a@.b.com> wrote in message
> > news:uTlfjk$IEHA.3576@.tk2msftngp13.phx.gbl...
> > > Actually, time-wise this is a lay-up. Every night the backup/restore
> > > process runs, and now I have a fixed view of the universe at that
> moment.
> > > We can go back and see it without any fear of manipulation of the
data.
> > The
> > > hard work I was trying to avoid was that to look at the data for a
> given,
> > I
> > > would have to do a Max(date) on all the data I was interested in prior
> to
> > a
> > > date, and then select based on that value (some data elements won't
have
> > > data for any given day). This would have made joins pretty ugly.
> > >
> > > So in essence, yes, all that just to avoid adding a DATETIME column.
> The
> > > application code is quite simple to write and maintain at that point.
> And
> > > disk space is cheap now. Frankly, I think it is quite an elegant
> > solution.
> > >
> > > But the question still remains. What problems is SQL server going to
> give
> > > me?
> > >
> > > Thanks,
> > >
> > > -Paul
> > >
> > >
> > > "Andrew J. Kelly" <sqlmvpnoooospam@.shadhawk.com> wrote in message
> > > news:%23OYNMY$IEHA.3216@.tk2msftngp13.phx.gbl...
> > > > All that just to avoid adding a DATETIME column? Boy you have more
> time
> > > on
> > > > your hands than anyone I know<g>.
> > > >
> > > > --
> > > > Andrew J. Kelly SQL MVP
> > > >
> > > >
> > > > "Paul" <a@.b.com> wrote in message
> > > > news:ueJzsB$IEHA.1388@.TK2MSFTNGP09.phx.gbl...
> > > > > Would it be possible to make a daily database version to avoid
> adding
> > a
> > > > data
> > > > > date to every piece of data in the system. For example, if I have
a
> > > > > database named MyDatabase, at the end of processing for the day of
> > > > 4/16/04,
> > > > > I make a backup, and restore the database as
MyDatabase-2004-16-04.
> > > This
> > > > > process would continue every day we run processing. We would
> probably
> > > > only
> > > > > need real time access to 18 months of data, and would delete the
> > oldest
> > > > > ones. Our database is probably not going to get very large, one
or
> > two
> > > > > dozen tables, a million or so rows at most.
> > > > >
> > > > > What things do you think I would need to worry about in doing
> > something
> > > > like
> > > > > this? The "old" databases would not need to be referenced all
that
> > > often,
> > > > > and could even be stored on a second server so they don't
interfere
> > with
> > > > the
> > > > > "current" database's performance. I am concerned that SQL Server
> > might
> > > > have
> > > > > a problem with having 550 databases defined.
> > > > >
> > > > > Thanks,
> > > > >
> > > > > -Paul
> > > > >
> > > > >
> > > >
> > > >
> > >
> > >
> >
> >
>|||Paul,
I agree with Andrew... It sounds as if you need a real-time piece and a
historical reporting piece. It is generally good to separate the two. But
splitting the things up into multiple databases one per day will end up
biting you in the butt... Imagine your boss asking you to compare some
information on a monthly basis. You'd had to get information from many
databases to put in a single report... It would get problematic quickly...
Since separation is still a good idea, start with separating the current
data from the historical data... On the history tables put an effective date
column... When a new row is added to the employee table, add it to the
history table as well with a current date of today... You may even consider
putting ALL of the history into a separate database from the current info
....
Good luck...
--
Wayne Snyder, MCDBA, SQL Server MVP
Computer Education Services Corporation (CESC), Charlotte, NC
www.computeredservices.com
(Please respond only to the newsgroups.)
I support the Professional Association of SQL Server (PASS) and it's
community of SQL Server professionals.
www.sqlpass.org
"Paul" <a@.b.com> wrote in message
news:ua0%23uyAJEHA.3704@.TK2MSFTNGP11.phx.gbl...
> Okay, please be patient with me while I digest. I am an application
> developer, not a DBA, and so I may not have all the right angles here
(which
> is why I am writing here). Can you clarify "storing a timestamp in a
single
> database"? I assume you mean storing a timestamp in every applicable
table?
> Just about every row of data in my database needs to be timestamped. So
> when a datum is changed, it needs to be done with an insert of a new row
> with the current date (intra-day changes overwrite). So, if I add an
> employee record with the name column as "Paul" on 1/1/04 (id=1), and
another
> with a name of "Fred" on 2/1/04 (id=2), and two months later we want to
> change Paul's name to "Pablo", I would insert a new row with the name
column
> as "Pablo" on 3/1/04 (id=1 still). Now, my boss asks me to get the names
of
> all the employees on 2/1/04. I SELECT id, Max(date) as max_date FROM
> employees where date <= 2/1/04 and then re-select where the id and
max_date
> match.
> This is not _terrible_ in the simple case, above, but now my boss wants me
> to join on the positions table, and perhaps a few other tables that are
time
> stamped as well. It is starting to get ugly.
> So, I figure, hey, I can do away with the timestamp altogether if I just
> back up the database every time it changes. If I am careful about using
> stored procedures to access data, and source control my application with
> corresponding database changes, then all my selects and joins are just the
> simple case, which is 99.9% of my needs (I will rarely need to go back to
a
> view of the database, but when I do, I want to do it quickly, adn not have
> to have someone call me up to do a restore for a particular date on a
> separate system). I was figuring that we were doing a backup of the
> database everyday anyway, why not mount it as a new database with the name
> as a date.
> If, however, there is a simpler way to do this that I am not aware of,
> please, feel free to let me know!
> Thanks,
> -Paul
>
> "Adam Machanic" <amachanic@.air-worldwide.nospamallowed.com> wrote in
message
> news:egfxIk$IEHA.3308@.TK2MSFTNGP10.phx.gbl...
> > Why?
> >
> > What's wrong with just storing a timestamp in a single database?
> >
> > This scheme will give you an incredible maintenance and development
> > nightmare, especially should you choose to change a column definition
> (want
> > to do that across 500 databases? I don't)... Or if you ever wish to
pull
> > historical aggregate data, please get a book because you'll need some
> > entertainment while waiting for your query to pull everything together
> from
> > 500 tables across 500 databases. And I don't know who's going to write
> that
> > query :)
> >
> > Anyway, the point is, unless you have a REALLY good reason, you should
not
> > do this... And a million rows of historical data will not affect your
> > performance in the slightest bit if you tune your indexes properly.
> >
> >
> >
> > "Paul" <a@.b.com> wrote in message
> > news:ueJzsB$IEHA.1388@.TK2MSFTNGP09.phx.gbl...
> > > Would it be possible to make a daily database version to avoid adding
a
> > data
> > > date to every piece of data in the system. For example, if I have a
> > > database named MyDatabase, at the end of processing for the day of
> > 4/16/04,
> > > I make a backup, and restore the database as MyDatabase-2004-16-04.
> This
> > > process would continue every day we run processing. We would probably
> > only
> > > need real time access to 18 months of data, and would delete the
oldest
> > > ones. Our database is probably not going to get very large, one or
two
> > > dozen tables, a million or so rows at most.
> > >
> > > What things do you think I would need to worry about in doing
something
> > like
> > > this? The "old" databases would not need to be referenced all that
> often,
> > > and could even be stored on a second server so they don't interfere
with
> > the
> > > "current" database's performance. I am concerned that SQL Server
might
> > have
> > > a problem with having 550 databases defined.
> > >
> > > Thanks,
> > >
> > > -Paul
> > >
> > >
> >
> >
>