当前位置:首页 » 编程语言 » sql日期查询

sql日期查询

发布时间: 2022-01-21 15:04:35

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个字段 日期 ,时间,并用整形表示

⑵ sql 根据日期查询

不知道你什么数据库

sqlserver的

只做参考,因为没有处理各个月的天数,只是默认每一个月都是30天

如果你要精确的,只能自己建立基础维护表来关联了

selectnumberasid,t2.cont
frommaster..spt_valuest1leftjoin(selecttime,sum(price)cont
fromtest
groupbytime)t2ont1.number=day(t2.time)
wheret1.type='P'
ANDt1.numberbetween1and30

⑶ sql server 日期范围查询

SELECT * FROM 表明 WHERE 日期字段名 BETWEEN '20130101' AND '20130130'

或者:

SELECT * FROM 表明 WHERE 日期字段名 BETWEEN CONVERT(datetime,'2013-01-01',120) AND CONVERT(datetime,'2013-01-30',120)

(3)sql日期查询扩展阅读:

注意事项

在写按时间段查询的sql语句的时候 一般我们会这么写查询条件:

where date>='2010-01-01' and date<='2010-10-1'。

但是在实执行Sql时些语句会转换成这样:

where date>='2010-01-01 0:00:00' and date<='2010-10-1:0:00:00',再看这个条件的话,就会有些明白,那就是'2010-10-1 0:00:00' 之后的数据例如('2010-10-1:08:25:00')查不到,也就是说2010-10-1的数据查不到。

修改查询条件为:

where date>='2010-01-01' and date<='2010-10-1 23:59:59' 或 where date>='2010-01-01' and date<='2010-10-2'。

某个表某个字段是Datetime型 以"YYYY-MM-DD 00:00:00" 存放

⑷ 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)

(4)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_网络

⑸ 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'

⑹ SQL 日期型查询

CaseSql="where month(开始日期)="&month(Request("开始日期"))&" and day(开始日期)>=1 and day(开始日期)<="&day(Request("开始日期"))

或者如果不用日期的,只用月份的

“where month(开始日期)=”&month(Request("开始日期"))

⑺ SQL语句查询特定时间段的数据怎么写

SQL服务器:

Select*fromtablewhere'2008-7-1'和'2008-12-31'

访问

从表中选择发生日期>#2008-7-1#和发生日期<#2008-12-31#

就是这样:注意,SQLserver中的日期和访问有一点不同。

(7)sql日期查询扩展阅读:

SQL查询日期语句

Select*fromShopOrderwheredatediff(week,ordTime,getdate()-1)=0//查询第一年的日期

Select*fromShopOrder,其中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*

FROMj_GradeShop

其中(GAddTimeBETWEENCONVERT(datetime,LEFT(GETDATE(),10)+'00:00:00.000'))

并转换(datetime,LEFT(GETDATE(),10)+'00:00:00.00.000')+1)

由GAddTime指定的订单

⑻ SQL中查询日期语句

SQL SERVER:

用函数datepart处理就可以了,示例:
select datepart(weekday,getdate()) as 周内的第几日

select datepart(week,getdate()) as 年内的第几周

select datepart(quarter,getdate()) as 年内的第几季

周内的第几日
-----------
5

(所影响的行数为 1 行)

年内的第几周
-----------
34

(所影响的行数为 1 行)

年内的第几季
-----------
3

(所影响的行数为 1 行)

热点内容
androidia安装 发布:2025-02-13 06:12:14 浏览:12
jsmcc文件夹 发布:2025-02-13 06:11:26 浏览:170
算法与程序设计教案 发布:2025-02-13 06:10:51 浏览:55
ftp登录需要输入用户名和密码 发布:2025-02-13 06:03:33 浏览:398
数控编程代表 发布:2025-02-13 05:58:51 浏览:385
编程凸轮 发布:2025-02-13 05:38:21 浏览:691
判断素数的编程 发布:2025-02-13 05:29:25 浏览:618
androidaes加密 发布:2025-02-13 05:08:36 浏览:493
李宗瑞文件夹 发布:2025-02-13 04:27:59 浏览:611
phpparent的parent 发布:2025-02-13 04:18:08 浏览:457