Click here to Skip to main content
15,886,757 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
from below string i want to remove He replace with other character .suppose ABC
He is a positive influence on other students

I am using replace function in sql server.
replace(He is a positive influence on other students, 'He','ABC')
I got one problem
see Output
ABC is a positive influence on oABCr students

but i want to make first character to replace

please help..
thanks in advance.
Posted

SQL also has SUBSTRING[^] so:
SQL
SELECT 'ABC' + SUBSTRING(myColumn, 3, LEN(MyColumn)) FROM MyTable
Will remove the first two characters and tack on ABC

[edit]Missed a close bracket[/edit]
 
Share this answer
 
v2
Comments
Amol Jadhao 5-Mar-14 6:44am    
this solution doesn't work out...because,
if string don't having "HE" character then what happen.
i want to find first character in string .If it is "HE" then only replace.
OriginalGriff 5-Mar-14 6:54am    
So try:
SELECT CASE WHEN DATA LIKE 'He%'
THEN 'ABC' + SUBSTRING(myColumn, 3, LEN(MyColumn))
ELSE myColumn
END
FROM MyTable
Try the below script

SQL
declare @str     varchar(1000),
  @word    varchar(100),
  @newword  varchar(100)

select  @str   = 'He is a positive influence on other students',
  @word  = 'He',
  @newword  = 'ABC'

select  @str Old, stuff(@str, charindex(@word, @str), len(@word), @newword) new
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900