日期:2014-05-17 浏览次数:20486 次
declare @nd varchar(10) set @nd=2012 declare @yf varchar(10) set @yf=7 select (cast(@nd + '-'+@yf + '-01' as datetime)) as rq /* rq ----------------------- 2012-07-01 00:00:00.000 */ declare @nd1 varchar(10) set @nd1='2001~2021' declare @yf1 varchar(10) set @yf1='1~12' select convert(varchar(10),cast(left(@nd1,charindex('~',@nd1)-1)+ '-'+left(@yf1,charindex('~',@yf1)-1)+'-01' as datetime),120)+'~'+ convert(varchar(10),cast(right(@nd1,len(@nd1)-charindex('~',@nd1))+ '-'+right(@yf1,len(@yf1)-charindex('~',@yf1))+'-01' as datetime),120) as rq /* rq --------------------- 2001-01-01~2021-12-01 */
------解决方案--------------------
Select CONVERT(varchar(10), (cast(nd + '-'+yf + '-01' as datetime)), 120)as rq from tb
------解决方案--------------------
select cast(DATENAME (YEAR, getdate())+'-'+DATENAME (MONTH, getdate())+'-01' as datetime)
------解决方案--------------------
--理解有误,不好意思 --try declare @nd varchar(20),@yf varchar(6) set @nd='2001~2021' set @yf='1~12' select substring(@nd,1,charindex('~',@nd)-1)+'-1-1~'+convert(varchar,convert(int,substring(@nd,charindex('~',@nd)+1,4))-1)+'-12-01'