从varchar中选择日期差异

时间:2014-08-19 06:14:36

标签: sql sql-server tsql

我需要找到DateDiff nvarchargetdate值的方法:

create table dbo.DateChk
([DateString] [nvarchar](24) NULL)
insert into dbo.DateChk (DateString) values ('20130112'),('20131121'), ('20130924'),('20130515')

select getdate(), DateString 
---,  getdate()- DateString as DateDiff
from dbo.DateChk

1 个答案:

答案 0 :(得分:1)

只需使用DATEDIFF功能:

select getdate(), DateString 
---,  getdate()- DateString as DateDiff
, DATEDIFF(DAY,  DateString, getdate())
from dbo.DateChk

http://www.w3schools.com/sql/func_datediff_mysql.asp

http://msdn.microsoft.com/en-us/library/ms189794.aspx