当前位置:首页 » 编程语言 » mssql日期查询
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

mssql日期查询

发布时间: 2023-04-08 19:44:34

Ⅰ 【求各位大神!】sql查询出本月所有日期

--MSSQL

--查询出本年所有日期:

SELECTCONVERT(VARCHAR(10),DATEADD(DD,number,DATENAME(YY,GETDATE())+'0101'),23)
FROMmaster..spt_values
WHEREtype='p'ANDnumber<=DATEDIFF(DD,DATENAME(YY,GETDATE())+'0101',DATENAME(YY,GETDATE())+'1231')


--查询出本月所有日期:

SELECTCONVERT(VARCHAR(10),DATEADD(DD,number,DATENAME(YY,GETDATE())+DATENAME(MM,GETDATE())+'01'),23)
FROMmaster..spt_values
WHEREtype='p'ANDnumber<DAY(DATEADD(MM,1,GETDATE())-DAY(GETDATE()))

Ⅱ 关于SQL模糊查询日期时间的方法

1、野塌Convert转成String,在用Like查询。

select*fromtable1 whereconvert(varchar,yourtime,120) like '2017-06-30%'

2、Between

select*fromtable1whereyourtime between '2017-06-30 0:00:00' and '2017-06-30 24:59:59'";

3、datediff()函数芹历

select*fromtable1 wheredatediff(day,yourtime,'2017-06-30')=0

(2)mssql日期查询扩展阅读

表达式DateDiff(timeinterval,date1,date2 [, firstdayofweek [, firstweekofyear]])

允许数据类型: timeinterval 表示相颂首圆隔时间的类型,代码为:

年份 yy、yyyy 季度 qq、q

月份 mm、m

每年的某一日 dy、y

日期 dd、d

星期 wk、ww

工作日 dw

小时 hh

分钟 mi、n

秒 ss、s

毫秒 ms

Ⅲ 使用sql语句查询日期的方法

使用sql语句查询日期的方法

结构化查询语言(Structured Query Language)简称SQL,是一种特殊目的的编程语言,是一种数据库查询和程序设计语言,用于存取数据以及查询、更新和管理关系数据库系统。以下是我为大家整理的使用sql语句查询日期的方法,仅供参考,希望能够帮助大家。

使用sql语句查询日期的方法 篇1

使用sql语句查询日期

select * from ShopOrder where datediff(week,ordTime,getdate()-1)=0 //查询当天日期在一周年的数据

--查询当天:

select * from info where DateDiff(dd,datetime,getdate())=0

--查询24小时内的:

select * from info where DateDiff(hh,datetime,getDate())<=24

--info为表名,datetime为数据库中的字段值

--查询当天:

select * from info where DateDiff(dd,datetime,getdate())=0

--查询24小时内的:

select * from info where DateDiff(hh,datetime,getDate())<=24 --info为表名,datetime为数据库中的字段值

Sql代码

--查询当天记录另类的方法

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

--查询当天记录另类的方法

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

DATEDIFF 函数:

语法:

DATEDIFF ( datepart , startdate , enddate )

备注:enddate 减去 startdate。如果 startdate 晚于 enddate,则返回负值。 如果结果超出整数值范围,则 DATEDIFF 将产生错误。对于毫秒,最大数是 24 天 20 小时 31 分钟零 23.647 秒。对于秒,最大数是 68 年。

跨分钟、秒和毫秒等边界计算的方法使得 DATEDIFF 指定的结果在所有数据类型中均一致。结果是带正负号的整数值,它等于跨第一个和第二个日期间的

datepart 边界数。例如,在 1 月 4 日(星期日)和 1 月 11 日(星期日)之间的'星期数是 1。

可以再MSSQL中测试:

Sql代码

--两个时间差刚好是24

--打印的方式

print dateDiff(hh,'2009-1-1 0:0:0','2009-1-2 0:0:0')

--查询的方式

print dateDiff(hh,'2009-1-1 0:0:0','2009-1-2 0:0:0')

--两个时间差刚好是24

--打印的方式

print dateDiff(hh,'2009-1-1 0:0:0','2009-1-2 0:0:0')

--查询的方式

print dateDiff(hh,'2009-1-1 0:0:0','2009-1-2 0:0:0')

Sql代码

--本月记录

SELECT * FROM 表 WHERE datediff(month,[dateadd],getdate())=0

--本周记录

