SQL时间段查询实例分享

发布时间:2020-12-18编辑:脚本学堂
分享一些sql时间段查询的例子,掌握下sql时间查询的方法,有需要的朋友做个参考。

本节内容:
sql时间段查询
 
ACCESS:
 

复制代码 代码示例:
select * from table where date1<#2008-1-20# and date2>#2007-8-30#
 

mysql
 

复制代码 代码示例:
select * from table where date1<'2008-1-20' and date2>'2007-3-30'
或者:
select * from table where date1 between '2008-1-20' and '2007-3-30'

以上mysql时间查询中,其中date1,date2都是日期类型的字段。
 
sQL时间型查询语句:
2007年12月04日 星期二 13:54
select * from jy_jjgl a ,jy_jygl b where convert(varchar(10),a.shtime,120)='2007-11-27'
1,显示本月第一天
 

复制代码 代码示例:
SELECT dateadd(mm,datediff(mm,0,getdate()),0)
select convert(datetime,convert(varchar(8),getdate(),
120)+'01',120)
 

2,显示本月最后一天
 

复制代码 代码示例:
select dateadd(day,-1,convert(datetime,convert
(varchar(8),dateadd(month,1,getdate()),120)+'01',120))
SELECT dateadd(ms,-3,DATEADD(mm,DATEDIFF(m,0,getdate())+1,0))
 

3,上个月的最后一天
 

复制代码 代码示例:
SELECT dateadd(ms,-3,DATEADD(mm,DATEDIFF(mm,0,getdate()),0))

4,本月的第一个星期一i
 

复制代码 代码示例:
select DATEADD(wk,DATEDIFF(wk,0, dateadd(dd,6-datepart(
day,getdate()),getdate())),0)
 

5,本年的第一天
 

复制代码 代码示例:
SELECT DATEADD(yy,DATEDIFF(yy,0,getdate()),0)

6,本年的最后一天
 

复制代码 代码示例:
SELECT dateadd(ms,-3,DATEADD(yy,DATEDIFF(yy,0,getdate())+1,0))

7,去年的最后一天
 

复制代码 代码示例:
SELECT dateadd(ms,-3,DATEADD(yy,DATEDIFF(yy,0,getdate()),0))
 

8,本季度的第一天
 

复制代码 代码示例:
SELECT DATEADD(qq,DATEDIFF(qq,0,getdate()),0)
 

9,本周的星期一
 

复制代码 代码示例:
SELECT DATEADD(wk,DATEDIFF(wk,0,getdate()),0)
 

10,查询本月的记录
 

复制代码 代码示例:
select * from tableName where DATEPART(mm, theDate)
=DATEPART(mm, GETDATE()) and DATEPART(yy, theDate)
= DATEPART(yy, GETDATE())

11,查询本周的记录
 

复制代码 代码示例:
select * from tableName where DATEPART(wk, theDate) = DATEPART
(wk, GETDATE()) and DATEPART(yy, theDate) = DATEPART(yy, GETDATE())

12,查询本季的记录 注:其中:GETDATE()是获得系统时间的函数。
 

复制代码 代码示例:
select * from tableName where DATEPART(qq, theDate) = DATEPART
(qq, GETDATE()) and DATEPART(yy, theDate) = DATEPART(yy, GETDATE())

13,获取当月总天数:
 

复制代码 代码示例:
select DATEDIFF(dd,getdate(),DATEADD
(mm, 1, getdate()))
select datediff(day,
dateadd(mm, datediff(mm,'',getdate()), ''),
dateadd(mm, datediff(mm,'',getdate()), '1900-02-01'))

14,获取当前为星期几
 

复制代码 代码示例:

DATENAME(weekday, getdate())

–返回当前时间
mysql> select now(),date(now()),sysdate();
+---------------------+-------------+---------------------+
| now()               | date(now()) | sysdate()           |
+---------------------+-------------+---------------------+
| 2008-12-02 10:11:36 | 2008-12-02  | 2008-12-02 10:11:36 |
+---------------------+-------------+---------------------+
1 row in set (0.00 sec)
mysql> select curdate(),curdate()+0,curtime(),curtime()+0;      
+---------------------+-------------+---------------------+
| curdate()  | curdate()+0 | curtime() | curtime()+0   |
+---------------------+-------------+---------------------+
| 2008-12-02 | 20081202    | 10:00:33  | 100033.000000 |
+---------------------+-------------+---------------------+
–返回日期当月最后一天
mysql> select last_day('2008-12-02');
+------------------------+
| last_day('2008-12-02') |
+------------------------+
| 2008-12-31             |
+------------------------+
1 row in set (0.00 sec)
–返回日期的星期几
mysql> select dayname('2008-12-02'),dayofweek('2008-12-02');
+-----------------------+-------------------------+
| dayname('2008-12-02') | dayofweek('2008-12-02') |
+-----------------------+-------------------------+
| tuesday               |                       3 |
+-----------------------+-------------------------+
1 row in set (0.00 sec)
–返回日期的年,月,日
mysql> select month('2008-12-02'),year('2008-12-02'),day('2008-12-02');
+---------------------+--------------------+-------------------+
| month('2008-12-02') | year('2008-12-02') | day('2008-12-02') |
+---------------------+--------------------+-------------------+
|                  12 |               2008 |                 2 |
+---------------------+--------------------+-------------------+
1 row in set (0.00 sec)
–返回日期的小时,分,秒
mysql> select hour('10:05:03'),minute('10:05:03'),second('10:05:03');      
+------------------+--------------------+--------------------+
| hour('10:05:03') | minute('10:05:03') | second('10:05:03') |
+------------------+--------------------+--------------------+
|               10 |                  5 |                  3 |
+------------------+--------------------+--------------------+
1 row in set (0.00 sec)

