Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 help with date function

Author  Topic 

amos
Starting Member

5 Posts

Posted - 2009-10-15 : 14:01:29
hello everyone,
i found this on the web, it returns the first day of last week:
SELECT DATEADD(wk,DATEDIFF(wk,6,GETDATE()-7),6)i cant understand how it works so tried to separate the function:

* GETDATE()-7 = returns today - 7 days

* DATEDIFF(wk,6,GETDATE()-7) = returns 5726, why? what is this
number? why does it means how many weeks are from 6 until today -
7 days?

* DATEADD(wk,DATEDIFF(wk,6,GETDATE()-7),6) = add 5726 weeks to 6,
why? how does it works?

can someone explain me?
thanks all.

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2009-10-15 : 14:10:26
It's magic :)

no, not really - see if this helps solve a missing part of the puzzle. the "6" is implicitly converted to datetime. 0 represents 1/1/1900. 6 represents dateadd(day, 6, 0)which works out to: 1/7/1900. that day was a Sunday.

Be One with the Optimizer
TG
Go to Top of Page

amos
Starting Member

5 Posts

Posted - 2009-10-15 : 16:12:20
thanks for the help,
one more question:

DATEDIFF(wk,6,GETDATE()-7) = returns 5726. what is this
number? why does it means how many weeks are from 6 until today -
7 days?

thanks a lot.
Go to Top of Page

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2009-10-15 : 16:29:30
>>what is this number?
there were 5726 weeks between 1/7/1900 and seven days ago.

Be One with the Optimizer
TG
Go to Top of Page
   

- Advertisement -