我有一个表,其中的
registrationDate
列具有 datetime
数据类型。我需要找到该列的时间部分为 00:00:00.000
的所有行。
例如:
registrationDate
-----------------------
2019-03-20 00:00:00.000
我需要查询类似如下的内容:
select *
from table
where registrationDate like '%00:00:00.000'
您可以将日期时间转换为时间:
WHERE CAST(registrationDate AS TIME) = '00:00'
WHERE registrationDate = CONVERT(date, registrationDate);
请参阅下面的更新
Create Table #tbl
(
registrationDate Datetime
)
Insert Into #tbl Values
('2019-03-20 00:00:00.000'),
('2019-03-20 00:00:25.000')
查询
Select * From #tbl
Where Cast(registrationDate As Time) = '00:00:00.0000000'
结果:
registrationDate
2019-03-20 00:00:00.000
更新:如果你确实需要使用“喜欢”
Select * From #tbl
Where convert(VarChar(50), registrationDate, 121) Like '%00:00:00.000'
select * from table where FORMAT(registrationDate, 'HH:mm:ss:ms') ='00:00:00.000'
以下表达式为每个日期时间值获取带有时间部分的相应日期:
dateadd(day, datediff(day, 0, '<date_time>'), 0)
因此,以下内容将完成这项工作:
create table foo (id int, registrationDate datetime)
insert foo values
(1, '2019-04-04T03:22:48.00'),
(2, '2019-04-04T00:00:00.00')
select * from foo
where registrationDate =
dateadd(day, datediff(day, 0, registrationDate), 0)