两个日期之间的天数差异

时间:2013-04-09 06:42:10

标签: sql sql-server date

我很陌生。 我需要在此查询的单独列中以天为单位进行区分 在t.1start和t1.finish之间。

谢谢,路易

SELECT ROW_NUMBER() OVER(ORDER BY TOWN) AS 'row', 
T1.Surname,
T1.Forename,
T1.Town,
T1.Description,
T1.Sex,
T1.DOB,
T1.start,
T1.finish,
FROM dbo.viewServiceContractFull T1 
WHERE
T1.finish>='2013/01/01'
and 
T1.finish<='2013/01/31'

我现在如何只查询某些城镇?

3 个答案:

答案 0 :(得分:1)

在SQL Server中,您可以使用

SELECT DATEDIFF (MyUnits, '2010-01-22 15:29:55.090', '2010-01-22 15:30:09.153')

答案 1 :(得分:0)

您可以使用DATEDIFF功能确定两个日期之间的间隔,您可以使用CAST/CONVERT将字符串日期转换为实际日期(如果需要),例如:

CAST('2010-01-22 15:29:55.090' AS DATETIME)

使用CONVERT功能可以选择日期的格式。

答案 2 :(得分:0)

SELECT ROW_NUMBER() OVER(ORDER BY TOWN) AS 'row', 
T1.Surname,
T1.Forename,
T1.Town,
T1.Description,
T1.Sex,
T1.DOB,
T1.start,
T1.finish, cast(DATEDIFF(DAY,T1.start,T1.finish) as varchar) as difference
FROM dbo.viewServiceContractFull T1 
WHERE
T1.finish>='2013/01/01'
and 
T1.finish<='2013/01/31'

另请查看this