Pages

Men

rh

3/25/2013

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

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 will be:

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

2010-12-31 20:39:29.997 Last Day of Previous Year

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

2011-12-31
20:39:29.997 Last Day of Current Year

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

2012-12-31
20:39:29.997 Last Day of Next Year

No comments :

Post a Comment