SELECT * FROM 表 WHERE datediff(week,[dateadd],getdate())=0

--包括本年这些查询方式是一样的

--本月记录

SELECT * FROM 表 WHERE datediff(month,[dateadd],getdate())=0

--本周记录

SELECT * FROM 表 WHERE datediff(week,[dateadd],getdate())=0

--包括本年这些查询方式是一样的

sql server中的时间函数

1. 当前系统日期、时间

select getdate()

2. dateadd 在向指定日期加上一段时间的基础上,返回新的 datetime 值 例如:向日期加上2天

select dateadd(day,2,'2004-10-15') --返回:2004-10-17 00:00:00.000

3. datediff 返回跨两个指定日期的日期和时间边界数。

select datediff(day,'2004-09-01','2004-09-18') --返回:17

4. datepart 返回代表指定日期的指定日期部分的整数。

SELECT DATEPART(month, '2004-10-15') --返回 10

5. datename 返回代表指定日期的指定日期部分的字符串

SELECT datename(weekday, '2004-10-15') --返回:星期五

6. day(), month(),year() --可以与datepart对照一下

select 当前日期=convert(var10),getdate(),120)

,当前时间=convert(var8),getdate(),114)

select datename(dw,'2004-10-15')

select 本年第多少周=datename(week,'2004-10-15')

,今天是周几=datename(weekday,'2004-10-15')

函数 参数/功能

GetDate( ) 返回系统目前的日期与时间

DateDiff (interval,date1,date2) 以interval 指定的方式,返回date2 与date1两个日期之间的差值 date2-date1

DateAdd (interval,number,date) 以interval指定的方式,加上number之后

的日期

DatePart (interval,date) 返回日期date中,interval指定部分所对应的整数值

DateName (interval,date) 返回日期date中,interval指定部分所对应的字符串名称

参数 interval的设定值如下:

值 缩 写(Sql Server) Access 和 ASP 说明

Year Yy yyyy 年 1753 ~ 9999

Quarter Qq q 季 1 ~ 4

Month Mm m 月1 ~ 12

Day of year Dy y 一年的日数,一年中的第几日 1-366

Day Dd d 日,1-31

Weekday Dw w 一周的日数,一周中的第几日 1-7

Week Wk ww 周,一年中的第几周 0 ~ 51

Hour Hh h 时0 ~ 23

Minute Mi n 分钟0 ~ 59

Second Ss s 秒 0 ~ 59

Millisecond Ms - 毫秒 0 ~ 999

access 和 asp 中用date()和now()取得系统日期时间;其中

DateDiff,DateAdd,DatePart也同是能用于Access和asp中,这些函数的用法也类似

举例:

1.GetDate() 用于sql server :select GetDate()

2.DateDiff('s','2005-07-20','2005-7-25 22:56:32')返回值为 514592 秒 DateDiff('d','2005-07-20','2005-7-25 22:56:32')返回值为 5 天

3.DatePart('w','2005-7-25 22:56:32')返回值为 2 即星期一(周日为1,周六为7)

DatePart('d','2005-7-25 22:56:32')返回值为 25即25号

DatePart('y','2005-7-25 22:56:32')返回值为 206即这一年中第206天 DatePart('yyyy','2005-7-25 22:56:32')返回值为 2005即2005年

使用sql语句查询日期的方法 篇2

datediff(week,zy_time,getdate())=0 //查询本周 datediff(month,zy_time,getdate())=0 //查询本月

本季:select * from table where datediff(qq,C_CALLTIME,getdate())=0

前半年1-6,后半年7-12:select * from table where datepart(mm,C_CALLTIME)/7 = datepart(mm,getdate())/7 1. 当前系统日期、时间 select getdate()

2. dateadd 在向指定日期加上一段时间的基础上,返回新的 datetime 值 例如:向日期加上2天

select dateadd(day,2,'2004-10-15') --返回:2004-10-17 00:00:00.000 3. datediff 返回跨两个指定日期的日期和时间边界数。

select datediff(day,'2004-09-01','2004-09-18') --返回:17 4. datepart 返回代表指定日期的指定日期部分的整数。 SELECT DATEPART(month, '2004-10-15') --返回 10 5. datename 返回代表指定日期的指定日期部分的字符串 SELECT datename(weekday, '2004-10-15') --返回:星期五 6. day(), month(),year() --可以与datepart对照一下 select 当前日期=convert(var10),getdate(),120) ,当前时间=convert(var8),getdate(),114) select datename(dw,'2004-10-15')

