Link to home
Start Free TrialLog in
Avatar of isames
isames

asked on

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!!!
ASKER CERTIFIED SOLUTION
Avatar of Brian Crowe
Brian Crowe
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>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.
"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;
Avatar of isames
isames

ASKER

Say you wanted to remove the leading 0.

Would you do a -
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