我需要帮助在字符串中插入字符,例如:
031613 05:39 AM
输出应该是:
03/16/13 05:39 AM
您可以使用STUFF
DECLARE @String NVARCHAR(20) = '031613 05:39 AM'
SELECT STUFF(STUFF(@String,3,0,'/'),6,0,'/')
使用SUBSTRING怎么样?
DECLARE @String VARCHAR(50) = '031613 05:39 AM'
SELECT @String,
SUBSTRING(@String,1,2) + '/' + SUBSTRING(@String,3,2) + '/' + SUBSTRING(@String,3,2) + SUBSTRING(@String,7,LEN(@String)-6)
使用也可以这样做..但相当大的解决方案。
declare @T varchar(100)= '031613 05:39 AM'
declare @result varchar(100) =''
;with CTE
as
(
Select 0 as startIndex, 2 as endIndex
union all
select convert(int, endIndex) as startIndex , endIndex+2 as endIndex from CTe where endIndex < charindex(' ',@T)
)
select top 3 @result += substring(@T,startIndex+1, endIndex-startIndex)+'/' from CTe
select substring( @result,0,len(@result))