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
Thanks,
Aiden
DECLARE @StartDate INT = '20161117';
select convert(varchar,CONVERT(datetime,convert(char(8),@StartDate))+365,112)
Put the date in quotes and then use DATEADD
:
DECLARE @StartDate = '2016-01-01';
SELECT DATEADD (day, 365, @StartDate)
FROM yourTable
DECLARE @Date DATE= '20160101'SELECT DATEADD(DAY,365,@Date)
FOR INT DATA TYPE :
DECLARE @Date INT = '20160101'
SELECT DATEADD(DAY,365,CONVERT (DATETIME,CONVERT(CHAR(8),@Date)))
DECLARE @StartDate INT = 20161117;
select cast(convert(varchar,cast(cast(@StartDate as varchar) as datetime)+366,112) as INT)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.