|
|
|
SSC-Dedicated
           
Group: Administrators
Last Login: Today @ 5:39 AM
Points: 31,529,
Visits: 13,866
|
|
|
|
|
|
SSC-Addicted
      
Group: General Forum Members
Last Login: Wednesday, June 12, 2013 5:53 AM
Points: 444,
Visits: 137
|
|
| Thanks Steve for the new year gift. And Happy new year for entire "sqlservercenter" family..
|
|
|
|
|
SSC-Addicted
      
Group: General Forum Members
Last Login: 2 days ago @ 5:59 AM
Points: 413,
Visits: 66
|
|
Thanks Steve for the good question on new year.
Happy new year for everyone...
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Yesterday @ 4:19 AM
Points: 1,115,
Visits: 1,210
|
|
Happy New Year
See, understand, learn, try, use efficient © Dr.Plch
|
|
|
|
|
SSC Eights!
      
Group: General Forum Members
Last Login: Friday, June 07, 2013 4:08 AM
Points: 941,
Visits: 239
|
|
Just to be picky, it actually won't work after the 28th Jan due to that dateadd of a month in there 
Happy new year
Kev
------------------------------- Oh no!
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Today @ 5:33 AM
Points: 1,233,
Visits: 1,376
|
|
|
|
|
|
SSCertifiable
       
Group: General Forum Members
Last Login: Today @ 3:38 AM
Points: 5,300,
Visits: 7,244
|
|
Kevin Gill (1/1/2013)
Just to be picky, it actually won't work after the 28th Jan due to that dateadd of a month in there  And to be even more picky, all three answer options have a mismatch in the parentheses. Here is the corrected version:
-- select 1 SELECT dateadd(day,-(datepart(day,dateadd(mm,1,GETDATE()))),dateadd(mm,-1,GETDATE())) -- select 2 SELECT dateadd(day,-(datepart(day,dateadd(mm,1,GETDATE()))),GETDATE()) -- select 3 SELECT dateadd(day,-(datepart(day,dateadd(mm,1,GETDATE()))),dateadd(mm,1,GETDATE())) And for those who prefer to use efficient code to get the last day of the previous month, use either one of these options:
-- Option 1 SELECT dateadd(day, -datepart(day, getdate()), getdate()); -- Option 2 SELECT dateadd(month, datediff(month, getdate(), '20110101'), '20101231'); Option 2 has the additional advantage that you get only the day; all other options include a time part.
(None of this is intended as criticism of a question, just to provide some additional information).
Hugo Kornelis, SQL Server MVP Visit my SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
|
|
|
|
|
SSCertifiable
       
Group: General Forum Members
Last Login: Today @ 3:38 AM
Points: 5,300,
Visits: 7,244
|
|
|
|
|
|
SSC Eights!
      
Group: General Forum Members
Last Login: Yesterday @ 5:40 PM
Points: 902,
Visits: 505
|
|
I also noted the missing parentheses as well. I believe that adding a month before getting the day was simply a red herring to throw you off, but as stated previously it won't work for the entire month of January because February doesn't have 31 days in it! In any case, it was a good thought exercise!!
"...when ye are in the service of your fellow beings ye are only in the service of your God." -- Mosiah 2:17
|
|
|
|
|
SSC Veteran
      
Group: General Forum Members
Last Login: 2 days ago @ 8:30 AM
Points: 238,
Visits: 215
|
|
is it me the only one who finds all answers wrong as there's a missing parenthesis in all of them??
depends on where you place the missing one in 'select 2' you get the right answer or syntax error which is kinda common tricky answer for a QotD...
|
|
|
|