October 28, 2010 at 2:26 am
Always worth considering a proper calendar table too..
October 28, 2010 at 12:06 pm
Another minor point. I deeply disagree with the use of:
FROM master..spt_values
WHERE TYPE = 'P'
AND number BETWEEN 1 AND 12
I think it's much better to just create your own in-line table of #s from 1 to 12.
SQL Server could remove the table/rows in a future release. Or, much worse, remove a single row from that range of values. Then you would get incorrect results.
Scott Pletcher, SQL Server MVP 2008-2010
October 28, 2010 at 12:16 pm
scott.pletcher (10/28/2010)
...SQL Server MVP 2008-2010
Pulling ranks, huh? 😀
N 56°04'39.16"
E 12°55'05.25"
October 28, 2010 at 12:37 pm
Pulling ranks, huh?
Lol, nah.
I love your technique, it's great. I like that general method -- every 7 days is always the same weekday -- rather than any method that relies on @@DATEFIRST.
Scott Pletcher, SQL Server MVP 2008-2010
October 28, 2010 at 1:07 pm
If anyone is wondering where 22801 came from:
Make Date function (like in VB)
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=22339
Although I would have assumed that anyone would realize without explanation that 22801/12 = 1900 😉
October 28, 2010 at 1:29 pm
Michael Valentine Jones (10/28/2010)
If anyone is wondering where 22801 came from:Make Date function (like in VB)
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=22339
Although I would have assumed that anyone would realize without explanation that 22801/12 = 1900 😉
Actually, it's 1900.083333333333. It's 22800/12=1900. When I saw the fraction, I really started puzzling over why this number was being used, and never looked to see what taking away one would do.
So, slowly, I'm starting to comprehend what is going on here...
Wayne
Microsoft Certified Master: SQL Server 2008
Author - SQL Server T-SQL Recipes
October 28, 2010 at 2:09 pm
In SQL Server integer math 22801/12 = 1900 = 22800/12 😀
May 10, 2016 at 6:17 am
{edit added this comnetn to wrong post!}
here's what i came up with to get the first date of the month, to the "latest" sunday 5pm
there might be a shorter datemath to do the same work, it's just the rabbithole i fell down
select
--beginning of current month
DATEADD(mm, DATEDIFF(mm,0,getdate()), 0) As StartOfMonth,
--the end of week(Sunday) plust a time component.
DATEADD(dd, DATEDIFF(dd,0,DATEADD(dd, DATEPART(DW,GETDATE())*-1 + 1, GETDATE())), 0)+ convert(time,'17:00:00') As TheSundayDate --last sunday
Lowell
Viewing 8 posts - 16 through 22 (of 22 total)
You must be logged in to reply to this topic. Login to reply