Need to update and replace first zero in a field using a where

I have a field that I need to replace the first zero. So if the field is 0381 I need it to be S381 but if its 00381 I don't want it changed.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Guy Hengel [angelIII / a3]Billing EngineerCommented:
ms sql server syntax:
UPDATE yourtable
  SET yourfield = 'S' + substring(yourfield, 2, 100)
WHERE yourfield LIKE '0%'
  AND yourfield NOT LIKE '00%'

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Aneesh RetnakaranDatabase AdministratorCommented:
SET i  = 'S'+ RIGHT(i, LEN(i) - 1 )
AND LEFT(i,2) <> '00'
try this

UPDATE table SET field='S' + MID(field,2) WHERE LEFT(field,1)='0' AND LEFT(field,2)<>'00'
Exploring SQL Server 2016: Fundamentals

Learn the fundamentals of Microsoft SQL Server, a relational database management system that stores and retrieves data when requested by other software applications.

first make sure the field can accept characters. varchar if in oracle or text if in microsoft.

second develop a habit of backing up your db before making changes.

third here you go

update [table name]

set field name = 'S' & field name

where field name like '0%';

maybe + and maybe & . sorry have not wrote sql in many platforms since sometime. :)

waiting for your reply
ooooops. i hope yu have not executed it yet.


and field name not like '%00';

waiting for your reply
Jinesh KamdarCommented:
For Oracle, use this modified version of Angel's query.

UPDATE table_name
SET field_name = 'S' || SUBSTR(field_name, 2)
WHERE field_name LIKE '0%'
AND field_name NOT LIKE '00%'
Forced accept.

EE Admin
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Query Syntax

From novice to tech pro — start learning today.