1,subdate(d,t):起始时间加上一段时间(year,month,day…)
 

复制代码 代码示例:
mysql> select date_add('1998-01-02', interval 31 day),adddate('1998-01-02', 31);     
+-----------------------------------------+---------------------------+
| date_add('1998-01-02', interval 31 day) | adddate('1998-01-02', 31) |
+-----------------------------------------+---------------------------+
| 1998-02-02                              | 1998-02-02                |
+-----------------------------------------+---------------------------+
1 row in set (0.00 sec)
mysql> select date_add('1998-01-02',interval 2 year);
+-----------------------------------------------------+
| date_add('1998-01-02', interval 2 year)
+-----------------------------------------------------+
| 2000-01-02                                
+-----------------------------------------------------+
1 row in set (0.00 sec)
mysql> select date_add('1998-01-02', interval 2 hour);
+-----------------------------------------------------+
| date_add('1998-01-02', interval 2 hour)
+-----------------------------------------------------+
| 1998-01-02 02:00:00                   
+-----------------------------------------------------+
1 row in set (0.00 sec)

2,subdate(d,t):起始时间减去一段时间
 

复制代码 代码示例:
mysql> select subdate('1998-01-02', interval 31 day),subdate('1998-01-02', 31);
+----------------------------------------+---------------------------+
| subdate('1998-01-02', interval 31 day) | subdate('1998-01-02', 31) |
+----------------------------------------+---------------------------+
| 1997-12-02                             | 1997-12-02                |
+----------------------------------------+---------------------------+
1 row in set (0.00 sec)

3,addtime(d,t):起始时间d加入时间t
 

复制代码 代码示例:
mysql> select addtime('1997-12-31 23:59:50','00:00:05'), addtime('23:59:50','00:00:05') ;
+-------------------------------------------+--------------------------------+
| addtime('1997-12-31 23:59:50','00:00:05') | addtime('23:59:50','00:00:05') |
+-------------------------------------------+--------------------------------+
| 1997-12-31 23:59:55                       | 23:59:55                       |
+-------------------------------------------+--------------------------------+
1 row in set (0.00 sec)
 

4,subtime(d,t):起始时间d减去时间t
 

复制代码 代码示例:
mysql> select subtime('1997-12-31 23:59:50','00:00:05'), subtime('23:59:50','00:00:05');    
+-------------------------------------------+--------------------------------+
| subtime('1997-12-31 23:59:50','00:00:05') | subtime('23:59:50','00:00:05') |
+-------------------------------------------+--------------------------------+
| 1997-12-31 23:59:45                       | 23:59:45                       |
+-------------------------------------------+--------------------------------+
1 row in set (0.00 sec)

5,datediff(d1,d2):返回起始时间d1和结束时间d2之间的天数
 

复制代码 代码示例:
mysql> select datediff('1997-12-31 23:59:59','1997-12-30');
+----------------------------------------------+
| datediff('1997-12-31 23:59:59','1997-12-30') |
+----------------------------------------------+
|                                            1 |
+----------------------------------------------+
1 row in set (0.00 sec)

6,date_format(date,format):根据format字符串显示date值的格式
 

复制代码 代码示例:
mysql> select date_format('2008-12-02 22:23:00', '%y %m %m %h:%i:%s');
+---------------------------------------------------------+
| date_format('2008-12-02 22:23:00', '%y %m %m %h:%i:%s') |
+---------------------------------------------------------+
| 2008 12 12 22:23:00                                     |
+---------------------------------------------------------+
1 row in set (0.00 sec)

7,str_to_date(str,format) 字符串转化为时间
 

复制代码 代码示例:
mysql> select str_to_date('04/31/2004', '%m/%d/%y %h:%i:s');
+-----------------------------------------------+
| str_to_date('04/31/2004', '%m/%d/%y %h:%i:s') |
+-----------------------------------------------+
| 2004-04-31 00:00:00                           |
+-----------------------------------------------+
1 row in set (0.00 sec)

8,timestamp(expr) , timestamp(expr,expr2) :
对于一个单参数,该函数将日期或日期时间表达式 expr 作为日期时间值返回。
对于两个参数, 它将时间表达式 expr2添加到日期或日期时间表达式 expr 中,将theresult作为日期时间值返回。
 

复制代码 代码示例:
mysql> select timestamp('2003-12-31'), timestamp('2003-12-31 12:00:00','12:00:00');
+-------------------------+---------------------------------------------+
| timestamp('2003-12-31') | timestamp('2003-12-31 12:00:00','12:00:00') |
+-------------------------+---------------------------------------------+
| 2003-12-31 00:00:00     | 2004-01-01 00:00:00                         |
+-------------------------+---------------------------------------------+
1 row in set (0.00 sec)

9,取当天0点0分,下一天0点0分
 

复制代码 代码示例:
mysql> select timestamp(date(sysdate())),timestamp(adddate(date(sysdate()),1));     
+----------------------------+---------------------------------------+
| timestamp(date(sysdate())) | timestamp(adddate(date(sysdate()),1)) |
+----------------------------+---------------------------------------+
| 2008-12-02 00:00:00        | 2008-12-03 00:00:00                   |
+----------------------------+---------------------------------------+
1 row in set (0.00 sec)