|
|
|
SSCertifiable
       
Group: General Forum Members
Last Login: Today @ 12:25 PM
Points: 7,110,
Visits: 7,184
|
|
That's quite a mess, isn't it!
The only correct answer is "none of the above", for two reasons:
1) none of the select statements is syntactically valid. 2) even when the missing parentheses are added to make them syntactically valid, none of them will return 31.12.2012 on 31st Jan 2013 because February doesn't have 31 days this year.
If you want a select statement that does what is sepcified, the simple approach is
select dateadd(day,-datepart(day,GETDATE()),GETDATE()) edit: I see Hugo posted a better method with only one getdate() call and returning a date without any time.
Tom Is minic a gheibheann béal oscailte dorn dúnta. Is minig a cheapas beul fosgailte dòrn dùinte.
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Tuesday, May 21, 2013 9:31 AM
Points: 1,041,
Visits: 1,356
|
|
L' Eomot Inversé (1/1/2013)
That's quite a mess, isn't it! The only correct answer is "none of the above", for two reasons: 1) none of the select statements is syntactically valid. 2) even when the missing parentheses are added to make them syntactically valid, none of them will return 31.12.2012 on 31st Jan 2013 because February doesn't have 31 days this year. If you want a select statement that does what is sepcified, the simple approach is select dateadd(day,-datepart(day,GETDATE()),GETDATE()) edit: I see Hugo posted a better method with only one getdate() call and returning a date without any time.
Came to post this same thing, and saw that you and Hugo had already caught it. Darn you time zones!!!
...because February doesn't have 31 days this year. In Steve's defense, perhaps this code was originally written in a year where February did have 31 days...?
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Monday, May 06, 2013 8:39 AM
Points: 1,055,
Visits: 218
|
|
| Regardless, Happy New Year to all.
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Today @ 4:58 AM
Points: 1,152,
Visits: 1,457
|
|
You've all missed the biggest error - besides the parenthesis and February issues. This question is listed in category "humor", and isn't one bit funny! 
Happy New Year!
Please don't go. The drones need you. They look up to you.
|
|
|
|
|
Say Hey Kid
      
Group: General Forum Members
Last Login: Friday, May 10, 2013 5:25 AM
Points: 694,
Visits: 277
|
|
Happy New Year 
concerning the QoTD: none of the syntax will work because of missing parenthesis. But hey, it's a good laugh and that's what this catergory is about.
|
|
|
|
|
SSC-Dedicated
           
Group: Administrators
Last Login: Today @ 3:30 PM
Points: 31,436,
Visits: 13,751
|
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: Today @ 10:43 AM
Points: 2,945,
Visits: 10,517
|
|
I think this method with the nested DATEADD/DATEDIFF is simple and has the advantage of eliminating the time part.
select a.DT, [LastDayOfPriorMonth] = dateadd(mm,datediff(mm,-1,a.DT)-1,-1), [LastDayOfPriorYear] = dateadd(yy,datediff(yy,-1,a.DT)-1,-1) from ( -- Test Dates select top 32 DT = getdate()-1+ row_number() over (order by object_id) from sys.objects ) a order by a.DT
Results:
DT LastDayOfPriorMonth LastDayOfPriorYear ----------------------- ----------------------- ----------------------- 2013-01-01 20:47:27.733 2012-12-31 00:00:00.000 2012-12-31 00:00:00.000 2013-01-02 20:47:27.733 2012-12-31 00:00:00.000 2012-12-31 00:00:00.000 2013-01-03 20:47:27.733 2012-12-31 00:00:00.000 2012-12-31 00:00:00.000 2013-01-04 20:47:27.733 2012-12-31 00:00:00.000 2012-12-31 00:00:00.000 2013-01-05 20:47:27.733 2012-12-31 00:00:00.000 2012-12-31 00:00:00.000 2013-01-06 20:47:27.733 2012-12-31 00:00:00.000 2012-12-31 00:00:00.000 2013-01-07 20:47:27.733 2012-12-31 00:00:00.000 2012-12-31 00:00:00.000 2013-01-08 20:47:27.733 2012-12-31 00:00:00.000 2012-12-31 00:00:00.000 2013-01-09 20:47:27.733 2012-12-31 00:00:00.000 2012-12-31 00:00:00.000 2013-01-10 20:47:27.733 2012-12-31 00:00:00.000 2012-12-31 00:00:00.000 2013-01-11 20:47:27.733 2012-12-31 00:00:00.000 2012-12-31 00:00:00.000 2013-01-12 20:47:27.733 2012-12-31 00:00:00.000 2012-12-31 00:00:00.000 2013-01-13 20:47:27.733 2012-12-31 00:00:00.000 2012-12-31 00:00:00.000 2013-01-14 20:47:27.733 2012-12-31 00:00:00.000 2012-12-31 00:00:00.000 2013-01-15 20:47:27.733 2012-12-31 00:00:00.000 2012-12-31 00:00:00.000 2013-01-16 20:47:27.733 2012-12-31 00:00:00.000 2012-12-31 00:00:00.000 2013-01-17 20:47:27.733 2012-12-31 00:00:00.000 2012-12-31 00:00:00.000 2013-01-18 20:47:27.733 2012-12-31 00:00:00.000 2012-12-31 00:00:00.000 2013-01-19 20:47:27.733 2012-12-31 00:00:00.000 2012-12-31 00:00:00.000 2013-01-20 20:47:27.733 2012-12-31 00:00:00.000 2012-12-31 00:00:00.000 2013-01-21 20:47:27.733 2012-12-31 00:00:00.000 2012-12-31 00:00:00.000 2013-01-22 20:47:27.733 2012-12-31 00:00:00.000 2012-12-31 00:00:00.000 2013-01-23 20:47:27.733 2012-12-31 00:00:00.000 2012-12-31 00:00:00.000 2013-01-24 20:47:27.733 2012-12-31 00:00:00.000 2012-12-31 00:00:00.000 2013-01-25 20:47:27.733 2012-12-31 00:00:00.000 2012-12-31 00:00:00.000 2013-01-26 20:47:27.733 2012-12-31 00:00:00.000 2012-12-31 00:00:00.000 2013-01-27 20:47:27.733 2012-12-31 00:00:00.000 2012-12-31 00:00:00.000 2013-01-28 20:47:27.733 2012-12-31 00:00:00.000 2012-12-31 00:00:00.000 2013-01-29 20:47:27.733 2012-12-31 00:00:00.000 2012-12-31 00:00:00.000 2013-01-30 20:47:27.733 2012-12-31 00:00:00.000 2012-12-31 00:00:00.000 2013-01-31 20:47:27.733 2012-12-31 00:00:00.000 2012-12-31 00:00:00.000 2013-02-01 20:47:27.733 2013-01-31 00:00:00.000 2012-12-31 00:00:00.000
|
|
|
|
|
SSCoach
         
Group: General Forum Members
Last Login: Today @ 10:25 AM
Points: 18,754,
Visits: 12,337
|
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Tuesday, May 21, 2013 1:58 AM
Points: 1,112,
Visits: 970
|
|
| Thanks Steve. Wish you a very happy new year.
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Tuesday, April 23, 2013 8:00 AM
Points: 1,065,
Visits: 698
|
|
Happy New Year to everyone!
Dutch Anti-RBAR League
|
|
|
|