如何用SQL查询一个时间段内的特定时间数据?

2024-12-03 17:30:35
推荐回答(3个)
回答1:

datetime型的精度是微秒级的,楼上两位只写到秒,还是有出错的可能

将一个datetime取整(取到00:00)有3种方法:

convert(smalldatetime,convert(varchar,日期,112),112)

cast(cast(日期 as int) as smalldatetime)

dateadd(dd,datediff(dd,'2010-1-1',日期),'2010-1-1')

根据你的需求,用方法1,条件写成
where tm>='2010-3-1' and tm<'2010-4-1'
and tm=dateadd(hh,12,convert(smalldatetime,convert(varchar,tm,112),112))

回答2:

sql server
SELECT * FROM 表名 WHERE datepart(hour,tm)=12 and datepart(minute,tm)=0 and datepart(second,tm)=0 and datediff(month,tm,getdate())<1

access:用now()代替getdate()
oracle:用sysdate代替getdate()

回答3:

SELECT * FROM 表名 WHERE Hour(tm)=12 and Minute(tm)=0 and Second(tm)=0