SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Finding New Years Eve


Finding New Years Eve

Author
Message
Tom Thomson
Tom Thomson
One Orange Chip
One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)

Group: General Forum Members
Points: 25086 Visits: 12484
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

sknox
sknox
Hall of Fame
Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)Hall of Fame (3.9K reputation)

Group: General Forum Members
Points: 3898 Visits: 2920
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...?
:-P
Lon-860191
Lon-860191
SSCommitted
SSCommitted (2K reputation)SSCommitted (2K reputation)SSCommitted (2K reputation)SSCommitted (2K reputation)SSCommitted (2K reputation)SSCommitted (2K reputation)SSCommitted (2K reputation)SSCommitted (2K reputation)

Group: General Forum Members
Points: 1981 Visits: 278
Regardless, Happy New Year to all.
Thomas Abraham
Thomas Abraham
Hall of Fame
Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)

Group: General Forum Members
Points: 3763 Visits: 2256
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
Vera-428803
Vera-428803
Ten Centuries
Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)

Group: General Forum Members
Points: 1243 Visits: 451
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.
Steve Jones
Steve Jones
SSC Guru
SSC Guru (139K reputation)SSC Guru (139K reputation)SSC Guru (139K reputation)SSC Guru (139K reputation)SSC Guru (139K reputation)SSC Guru (139K reputation)SSC Guru (139K reputation)SSC Guru (139K reputation)

Group: Administrators
Points: 139355 Visits: 19412
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
My Blog: www.voiceofthedba.com
Michael Valentine Jones
Michael Valentine Jones
SSChampion
SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)

Group: General Forum Members
Points: 13685 Visits: 11848
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

SQLRNNR
SQLRNNR
SSC Guru
SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)

Group: General Forum Members
Points: 63529 Visits: 18570
Thanks Steve



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


SQL RNNR

Posting Performance Based Questions - Gail Shaw

baabhu
baabhu
SSCrazy
SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)

Group: General Forum Members
Points: 2381 Visits: 1218
Thanks Steve. Wish you a very happy new year.
gserdijn
gserdijn
SSCommitted
SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)

Group: General Forum Members
Points: 1604 Visits: 818
Happy New Year to everyone!



Dutch Anti-RBAR League
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search