Avatar of kdeutsch
kdeutsch
Flag for United States of America asked on

Replace characters with others in SQL

I am trying to replace the first character which is a zero with the letter O.  I have thousands of records I need to do this on.  1st is what they look like in DB and second is what I need them to look like.
01    need to look like  O1
02     O2
03     O3
04     O4
05     O5
06     O6
07     O7
08     O8

The field name is strGrade.  how can I do an replace and update.
thanks
Microsoft SQL Server 2005SQL

Avatar of undefined
Last Comment
kdeutsch

8/22/2022 - Mon
SOLUTION
chapmandew

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Lee

Hi,

Use the replace function:

declare @myoutput varchar(2)

select myInputCol = replace(myInputCol, '0', 'O') from myTable

Regards,

Lee
ASKER CERTIFIED SOLUTION
Lee

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
SOLUTION
CCongdon

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
CCongdon

My code sample ONLY replaces the first character with an 'O' if it is a zero, and no other zeros in the string.

lsavidge: Your code replaces ALL zeroes in the string.
chapmandew: Your code replaces the first character of every field, whether or not it is a zero.
Lee

Hi,

I know what my code does. It is what was asked for given the information above. My plan was to keep it simple.

Regards,

Lee
Your help has saved me hundreds of hours of internet surfing.
fblack61
chapmandew

CCongdon, The author asked to replace the first letter...which  happened to be 0.
CCongdon

And that's fine. If that is really what he needs, then by all rights, you deserve the points.

However, I'm sure you both are experienced enough around here to find that sometimes what was originally posted isn't what the author really wanted. I should apologize however, my comments shouldn't have been directed AT you two, but more at your code as reference for kdeutsch, so that the questioner knows what the difference in the 3 code samples is.
chapmandew

No worries...I've answered a lot of questions...I've also commented on a LOT of questions where I thought I knew what the asker was asking, but they ended up asking something entirely different.  :)
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
CCongdon

Heh, been there done that. Thought for dang sure I knew what it was supposed to be...and woops...missed a word (or two) in the description! Defintintely a couple of questions where I walked away without making any more comments because I felt I had egg all over my face.

kdeutsch

ASKER
Ok,
My original post is what I wanted, unfortunely I did not give all info. I have other data in there that is like
E5
E6
E7
The 1st post by Chapmandew replaced these but I did a select before an update to make sure data was good.
The second one by lsavidge works just fine.
But I like the 3rd one the best by CCongdon because I can use it for other fields in my table.
Because I used Isavidges to do my table, I will award most points here but feel obligated to add points and share with others becuase they are all correct.
kdeutsch

ASKER
I based points off of whom gave best solution first.
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck