1.DATEADD函数
在向指定日期加上一段时间的基础上,返回新的 datetime 值。
语法
DATEADD ( datepart , number, date )
参数
datepart
日期部分 |
缩写 |
Year |
yy, yyyy |
quarter |
qq, q |
Month |
mm, m |
dayofyear |
dy, y |
Day |
dd, d |
Week |
wk, ww |
Hour |
hh |
minute |
mi, n |
second |
ss, s |
millisecond |
ms |
2. DATEDIFF函数
返回跨两个指定日期的日期和时间边界数
参数
datepart
是规定了应在日期的哪一部分计算差额的参数。
同上
测试数据:
if object_id('mma') is not null drop table mma
go
create table mma(mydate datetime)
go
insert into mma
select '20090203' union all
select '20090205' union all
select '20090209' union all
select '20091231' union all
select '20100102' union all
select '20100112' union all
select '20100116'
--查询返回本月的记录
方法一
select mydate from mma where datediff(m,mydate,getdate())=0(注当前日期是20100116)
方法二
SELECT * FROM mma WHERE CONVERT(CHAR(6), mydate,112)=CONVERT(CHAR(6),GETDATE(),112)
方法三
select mydate from aa where substring(convert(varchar(20),getdate(),120),0,8)=substring(convert(varchar(20),mydate,120),0,8)