在SQL中转换时间

时间:2019-05-15 14:13:29

标签: sql sql-server

我正试图在15分钟到1个小时之间适应。下面的SQL是我使用TIMESTAMPDIFF提出的。我如何得到“ TIMESTAMPDIFF”错误不是公认的内置函数名称。

我的SQL

SELECT Name, count(*)
FROM [test.database]
where TME between '2018-10-01 00:00:00.000' and '2019-01-31 00:00:00.999'
and TIMESTAMPDIFF(SECOND, date_trunc('SECOND', DT), date_trunc('SECOND', TME)) >= 900
and TIMESTAMPDIFF(SECOND, date_trunc('SECOND', DT), date_trunc('SECOND', TME)) < 3600
group by Name 
order by Name 

请有人帮我使我的SQL工作正常。

谢谢

2 个答案:

答案 0 :(得分:0)

大概您正在使用SQL Server,并且需要dateadd()。但是,我会使用select Name, count(*) from [test.database] where TME >= '2018-10-01' and TME < '2019-01-31' and TME >= dateadd(SECOND, 900, DT) and TME <= dateadd(SECOND, 3, DT) group by Name order by Name

初始日期比较看起来很麻烦。我不知道您为什么要到1月31日结束一秒钟。所以我猜您想要这样的事情:

disable-web-security

答案 1 :(得分:0)

SELECT Name, count(*)
FROM [test.database]

where (TME between '2018-10-01 00:00:00.000' and '2019-01-31 00:00:00.999')
and (datepart(SECOND,TME) between 15 and 60)
group by Name 
order by Name