SELECT a.dt AS '时间' FROM(
SELECT dateadd(d, number, '2020-11-01') dt
FROM master..spt_values
WHERE type = 'p'
AND dateadd(d, number, '2020-11-01') <= '2020-11-30'
) a
select convert(varchar(7),dateadd(mm,-t.number,getdate()),120)
from(
select number from master..spt_values where type='P'
) t
where year(dateadd(mm,-t.number,getdate()))=year(getdate())
order by convert(varchar(7),dateadd(mm,-t.number,getdate()),120)
SELECT
a.dt AS '时间'
FROM
(
SELECT
CONVERT(VARCHAR(7),dateadd(dd, number, CONVERT(VARCHAR(10),dateadd(year,-1,getdate()),23)),23) dt
FROM
master..spt_values
WHERE
type = 'p'
AND dateadd(d, 1, CONVERT(VARCHAR(10),dateadd(d, number, CONVERT(VARCHAR(10),dateadd(year,-1,getdate()),23)),23)) <= (select CONVERT(VARCHAR(10),GETDATE(),23))
) a
GROUP BY a.dt
select convert(varchar(7),dateadd(mm,t.number,dateadd(year, datediff(year, 0, getdate()), 0)),120) month
from
(select number from master..spt_values
where type='P'AND number>=0 and number<=12) t
where year(dateadd(mm,t.number,dateadd(year, datediff(year, 0, getdate()), 0)))=year(getdate())
select 字段 from 表名
where (DATEPART(yy, 字段) = DATEPART(yy, GETDATE()))
AND (DATEPART(mm, 字段) = DATEPART(mm, GETDATE()))
select 字段 from 表名
where DATEPART(qq, 字段) = DATEPART(qq, GETDATE())
and DATEPART(yy, 字段) = DATEPART(yy, GETDATE())
今天的所有数据:
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
https://www.cnblogs.com/net-sky/p/11959312.html一个总结日期很好的文章,可以借鉴一下