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
In SQL Server, you can use the T-SQL STUFF() function to insert a string into another string. This enables you to do things like insert a word at a specific position. It also allows you to replace a word at a specific position. character_expression is the original string.
The STUFF function inserts a string into another string.
The BETWEEN operator is used in the WHERE conditions to filter records within the specified range. The range of values can be strings, numbers, or dates.
You can use STUFF
DECLARE @String NVARCHAR(20) = '031613 05:39 AM'
SELECT STUFF(STUFF(@String,3,0,'/'),6,0,'/')
Fiddle
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With