sql中日期函数
发布网友
发布时间:2022-04-24 01:26
我来回答
共1个回答
热心网友
时间:2022-04-08 22:15
这样就行了,注意你cast成nvarchar类型,那么‘年’这种字符串前一定要加N,:
declare @Year int
declare @Month int
select @Year =2010
select @Month =12
select
case when @Month = 12 then cast(@Year+1 as nvarchar)+N'年'+cast(@Month-11 as nvarchar)+N'月01'
else
cast(@Year as nvarchar)+N'年'+cast(@Month+1 as nvarchar)+N'月01' end+N'至'+
case when @Month = 12 then cast(@Year+1 as nvarchar)+N'年'+cast(@Month-11 as nvarchar)+N'月30'
else
cast(@Year as nvarchar)+N'年'+cast( @Month+1 as nvarchar)+N'月30' end
我也不知道你到底想要什么东西,如果用datediff的话得到的就是一个天数,也没有必要加上年月日了:
declare @Year int
declare @Month int
select @Year =2010
select @Month =4
select
datediff
(day,
case when @Month = 12 then cast(@Year+1 as nvarchar)+' '+cast(@Month-11 as nvarchar)+' 01'
else
cast(@Year as nvarchar)+' '+cast(@Month+1 as nvarchar)+' 01' end,
cast(@Year as nvarchar)+' '+cast( @Month as nvarchar)+' 01' )