select 本年第多少周=datename(week,'2004-10-15') ,今天是周几=datename(weekday,'2004-10-15') 函数 GetDate( )

参数/功能

返回系统目前的日期与时间

以interval 指定的方式,返回

DateDiff

date2 与date1两个日期之间的

(interval,date1,date2)

差值 date2-date1 DateAdd 以interval指定的方式,加上(interval,number,date) number之后的日期

返回日期date中,interval指定

DatePart (interval,date)

部分所对应的整数值 返回日期date中,interval指定

DateName (interval,date)

部分所对应的字符串名称

参数 interval的设定值如下:

Access 和

缩 写(Sql Server)

ASP Yy Qq Mm

yyyy q m y d w ww h n s -

值 Year Quarter Month

说明

年 1753 ~ 9999 季 1 ~ 4 月1 ~ 12

一年的日数,一年中的第几日 1-366 日,1-31

一周的日数,一周中的第几日 1-7

周,一年中的第几周 0 ~ 51 时0 ~ 23 分钟0 ~ 59 秒 0 ~ 59 毫秒 0 ~ 999

Day of year Dy Day Weekday Week Hour Minute Second

Dd Dw Wk Hh Mi Ss

Millisecond Ms

access 和 asp 中用date()和now()取得系统日期时间;其中DateDiff,DateAdd,DatePart也同是能用于Access和asp中,这些函数的用法也类似

举例:

1.GetDate() 用于sql server :select GetDate()

2.DateDiff('s','2005-07-20','2005-7-25 22:56:32')返回值为 514592 秒 DateDiff('d','2005-07-20','2005-7-25 22:56:32')返回值为 5 天

3.DatePart('w','2005-7-25 22:56:32')返回值为 2 即星期一(周日为1,周六为7) DatePart('d','2005-7-25 22:56:32')返回值为 25即25号

DatePart('y','2005-7-25 22:56:32')返回值为 206即这一年中第206天 DatePart('yyyy','2005-7-25 22:56:32')返回值为 2005即2005年 sql 查询本周本月问题 ---求相差天数

select datediff(day,'2004-01-01',getdate())

--1.一个月第一天的

SELECT DATEADD(mm, DATEDIFF(mm,0,getdate()), 0)

--2.本周的星期一

SELECT DATEADD(wk, DATEDIFF(wk,0,getdate()), 0)

select dateadd(wk,datediff(wk,0,getdate()),6) --3.一年的第一天

SELECT DATEADD(yy, DATEDIFF(yy,0,getdate()), 0)

--4.季度的第一天

SELECT DATEADD(qq, DATEDIFF(qq,0,getdate()), 0)

--5.当天的半夜

SELECT DATEADD(dd, DATEDIFF(dd,0,getdate()), 0)

--6.上个月的最后一天

SELECT dateadd(ms,-3,DATEADD(mm, DATEDIFF(mm,0,getdate()), 0))

--7.去年的最后一天

SELECT dateadd(ms,-3,DATEADD(yy, DATEDIFF(yy,0,getdate()), 0))

--8.本月的最后一天

SELECT dateadd(ms,-3,DATEADD(mm, DATEDIFF(m,0,getdate())+1, 0))

--9.本年的最后一天

SELECT dateadd(ms,-3,DATEADD(yy, DATEDIFF(yy,0,getdate())+1, 0))

--10.本月的第一个星期一 select DATEADD(wk,

DATEDIFF(wk,0,dateadd(dd,6-datepart(day,getdate()),getdate())), 0) --查询本周注册人数

select count(*) from [user]

where datediff(week,create_day-1,getdate())=0

--上周注册人数

select count(*) from [user]

where datediff(week,create_day-1,getdate())=1

--本月注册人数

select count(*) from [user]

where datediff(month,create_day,getdate())=0

--上月注册人数

select count(*) from [user]

where datediff(month,create_day,getdate())=1

--如果要效率,这样写查询

--查询本周注册人数

where

create_day>=dateadd(day,2-datepart(weekday,getdate()),convert(varchar,getdate(),112)) and

create_day<dateadd(day,9-datepart(weekday,getdate()),convert(varchar,getdate(),112))

--上周注册人数

select count(*) from [user] where

create_day>=dateadd(day,-5-datepart(weekday,getdate()),convert(varchar,getdate(),112)) and

