时间段间隔查询
这是sql的问题,但是我在论坛找不到sql专区,先发到这里啦
我想进行这个样一个查询,从2013年4月1日到2013年4月20日位于每天的中午的数据。那么这个时间应该是这样的:
2013-4-1 10:30:00 --- 2013-4-1 14:00:00
2013-4-2 10:30:00 --- 2013-4-2 14:00:00
2013-4-3 10:30:00 --- 2013-4-3 14:00:00
2013-4-4 10:30:00 --- 2013-4-4 14:00:00
2013-4-5 10:30:00 --- 2013-4-5 14:00:00
2013-4-6 10:30:00 --- 2013-4-6 14:00:00
2013-4-7 10:30:00 --- 2013-4-7 14:00:00
2013-4-8 10:30:00 --- 2013-4-8 14:00:00
2013-4-9 10:30:00 --- 2013-4-9 14:00:00
2013-4-10 10:30:00 --- 2013-4-10 14:00:00
2013-4-11 10:30:00 --- 2013-4-11 14:00:00
2013-4-12 10:30:00 --- 2013-4-12 14:00:00
2013-4-13 10:30:00 --- 2013-4-13 14:00:00
2013-4-14 10:30:00 --- 2013-4-14 14:00:00
2013-4-15 10:30:00 --- 2013-4-15 14:00:00
2013-4-16 10:30:00 --- 2013-4-16 14:00:00
2013-4-17 10:30:00 --- 2013-4-17 14:00:00
2013-4-18 10:30:00 --- 2013-4-18 14:00:00
2013-4-19 10:30:00 --- 2013-4-19 14:00:00
2013-4-20 10:30:00 --- 2013-4-20 14:00:00
那么这个sql应该怎么写呢?
SQL
------解决方案--------------------替换table_name为你的数据表名,CreateDate为你的时间字段
select * from table_name where CreateDate between '2013-4-1' and '2013-4-20' and CreateDate between convert(datetime,convert(nvarchar(50),year(CreateDate))+'-'+CONVERT(nvarchar(50),month(CreateDate))+'-'+CONVERT(nvarchar(50),day(CreateDate))+' 10:30:00') and convert(datetime,convert(nvarchar(50),year(CreateDate))+'-'+CONVERT(nvarchar(50),month(CreateDate))+'-'+CONVERT(nvarchar(50),day(CreateDate))+' 14:00:00')