首页 生活指南 正文内容

sql查询日期时间范围(sql查询日期时间范围char类型)

阿立指南 生活指南 2022-08-02 12:08:10 250 0

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 table1 where year(d)=2010 and month(d)=7 and day(d) between 1 and 31

and (Datepart(hour,d)=22 or Datepart(hour,d)6)

扩展资料:

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查询日期时间范围(sql查询日期时间范围char类型) 第1张

如何在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中的日期有一点不一样。

扩展资料:

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

sql查询日期范围内时间范围

where CONVERT(varchar(100), markdate+marktime, 25)='2018-01-02 8:00:00.000' and CONVERT(varchar(100), markdate+marktime, 25)='2018-01-03 8:00:00.000'

sql 查询时间、日期范围内的数据

select * from table

where CDate(Format(年月日,"yyyy-mm-dd")+时分秒)CDate('2012-01-01 19:00:00')

and CDate(Format(年月日,"yyyy-mm-dd")+时分秒)CDate('2012-01-04 10:00:00')

数据库的日期区间查询方法。

access中有个mid函数,可以用来截取字符串或者日期。

select * from 表名 where mid([TestTime],5,10) ='04/19/2013'其中,5代表截取的开始位置,从左数,10代表截取的长度。

数据库的日期区间查询有两种情况:

1:查询给定时间在开始时间列与结束时间列范围中数据;

2:查询日期列在开始时间列与结束时间列范围中数据。

第一种:,, = , =

select * from 表名 where 日期列 = to_date('2015-10-20 00:00:00','yyyy-mm-dd hh24:mi:ss')

and t.日期列 = to_date('2015-10-20 23:59:59','yyyy-mm-dd hh24:mi:ss')。

第二种 between and

select * from 表名 where 日期列 between to_date('2015-10-20 00:00:00','yyyy-mm-dd

hh24:mi:ss')and to_date('2015-10-20 23:59:59','yyyy-mm-dd hh24:mi:ss')。

扩展资料:

SQL数据库语句:

创建数据库:

CREATE DATABASE database-name。

删除数据库:

drop database dbname。

创建新表:

create table tabname(col1 type1 [not null] [primary key],col2 type2 [not null],..)。

删除新表:

drop table tabname。

增加一个列:

Alter table tabname add column col type。

添加主键:

Alter table tabname add primary key(col)。

删除主键:

Alter table tabname drop primary key(col)。

创建索引:

create [unique] index idxname on tabname(col….)。

删除索引:

drop index idxname。

创建视图:

create view viewname as select statement。

删除视图:

drop view viewname。

参考资料来源:百度百科-sql语句大全

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)

扩展资料:

注意事项

在写按时间段查询的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" 存放

欢迎 发表评论:

文章目录
    搜索
    111