create_day<dateadd(day,2-datepart(weekday,getdate()),convert(varchar,getdate(),112))

--本月注册人数

select count(*) from [user]

where create_day>=dateadd(day,1-day(getdate()),convert(varchar,getdate(),112)) and

create_day<dateadd(month,1,dateadd(day,1-day(getdate()),convert(varchar,getdate(),112)))

--上月注册人数

select count(*) from [user] where

create_day>=dateadd(month,-1,dateadd(day,1-day(getdate()),convert(varchar,getdate(),112)))

and create_day<dateadd(day,1-day(getdate()),convert(varchar,getdate(),112))

--本周

select count(*) from User

where datediff(dd,create_day,getdate()) <= datepart(dw,getdate())

--上周

select count(*) from User

where datediff(dd,create_day,(getdate() - datepart(dw,getdate()))) <= 7

--本月

select count(*) from User

where datepart(mm,create_day) = datepart(mm,getdate()) --上月

where datepart(mm,create_day) = datepart(mm,getdate()) - 1 --本周

select count(*) from [User]

where datediff(dd,create_day,getdate()) <= datepart(dw,getdate())

--上周

select count(*) from [User]

where datediff(dd,create_day,(getdate() - datepart(dw,getdate()))) <= 7

--本月

select count(*) from [User]

where datepart(mm,create_day) = datepart(mm,getdate()) --上月

select count(*) from [User]

where datepart(mm,create_day) = datepart(mm,getdate()) - 1 学习

month(create_day)=month(getdate())本月 month(create_day)=month(getdate())-1 上月

补充 查询今日所有的

SELECT * from feedback WHERE (DATEDIFF(d,fedtime,GETDATE())=0) ORDER BY fedid DESC

;

Ⅳ SqlServer数据库中,日期的模糊查询

1、打开SQL Server Management Studio管理工具,点击【新建查询】,新建一个SQL书写窗口,用于演示SQL的执行。

Ⅳ sql查询当天记录

1、SQL在查询当天记录时要注意是从当天的0点0分0秒0毫秒开始,到次日0点0分0秒0毫秒截止,但不包含次日的0点0分0秒0毫秒。

2、注意:在不同数据库产品中,获得当天日期的函数不一样。

MSSQL获得当前日期:convert(varchar(10),Getdate(),120)

MYSQL获得当前日期:date(now())

Oracle获得当前日期:to_char(sysdate,'yyyy-mm-dd')

Access获得当前日期:date()

3、在各个数据库里获得当天的记录写法为(假设表名为:Table_1,日期列名为:date_col):

MSSQL获得当天记录:

select*fromtable_1wheredate_col>=convert(varchar(10),Getdate(),120)anddate_col<convert(varchar(10),dateadd(d,1,Getdate()),120)

MYSQL获得当天记录:

select*fromtable_1wheredate_col>=date(now())anddate_col<DATE_ADD(date(now()),INTERVAL1DAY)

Oracle获得当天记录:

select*fromtable_1wheredate_col>=to_char(sysdate,'yyyy-mm-dd')anddate_col<to_char(sysdate+1,'yyyy-mm-dd')

Access获得当天记录:

select*fromtable_1wheredate_col>=date()anddate_col<DateAdd("d",1,date())

4、另外,在查询的时候,尽量不要对列进行运算,因为日期列上若有索引,就无法使用索引了。

Ⅵ SQL 如何查询日期在一定范围内的数据

查询方法:

select * from 表 where 日期>='20130101' and 日期<='20130130'。

(6)mssql日期查询扩展阅读:

SQL是一种查询功能很强的语言,只要是数据库存在的数据,总能通过适当的方法将它从数据库中查找出来。

SQL中的查询语句只有一个:SELECT,它可与其它语句配合完成所有的查询功能。SELECT语句的完整语法,可以有6个子句。

完整的语法如下:

SELECT 目标表的列名或列表达式集合

FROM 基本表或(和)视图集合

〔WHERE条件表达式〕

〔GROUP BY列名集合〔HAVING组条件表达式〕〕

〔ORDER BY列名〔集合〕?〕

简单查询,使用TOP子句。

查询结果排序order by。

带条件的查询where,使用算术表达式,使用逻辑表达式,使用between关键字,使用in关键字,

模糊查询like。

网络—SQL数据库

Ⅶ 如何在MS SQL中进行日期查询!

