Pages

Men

rh

3/25/2013

How to get First Day of next Year and Last Day of Next year in SQL

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 10:39: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 10:39:59.997 Last Day of Next Year

(2 row(s) affected)

No comments :

Post a Comment