Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase ««12345»»»

Finding New Years Eve Expand / Collapse
Author
Message
Posted Tuesday, January 1, 2013 8:08 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 4:57 PM
Points: 7,745, Visits: 9,493
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
Post #1401604
Posted Tuesday, January 1, 2013 8:40 AM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: 2 days ago @ 6:45 AM
Points: 1,333, Visits: 1,701
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...?
Post #1401607
Posted Tuesday, January 1, 2013 8:44 AM
UDP Broadcaster

UDP BroadcasterUDP BroadcasterUDP BroadcasterUDP BroadcasterUDP BroadcasterUDP BroadcasterUDP BroadcasterUDP Broadcaster

Group: General Forum Members
Last Login: Tuesday, September 2, 2014 11:51 AM
Points: 1,443, Visits: 254
Regardless, Happy New Year to all.
Post #1401608
Posted Tuesday, January 1, 2013 8:52 AM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Today @ 9:12 AM
Points: 1,837, Visits: 2,181
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.
Connect to me on LinkedIn
Post #1401610
Posted Tuesday, January 1, 2013 1:03 PM


SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Wednesday, September 24, 2014 3:18 AM
Points: 925, Visits: 374
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.
Post #1401620
Posted Tuesday, January 1, 2013 5:11 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: Administrators
Last Login: Yesterday @ 5:59 PM
Points: 31,082, Visits: 15,529
Parens corrected, and my apologies. I changed to question to exclude Jan 31, this that doesn't work.

Happy New Year.

I'll award back points tomorrow.







Follow me on Twitter: @way0utwest

Forum Etiquette: How to post data/code on a forum to get the best help
Post #1401633
Posted Tuesday, January 1, 2013 6:50 PM
Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Monday, September 29, 2014 11:09 PM
Points: 3,108, Visits: 11,502
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
Post #1401637
Posted Tuesday, January 1, 2013 11:14 PM


SSCoach

SSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoach

Group: General Forum Members
Last Login: Today @ 9:34 AM
Points: 17,732, Visits: 15,599
Thanks Steve



Jason AKA CirqueDeSQLeil
I have given a name to my pain...
MCM SQL Server


SQL RNNR

Posting Performance Based Questions - Gail Shaw
Posting Data Etiquette - Jeff Moden
Hidden RBAR - Jeff Moden
VLFs and the Tran Log - Kimberly Tripp
Post #1401660
Posted Tuesday, January 1, 2013 11:39 PM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Thursday, September 18, 2014 10:01 PM
Points: 1,287, Visits: 1,122
Thanks Steve. Wish you a very happy new year.
Post #1401666
Posted Wednesday, January 2, 2013 12:03 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: 2 days ago @ 7:41 AM
Points: 1,255, Visits: 771
Happy New Year to everyone!





Dutch Anti-RBAR League
Post #1401673
« Prev Topic | Next Topic »

Add to briefcase ««12345»»»

Permissions Expand / Collapse