Archive

Posts Tagged ‘year start day’

How to get First and Last day of a Year – TSQL

17.05.2011 3 comments

Yesterday, I posted bout How to get the First and Last day of a Month. Same can be modified to calculate first/last day of a given year:

— © 2011 – Vishal (http://SqlAndMe.com)

 

— First/Last Day of the Year:

SELECT      DATEADD(YEAR, DATEDIFF(YEAR, 0,

            DATEADD(YEAR, 1, GETDATE())), 0),

            'First Day of Previous Year'

UNION ALL

SELECT      DATEADD(MILLISECOND, 3, DATEADD(YEAR,

            DATEDIFF(YEAR, 0, DATEADD(YEAR, 1, GETDATE())) + 1, 0)),

            'Last Day of Previous Year'

UNION ALL

SELECT      DATEADD(YEAR, DATEDIFF(YEAR, 0, GETDATE()), 0),

            'First Day of Current Year'

UNION ALL

SELECT      DATEADD(MILLISECOND, 3,

            DATEADD(YEAR, DATEDIFF(YEAR, 0, GETDATE()) + 1, 0)),

            'Last Day of Current Year'

UNION ALL

SELECT      DATEADD(YEAR, DATEDIFF(YEAR, 0,

            DATEADD(YEAR,1,GETDATE())), 0),

            'First Day of Next Year'

UNION ALL

SELECT      DATEADD(MILLISECOND, 3,

            DATEADD(YEAR, DATEDIFF(YEAR, 0,

            DATEADD(YEAR, 1, GETDATE())) + 1, 0)),

            'Last Day of Next Year'

Result Set:

———————– ————————–

2010-01-01 00:00:00.000 First Day of Previous Year

2010-12-31 23:59:59.997 Last Day of Previous Year

2011-01-01 00:00:00.000 First Day of Current Year

2011-12-31 23:59:59.997 Last Day of Current Year

2012-01-01 00:00:00.000 First Day of Next Year

2012-12-31 23:59:59.997 Last Day of Next Year

 

(6 row(s) affected)

These can be generalized as:

— © 2011 – Vishal (http://SqlAndMe.com)

 

DECLARE @TRAVELYEARS INT = 1

SELECT      DATEADD(YEAR, DATEDIFF(YEAR, 0,

            DATEADD(YEAR, @TRAVELYEARS ,GETDATE())), 0),

            'First Day of Year'

UNION ALL

SELECT      DATEADD(MILLISECOND, 3,

            DATEADD(YEAR, DATEDIFF(YEAR, 0,

            DATEADD(YEAR, @TRAVELYEARS, GETDATE())) + 1, 0)),

            'Last Day of Year'

 

SET @TRAVELYEARS = 1

SELECT      DATEADD(YEAR, DATEDIFF(YEAR, 0,

            DATEADD(YEAR, @TRAVELYEARS ,GETDATE())), 0),

            'First Day of Year'

UNION ALL

SELECT      DATEADD(MILLISECOND, 3,

            DATEADD(YEAR, DATEDIFF(YEAR, 0,

            DATEADD(YEAR, @TRAVELYEARS, GETDATE())) + 1, 0)),

            'Last Day of Year'

Result Set:

———————– ———————-

2010-01-01 00:00:00.000 First Day of Next Year

2010-12-31 23:59:59.997 Last Day of Next Year

 

(2 row(s) affected)

 

                       

———————– ———————-

2012-01-01 00:00:00.000 First Day of Next Year

2012-12-31 23:59:59.997 Last Day of Next Year

 

(2 row(s) affected)

Hope This Helps!

Vishal