Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to insert character in between a string in SQL Server 2008?

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

like image 957
ajdeguzman Avatar asked Jun 19 '13 06:06

ajdeguzman


People also ask

How do I add a character to a string in SQL Server?

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.

Which function is used to insert string into another string in SQL Server?

The STUFF function inserts a string into another string.

Can Between be used for strings in SQL?

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.


2 Answers

You can use STUFF

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

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

Fiddle

like image 88
Nenad Zivkovic Avatar answered Oct 18 '22 02:10

Nenad Zivkovic


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

like image 36
Adriaan Stander Avatar answered Oct 18 '22 02:10

Adriaan Stander