convert varchar to date and subtract it from anoth

2019-08-22 00:43发布

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条回答
该账号已被封号
2楼-- · 2019-08-22 01:30

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.

查看更多
登录 后发表回答