SQLServer 获取时间日期,年月日,获取当月天数,获取当月日期
一、当月的天数(2019-12)
select day(dateadd(month,1,(SELECT LEFT (CONVERT(varchar(12),DATEADD(MONTH,0,GETDATE()),112) ,6))+'01')-1) count
二、当月日期(2019-12-01,2019-12-02…)
select convert(varchar(10),dateadd(DAY,t2.number,t1.day),120) day from
(select (SELECT LEFT (CONVERT(varchar(12),DATEADD(MONTH,0,GETDATE()),23) ,7)) +'-01' day) t1,
(select number from MASTER..spt_values WHERE TYPE='P' AND number>=0 and number<=31) t2
where convert(varchar(10),dateadd(DAY,t2.number,t1.day),120) like (SELECT LEFT (CONVERT(varchar(12),DATEADD(MONTH,0,GETDATE()),23) ,7))+'%'
三、年月日
SELECT LEFT (CONVERT(varchar(12),DATEADD(MONTH,0,GETDATE()),112) ,6) month
SELECT CONVERT(varchar(12),DATEADD(MONTH,0,GETDATE()),112) day
SELECT LEFT (CONVERT(varchar(12),DATEADD(MONTH,0,GETDATE()),23) ,7)month
SELECT CONVERT(varchar(12),DATEADD(MONTH,0,GETDATE()),23) day
扩展近三个月的日期
select convert(varchar(10),DATEADD(day,number,CONVERT(datetime,((SELECT LEFT (CONVERT(varchar(12),DATEADD(MONTH,-2,GETDATE()),23) ,7))+
'-1'))) ,120) AS kqrq
from master.dbo.spt_values
where type='p' AND number<=DATEDIFF(day,CONVERT(datetime,((SELECT LEFT (CONVERT(varchar(12),DATEADD(MONTH,-2,GETDATE()),23) ,7))+'-1')),GETDATE())