用datediff()函数来做好了:

SELECT * FROM table
WHERE DateDiff(d,"2003-03-12",addtime) >裂衡谨= 0 AND DateDiff(d,addtime,"拦高2005-05-30") >=0

addtime是你的肆基日期字段。

Ⅷ SQL语句怎么查询在某日期之前的数据

工具/材料:Management Studio。

1、首先在桌面上,点击“Management Studio”缺衡图标。

Ⅸ mysql中怎么查询一周内,三个月内,半年内的数据

mysql中怎么查询一周内,三个月内x0dx0a使用sql语句查询日期在一周内的数据x0dx0aselect * from ShopOrder where datediff(week,ordTime,getdate()-1)=0 //查询当天袜枣丛日期在一周年的数据x0dx0aselect * from ShopOrder where datediff(day,ordTime,getdate()-1)=0 //查询当天的所有数据x0dx0a SELECT * FROM A where datediff(d,datetime,getdate()) <=30 //前30天x0dx0a SELECT * FROM A WHERE DATEDIFF(m, shijian, GETDATE()) <=1 // 上一月x0dx0a--查询当天: x0dx0aselect * from info where DateDiff(dd,datetime,getdate())=0 x0dx0ax0dx0a--查询24小时内的: x0dx0aselect * from info where DateDiff(hh,datetime,getDate())<=24 x0dx0ax0dx0a--info为表名,datetime为数据库中的字段值x0dx0a--查询当天:x0dx0aselect * from info where DateDiff(dd,datetime,getdate())=0x0dx0a--查询24小时内的:x0dx0aselect * from info where DateDiff(hh,datetime,getDate())<=24x0dx0a--info为表名,datetime为数据库中的字段值x0dx0aSql代码x0dx0a--查询当天记录另类的方法 x0dx0aSELECT * x0dx0aFROM j_GradeShop x0dx0aWHERE (GAddTime BETWEEN CONVERT(datetime, LEFT(GETDATE(), 10) + 00:00:00.000') x0dx0aAND CONVERT(datetime, LEFT(GETDATE(), 10) + 00:00:00.000') + 1) x0dx0aORDER BY GAddTime DESCx0dx0a--查询当天记录另类的方法x0dx0aSELECT *x0dx0aFROM j_GradeShopx0dx0aWHERE (GAddTime BETWEEN CONVERT(datetime, LEFT(GETDATE(), 10) + 00:00:00.000')x0dx0aAND CONVERT(datetime, LEFT(GETDATE(), 10) + 00:00:00.000') + 1)x0dx0aORDER BY GAddTime DESCx0dx0aDATEDIFF 函数:x0dx0a语法:x0dx0aDATEDIFF ( datepart , startdate , enddate )x0dx0a备注:enddate 减去 startdate。岩芦如果 startdate 晚于 enddate,则返回负值。x0dx0a如果结果超出整数值范围,则 DATEDIFF 将产生错误。对于毫秒,最大数是 24 天 20 小时 31 分钟零 23.647 秒。对于秒,最大数是 68 年。x0dx0a跨分钟、秒和毫秒等边界计算的方法使得 DATEDIFF 指定的结果在所有数据类型中均一致。结果是带正负号的整数值,它等于跨第一个和第二个日期间的 datepart 边界数。例如,在 1 月 4 日(星期日)和 1 月 11 日(星期日)之间的星期数是 1。x0dx0a可以再MSSQL中测试:x0dx0aSql代码x0dx0a--两个时间差刚好是24 x0dx0a--打印的方式告樱 x0dx0aprint dateDiff(hh,񟭉-1-1 0:0:0',񟭉-1-2 0:0:0') x0dx0a--查询的方式 x0dx0aprint dateDiff(hh,񟭉-1-1 0:0:0',񟭉-1-2 0:0:0')x0dx0a--两个时间差刚好是24x0dx0a--打印的方式x0dx0aprint dateDiff(hh,񟭉-1-1 0:0:0',񟭉-1-2 0:0:0')x0dx0a--查询的方式x0dx0aprint dateDiff(hh,񟭉-1-1 0:0:0',񟭉-1-2 0:0:0')x0dx0aSql代码x0dx0a--本月记录 x0dx0aSELECT * FROM 表 WHERE datediff(month,[dateadd],getdate())=0

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

(10)mssql日期查询扩展阅读:

注意事项

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