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


Finding New Years Eve


Finding New Years Eve

Author
Message
john.arnott
john.arnott
Hall of Fame
Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)

Group: General Forum Members
Points: 3642 Visits: 3059
Or, since Nigel has led us outside the box....
Select convert(datetime, rtrim(Datepart(year,getdate())-1)+'-12-31')


Yeah, I cheat by using rtrim() as a quickly coded forced conversion to a string.
sknox
sknox
SSCarpal Tunnel
SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)

Group: General Forum Members
Points: 4061 Visits: 2932
nigel. (1/4/2013)
Thanks Steve for the question.

Thought I'd throw this in just for fun. Should work for every day of the year:

SELECT DATEADD(DAY, -DATEPART(dayofyear,GETDATE()),GETDATE())



The only problem with that is, since getdate() returns a datetime[2]*, you're actually returning some time during the day on New Years Eve rather than the day of New Years Eve itself.

Of course, as of SQL2008R2, this still works:
SELECT cast(cast(GETDATE() as int)-DATEPART(dayofyear,getdate())-1 as datetime)



If you have SQL 2008 or higher and want to avoid questionable conversions, you could just modify your code thus:
SELECT CAST(DATEADD(DAY, -DATEPART(dayofyear,GETDATE()),GETDATE()) as DATE)



*depending on your SQL Server version
nigel.
nigel.
Hall of Fame
Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)

Group: General Forum Members
Points: 3285 Visits: 2908
sknox (1/4/2013)
nigel. (1/4/2013)
Thanks Steve for the question.

Thought I'd throw this in just for fun. Should work for every day of the year:

SELECT DATEADD(DAY, -DATEPART(dayofyear,GETDATE()),GETDATE())



The only problem with that is, since getdate() returns a datetime[2]*, you're actually returning some time during the day on New Years Eve rather than the day of New Years Eve itself.
...


Very good point. How about this one:

SELECT DATEADD(year,DATEDIFF(YEAR,'19001231',GETDATE())-1,'19001231')



--
Nigel
Useful Links:
How to post data/code on a forum to get the best help
The "Numbers" or "Tally" Table - Jeff Moden

DavidBridgeTechnology.com
DavidBridgeTechnology.com
SSC Veteran
SSC Veteran (207 reputation)SSC Veteran (207 reputation)SSC Veteran (207 reputation)SSC Veteran (207 reputation)SSC Veteran (207 reputation)SSC Veteran (207 reputation)SSC Veteran (207 reputation)SSC Veteran (207 reputation)

Group: General Forum Members
Points: 207 Visits: 234
Whilst others are being picky I thought I would add my bit.

None of the answers are right

When is New Years Eve anyway?

If its January 9th (as it is today) then New years eve this year will be 31-12-2013.

31-12-2012 was new years eve LAST YEAR

Very picky I know but I think technically correct

Dave

David Bridge
David Bridge Technology Limited
www.davidbridgetechnology.com
Toreador
Toreador
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: 3849 Visits: 8125
DavidBridgeTechnology.com (1/9/2013)
When is New Years Eve anyway?



February 9th ;-)
DavidBridgeTechnology.com
DavidBridgeTechnology.com
SSC Veteran
SSC Veteran (207 reputation)SSC Veteran (207 reputation)SSC Veteran (207 reputation)SSC Veteran (207 reputation)SSC Veteran (207 reputation)SSC Veteran (207 reputation)SSC Veteran (207 reputation)SSC Veteran (207 reputation)

Group: General Forum Members
Points: 207 Visits: 234
Toreador (1/9/2013)
DavidBridgeTechnology.com (1/9/2013)
When is New Years Eve anyway?



February 9th ;-)



Ha Ha.:-P

David Bridge
David Bridge Technology Limited
www.davidbridgetechnology.com
DavidBridgeTechnology.com
DavidBridgeTechnology.com
SSC Veteran
SSC Veteran (207 reputation)SSC Veteran (207 reputation)SSC Veteran (207 reputation)SSC Veteran (207 reputation)SSC Veteran (207 reputation)SSC Veteran (207 reputation)SSC Veteran (207 reputation)SSC Veteran (207 reputation)

Group: General Forum Members
Points: 207 Visits: 234
-- In 2008 + you can use a date instead of a datetime with implicit conversion

DECLARE @Date date = GETDATE()

SELECT DATEADD(DAY, -DATEPART(dayofyear,@Date),@Date)

David Bridge
David Bridge Technology Limited
www.davidbridgetechnology.com
nigel.
nigel.
Hall of Fame
Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)

Group: General Forum Members
Points: 3285 Visits: 2908
DavidBridgeTechnology.com (1/9/2013)
Whilst others are being picky I thought I would add my bit.

None of the answers are right

When is New Years Eve anyway?

If its January 9th (as it is today) then New years eve this year will be 31-12-2013.

31-12-2012 was new years eve LAST YEAR

Very picky I know but I think technically correct

Dave


Correct, the original question didn't explicitly state whether it should return New Years Eve for this year or last, but the correct answer implied that it should be last years (2012/12/31)

--
Nigel
Useful Links:
How to post data/code on a forum to get the best help
The "Numbers" or "Tally" Table - Jeff Moden

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