這樣實現日期格式比較正式通用:
select * from booking where cast([date] as varchar(19))
between convert(varchar(10),getdate()-1,120)+' 10:00:00'
and convert(varchar(10),getdate(),120)+' 10:00:00'
刚才有点错误,现在测了一下:
表booking
date
2003-11-05 12:00:00.000
2004-01-12 14:00:00.000
2004-01-12 15:00:00.000
2005-01-01 07:00:00.000
-----------------------------
sql语句:
select * from booking
where date between convert(varchar(10),dateadd(day,-1,getdate()),120)+' 10:00:00' and convert(varchar(10),getdate(),120)+' 10:00:00'
-----------结果-------------------
date
2004-01-12 14:00:00.000
2004-01-12 15:00:00.000
----------------------------------
(所影响的行数为 2 行)
--测试数据
declare @t table(dt datetime)
insert into @t
select '2004-01-13 12:01'
union all select '2004-01-13 09:01'
union all select '2004-01-13 10:30'
union all select '2004-01-13 10:01'
union all select '2004-01-12 12:01'
union all select '2004-01-12 11:01'
union all select '2004-01-12 10:00'
union all select '2004-01-12 09:01'
union all select '2004-01-12 08:01'
--查询
select * from @t
where datediff(day,dateadd(hour,-10,dt),getdate())=1
select * from booking
where DATEDIFF (hour,cast(convert(char(10),getdate()-1,120)+' 10:00:00' as datetime),[date]) <12 and DATEDIFF ( hour , cast(convert(char(10),getdate()-1,120)+' 10:00:00' as datetime),[date]) >0
select * from booking where date between to_date('2004-01-10 12:43:22','yyyy-mm-dd hh24:mi:ss') and to_date('2004-01-11 12:43:22','yyyy-mm-dd hh24:mi:ss')