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


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: Yesterday @ 7:19 PM
Points: 8,684, Visits: 9,214
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: Today @ 6:43 AM
Points: 1,306, Visits: 1,673
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
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Tuesday, August 19, 2014 8:13 AM
Points: 1,406, Visits: 252
Regardless, Happy New Year to all.
Post #1401608
Posted Tuesday, January 1, 2013 8:52 AM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: 2 days ago @ 8:26 AM
Points: 1,803, Visits: 2,168
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, August 6, 2014 7:37 AM
Points: 913, Visits: 356
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 @ 4:05 PM
Points: 33,165, Visits: 15,299
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: Yesterday @ 1:46 PM
Points: 3,135, Visits: 11,475
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


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 7:55 AM
Points: 21,625, Visits: 15,282
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: Monday, August 11, 2014 10:25 PM
Points: 1,287, Visits: 1,116
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: Today @ 12:32 AM
Points: 1,223, Visits: 756
Happy New Year to everyone!





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

Add to briefcase ««12345»»»

Permissions Expand / Collapse