convert varchar to date and subtract it from anoth

2019-08-22 01:19发布

问题:

How do I convert varchar into date in MS sql Server.?

eg - there is a number like this --> 19690409. This is of type varchar. It has to converted into a date format like 1969-04-09(yyyy-mm-dd). After converting how do I subtract this date from 2015-07-01(yyyy-mm-dd).

Please help

回答1:

You could try to use the convert() and the datediff() functions as below:

select convert(date, '19690409') -- the date you need
select datediff(day, convert(date, '19690409'), '2015-07-01') as diff_in_days

The difference between those two dates is in days, but you can chose from many possible options as stated in the documentation.