Skip to content
Advertisement

Count number of days from a date then convert that count to a date-time?

I want to count the number of days from a date from a date-time column, let’s say one of the value is ’10/31/2018′ and I want to count up to 30 days. So in perspective it would look like ’11/30/2018′ in a new column. How do I convert that count to a date-time value?

I can’t get my head around this and I been trying to solve this. Any help would be great!

edit: Now suppose I want to add 30 days, 60 days and 90 days. How do I do that in the same line of code adding new columns for each count?

Advertisement

Answer

You just use dateadd

select dateadd(d,30, '10/31/2018') 

if your column it date from table tbl, you do

select *
, dateadd(d,30,date) as date30 
, dateadd(d,60,date) as date60 
, dateadd(d,90,date) as date90 
, dateadd(d,xx,date) as datexx 
from tbl
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement