MS sqlserver 获取某月某年的天数

 1 --定义传入时间

 2 declare @datetime varchar(10)

 3 set @datetime='2012-02-01'

 4 --定义月的天数

 5 declare @dayCountM int 

 6 --定义年的天数

 7 declare @dayCountY int

 8 select @dayCountY=datediff(day,(cast ((datepart(year,dateadd(year,-1,@datetime))) as varchar)+'-12-31'),((cast (datepart(year ,@datetime) as varchar))+'-12-31'))

 9 select @dayCountM=datediff(day,(cast(datepart(year,@datetime) as varchar)+'-'+cast(datepart(month,@datetime) as varchar)+'-1'),(cast(datepart(year,dateadd(month,1,@datetime)) as varchar)+'-'+cast(datepart(month,dateadd(month,1,@datetime)) as varchar)+'-1'))

10 

11 select @dayCountM as dayCountM,@dayCountY as dayCountY

 

你可能感兴趣的:(sqlserver)