SQL: First and Last Day of Year


These lines of code will get you the First and Last day of the current year.

SQL:

SELECT DATEADD(yy, DATEDIFF(yy,0,getdate()), 0),'First Day of the Year'
UNION
SELECT DATEADD(dd,-1,DATEADD(yy,0,DATEADD(yy,DATEDIFF(yy,0,getdate())+1,0))),'Last Day of the Year'

Results:
2008-01-01 00:00:00.000 First Day of the Year
2008-12-31 00:00:00.000 Last Day of the Year

  1. #1 by vidya on May 11, 2010 - 4:08 AM

    Nice work is good..

  2. #2 by Scott on August 19, 2010 - 1:00 PM

    This is exactly what I needed, thanks.

  3. #3 by Terry Apodaca on August 19, 2010 - 4:07 PM

    Glad I could help Scott. I should post more stuff like this. I am needing these kinds of things all the time. Sharing so others can quickly use them.

  4. #4 by Will Schulz on January 6, 2011 - 1:09 PM

    Nice job!

  5. #5 by Fashion Mantra on January 19, 2011 - 10:12 PM

    great job.thanx

  6. #6 by dacree on April 21, 2011 - 2:55 PM

    why make it so hard? Those are fixed dates
    SELECT ’1-1-’ & Format(Date(),’yyyy’) AS ‘First Day of Year’, ’12-31-’ & Format(Date(),’yyyy’) AS ‘Last Day of Year’;

  7. #7 by Terry Apodaca on April 21, 2011 - 3:02 PM

    dacree :

    why make it so hard? Those are fixed dates
    SELECT ’1-1-’ & Format(Date(),’yyyy’) AS ‘First Day of Year’, ’12-31-’ & Format(Date(),’yyyy’) AS ‘Last Day of Year’;

    Because it’s code like yours that make most of us (Good) coders cringe! At least my code follows SQL standards, is easy to maintain, and probably will never have to be touched again once implemented.

    Hard Coded = FAIL!

  8. #8 by David on April 21, 2011 - 3:25 PM

    Amen Terry! It wouldn’t surprise me to see our government change the calendar, it’s not like there’s anything important to work on.

  9. #9 by Trevor on April 25, 2011 - 7:28 PM

  10. #10 by Sohel Nax on May 23, 2011 - 3:46 AM

    –GET FIRST DAY OF LAST MONTH

    SELECT DATEADD(MM, DATEDIFF(MM, ’01/01/2000′, DATEADD(MM, -1,GETDATE())), ’01/01/2000′) as [Date of First Day of Last Month]

    –GET LAST DAY OF LAST MONTH

    SELECT DATEADD(SS,-1,DATEADD(MM, DATEDIFF(MM,’01/01/2000′,GETDATE()),’01/01/2000′)) as [Date of Last Day of Last Month]

  11. #11 by Sohel Nax on May 23, 2011 - 3:47 AM

    –First Day of this year
    select DATEADD(yy, DATEDIFF(yy,0,getdate()), 0)

    –Last day of this year
    select dateadd(ms,-3,DATEADD(yy, DATEDIFF(yy,0,getdate() )+1, 0))

  12. #12 by Araceli on July 22, 2011 - 5:45 AM

    I need help in calculating First date Licensed format mm/dd/yyyy. I’m working on a project.
    Not sure what to use:
    What I have is Column storing when a quote was made,
    Quote_Date mm/dd/yyyy example:7/19/2010
    Date of Birth mm/dd/yyyy 06/12/1971
    System date mm/dd/yyyy? 07/22/2011
    Years_Licensed 12

  13. #13 by joe brown on October 20, 2011 - 11:14 PM

    –Last day of this year

    select dateadd(dd, -1, DATEADD(yy, DATEDIFF(yy,-1,getdate()), 0))

(will not be published)