15

I need help in inserting a character inside a string, e.g.:

031613 05:39 AM

The output should be:

03/16/13 05:39 AM

0

3 Answers 3

32

You can use STUFF

DECLARE @String NVARCHAR(20) = '031613 05:39 AM'

SELECT STUFF(STUFF(@String,3,0,'/'),6,0,'/')

Fiddle

Sign up to request clarification or add additional context in comments.

Comments

1

How about using 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)

SQLFiddle DEMO

1 Comment

Thanks!Can you also use STUFF T-SQL for that problem?
0

use can also do like this.. but quite big solution.

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))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.