SQL查询只需要午夜示例

时间:2019-02-21 13:01:10

标签: sql-server tsql timestamp

我正在对积算器运行查询,我只希望从00.00.00小时开始的样本。我搜索了是否可以在timestamp字段上使用通配符,但没有给出答案。我已经注释掉了下面的行,在该行中,我尝试仅对表中的“ ts”(时间戳)字段使用“ 00:00:00”,因为它不起作用。有什么办法只能从桌子上取出午夜的样品吗?

SELECT TOP (100) percent
       [ts] AS 'Timestamp'
      ,[value]
  FROM [enteliwebDB].[dbo].[UASTP_150000_TL63]
  where ts > dateadd(day, datediff(day, 0 ,getdate())-31, 0)
  --and ts = '00:00:00'

Timestamp                   value
2019-01-21 00:00:00.0040000 1122981.5
2019-01-21 01:00:00.0030000 1125681.625
2019-01-21 02:00:00.0020000 1128380.75
2019-01-21 03:00:00.0020000 1131080.5
2019-01-21 04:00:00.0020000 1133778.625
2019-01-21 05:00:00.0020000 1136477.625
2019-01-21 06:00:00.0030000 1139177.875
2019-01-21 07:00:00.0010000 1141877.375
2019-01-21 08:00:00.0030000 1144579.125
2019-01-21 09:00:00.0010000 1147277.625
2019-01-21 10:00:00.0030000 1149976.375
2019-01-21 11:00:00.0000000 1152676.25
2019-01-21 12:00:00.0020000 1155376.75
2019-01-21 13:00:00.0030000 1158078.25
2019-01-21 14:00:00.0040000 1160776.875
2019-01-21 15:00:00.0030000 1163476.125
2019-01-21 16:00:00.0000000 1166175
2019-01-21 17:00:00.0000000 1168872.75
2019-01-21 18:00:00.0020000 1171571.5
2019-01-21 19:00:00.0030000 1174271.875
2019-01-21 20:00:00.0010000 1176972.625
...

2 个答案:

答案 0 :(得分:1)

您有毫秒数,可以通过在午夜之后的第一秒仅包含时间来舍入。我正在使用datediff来计算自我们进入午夜以来整整经过的秒数

SELECT TOP (100) percent
       [ts] AS 'Timestamp'
      ,[value]
  FROM [enteliwebDB].[dbo].[UASTP_150000_TL63]
  where ts > dateadd(day, datediff(day, 0 ,getdate())-31, 0) and (DATEDIFF(second,cast(ts as date),ts)) = 0

答案 1 :(得分:0)

您可以使用转换输入TIME,然后将其与00:00:00进行比较:

AND CONVERT(TIME, [ts]) = '00:00:00'

或者如果您想要所有记录,而不管毫秒数是否总是始终为0(如您的示例):

AND CONVERT(TIME, [ts]) >= '00:00:00'
AND CONVERT(TIME, [ts])  < '00:00:01'