隐藏

Sql Server 生成连续的日期列表

发布:2024/11/18 21:50:03作者:管理员 来源:本站 浏览次数:194



Sql Server 的内置表 master..spt_values n的利用:

具体表内容可自行查询:


SELECT

   CONVERT (VARCHAR (100),dateadd(d, number, GETDATE() + 1 - 7),23) AS 日期

FROM

   master..spt_values n

WHERE

   n.type = 'p'

AND n.number <= 7;





--sql server 查询当前月份日期列表数据


SELECT

CONVERT (VARCHAR (10), dateadd(dd,number, dateadd(dd,-day(getdate())+1,getdate())),120) AS every_time, --日期

datepart(weekday, dateadd(dd,number, dateadd(dd,-day(getdate())+1,getdate())) ) AS Weekd --星期几

FROM

master..spt_values n

WHERE

n.type = 'p'

and number<datediff(day,dateadd(dd,-day(getdate())+1,getdate()),dateadd(mm,+1,dateadd(dd,-day(getdate())+1,getdate())))









select convert(varchar(7),dateadd(mm,t.number,dateadd(year, datediff(year, 0, getdate()), 0)),120) 月份

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