How to replace character in SQL

I want to replace a specific character at position 4 in sql Server, I know about a replacement or case when, but my problem is that I just want to replace the fourth character,

I'm trying like

SELECT REPLACE(_NAME,0,1) AS exp FROM _EMPLOYEE 

but he will not check the 4th character

for example, if _name contains IMR002001 , then it should be IMR012001

+5
source share
1 answer

Use stuff() :

 select stuff(_NAME, 4, 1, '@') 

This replaces the substring, starting at position 4 of length 1, with the string, which is the fourth argument. A string can be longer or shorter than the replaced string.

In your example:

 select stuff(_NAME, 4, 1, '1') 
+7
source

Source: https://habr.com/ru/post/1205773/


All Articles