1. sql 如何查询日期在一定范围内的数据
select * from 表 where 日期字段>='开始日期' and 日期字段<='截止日期' and convert(char(8),日期字段,108)>='开始时间' and convert(char(8),日期字段,108)<='截止时间'。
SELECT * FROM 表明 WHERE 日期字段名 BETWEEN '20130101' AND '20130130'。
例如:
select * from tb1 where dDate>='2010-11-05' and dDate<='2010-11-15'
and convert(char(8),dDate,108)>='8:00:00' and convert(char(8),dDate,108)<='9:00:00'.
select * from table1where year(d)=2010 and month(d)=7 and day(d) between 1 and 31
and (Datepart(hour,d)>=22 or Datepart(hour,d)<6)
(1)sql限定时间段查询扩展阅读:
SQL查询日期:
今天的所有数据:select * from 表名 where DateDiff(dd,datetime类型字段,getdate())=0
昨天的所有数据:select * from 表名 where DateDiff(dd,datetime类型字段,getdate())=1
7天内的所有数据:select * from 表名 where DateDiff(dd,datetime类型字段,getdate())<=7
30天内的所有数据:select * from 表名 where DateDiff(dd,datetime类型字段,getdate())<=30
本月的所有数据:select * from 表名 where DateDiff(mm,datetime类型字段,getdate())=0
本年的所有数据:select * from 表名 where DateDiff(yy,datetime类型字段,getdate())=0
参考资料:SQL_网络
2. SQL语句查询特定时间段的数据怎么写
select
*
from
your_table
where
trunc(your_date_column,'HH24')
between
to_date('2010070122','yyyymmddhh24')
and
to_date('2010073106','yyyymmddhh24');
--your_date_column就是指你的datetime格式的字段
--your_table就是你的table名
3. sql查询时间段
select * from 表 where 日期字段>='开始日期' and 日期字段<='截止日期'
and convert(char(8),日期字段,108)>='开始时间' and convert(char(8),日期字段,108)<='截止时间'
例如:
select * from tb1 where dDate>='2010-11-05' and dDate<='2010-11-15'
and convert(char(8),dDate,108)>='22:30:00' and convert(char(8),dDate,108)<='23:00:00'
4. SQL语句查询特定时间段的数据怎么写
where条件后面加上时间的判断就行啊,时间字段大于开始时间小于结束时间即可
5. sql怎么查询指定时间段内包含某个内容的所有数据
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()
6. 如何在SQL中按时间段查询数据
sql server:
select * from 表 where 发生日期>'2008-7-1' and 发生日期<'2008-12-31'
access:
select * from 表 where 发生日期>#2008-7-1# and 发生日期<#2008-12-31#
这样就可以了,注意sql server与access中的日期有一点不一样。
(6)sql限定时间段查询扩展阅读:
sql查询日期语句
select * from ShopOrder where datediff(week,ordTime,getdate()-1)=0 //查询当天日期在一周年的数据
select * from ShopOrder where datediff(day,ordTime,getdate()-1)=0 //查询当天的所有数据
SELECT * FROM A where datediff(d,datetime,getdate()) <=30 //前30天
SELECT * FROM A WHERE DATEDIFF(m, shijian, GETDATE()) <=1 //上一月
查询当天记录另类的方法:
SELECT *
FROM j_GradeShop
WHERE (GAddTime BETWEEN CONVERT(datetime, LEFT(GETDATE(), 10) + ' 00:00:00.000')
AND CONVERT(datetime, LEFT(GETDATE(), 10) + ' 00:00:00.000') + 1)
ORDER BY GAddTime DESC
7. 如何用SQL查询一个时间段内的特定时间数据
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))
8. sql 时间段查询
1.得到前十五秒的数据select * from table where datatime(时间字段) >=dateadd(second,-15,getdate()) and datatime<=getdate()2.select * from table where datatime(时间字段) >=getdate() 你上面那个每天时间是什么意思
9. SQL语句查询特定时间段的数据怎么写
select * from 表 where 日期字段>='开始日期' and 日期字段<='截止日期'
and convert(char(8),日期字段,108)>='开始时间' and convert(char(8),日期字段,108)<='截止时间'
10. sql如何按时间段来查询
select * from ms_cf01 a where a.kfrq between to_date('20100101 180000','yyyymmdd hh24miss')
and to_date('20101231 180000','yyyymmdd hh24miss')
and to_char(a.kfrq,'hh24miss') between '180000' and '240000'
主要用到 to_char,to_date对时间字段的转换方法,具体使用方法可
如果这么查询,主要是第2个条件无法用上索引,所以最好的方式是在涉及表的时候将该字段拆成2个字段 日期 ,时间,并用整形表示