Click here to monitor SSC
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
UDP Broadcaster
UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)

Group: General Forum Members
Points: 1464 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
SSCrazy
SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)

Group: General Forum Members
Points: 2030 Visits: 2707
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.
Ten Centuries
Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)

Group: General Forum Members
Points: 1331 Visits: 2822
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 Journeyman
SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)

Group: General Forum Members
Points: 79 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
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: 2404 Visits: 8055
DavidBridgeTechnology.com (1/9/2013)
When is New Years Eve anyway?



February 9th ;-)
DavidBridgeTechnology.com
DavidBridgeTechnology.com
SSC Journeyman
SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)

Group: General Forum Members
Points: 79 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 Journeyman
SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)

Group: General Forum Members
Points: 79 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.
Ten Centuries
Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)

Group: General Forum Members
Points: 1331 Visits: 2822
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