Aiden Aiden - 4 months ago 62
SQL Question

Add days in date in yyyymmdd format SQL server

I have a hard-coded date in a variable in yyyymmdd format

DECLARE @StartDate = 20160101;

Now I want to add 365 days in this date.

When I do this 20160101 + 365, it gives incorrect output 20160466, it should give me answer after adding 365 days which I think is 20160102

Please tell me how to do it in SQL server in DECLARE variable ? I want output in yyyymmdd format



DECLARE @StartDate INT = '20161117';
select convert(varchar,CONVERT(datetime,convert(char(8),@StartDate))+365,112)