Search code examples
t-sqlsql-server-2000

First Day of Next Year for SQL Server?


This should not be this hard. I simply need the following:

SET @DueDate = CONVERT (DATETIME, '01/01/2010')

However, I need it pragmatically so that if it were March of 2010, the date given would be '01/01/2011'.

I know it's simple, but my brain isn't coming up with it. I'm sure it's with a DateAdd and getdate().


Solution

  • Number of year boundaries between now and year zero less one (31 dec 1899), add back on.

    SELECT DATEADD(year, DATEDIFF(year, -1, GETDATE()), 0)
    

    Let's try a date next year to get 2011. because 1 Jan 2010 is start of next year in 2009...

    SELECT DATEADD(year, DATEDIFF(year, -1, '2010-03-21'), 0)