将varchar转换为日期并从另一个日期减去 [英] convert varchar to date and subtract it from another date
本文介绍了将varchar转换为日期并从另一个日期减去的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在MS sql Server中将varchar转换为日期??
How do I convert varchar into date in MS sql Server.?
例如-像这样的数字->19690409。类型为varchar。它必须转换为日期格式,例如1969-04-09(yyyy-mm-dd)。转换后如何从2015-07-01(yyyy-mm-dd)中减去此日期。
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).
请帮助
推荐答案
您可以尝试使用 convert( )和 datediff()的功能如下:
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.
这篇关于将varchar转换为日期并从另一个日期减去的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文