SQL Update Query

I have a table that has a column of store numbers.

Example of Store Numbers: 1602, 1603, 1604, 1605 ...

I want to add a 0 in front of all the store numbers without having to modify each store number separately.

After the update, they would read as: 01602, 01603, 01604 ...

Thanks!!!
LVL 1
isamesAsked:
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.

Brian CroweDatabase AdministratorCommented:
What is the datatype of the StoreNumber field?  I will assume it is a string field of some kind because otherwise the answer is "you can't"

UPDATE myTable
SET StoreNumber = '0' + StoreNumber
0

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
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
>I want to add a 0 in front of all the store numbers without having to modify each store number separately
Okay, but numeric data types do not support leading zeros, so there's two ways to pull this off.
Run an ALTER TABLE statement converting the Store Number column to a varchar, then run an UPDATE statement to add the leading zero.
(Preferred) Forget about doing this in the database and handle it in the reporting / ETL layers.
0
lcohanDatabase AnalystCommented:
"I have a table that has a column of store numbers."

If the column data type is any of int/bigint/smallint you can't do that without changeing it to a varchar daratype however....do NOT do that on production databases as it may break your code.

If you can change that datatypw to nvarchar of something you can do a


UPDATE tabname SET yourColname = '0'+yourColname;
0
isamesAuthor Commented:
Say you wanted to remove the leading 0.

Would you do a -
0
Brian CroweDatabase AdministratorCommented:
The easiest thing to do in your case would be to CAST the value to an INT since all of your values so far as I can tell are numeric.  Otherwise you could use SUBSTRING.

SELECT SUBSTRING(StoreNumber, 2, LEN(StoreNumber)) AS StoreNumberWithoutLeadingZero
FROM myTable
0
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
Microsoft SQL Server 2008

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.