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!!!
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
"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;
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;
ASKER
Say you wanted to remove the leading 0.
Would you do a -
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 StoreNumberWithoutLeadingZ ero
FROM myTable
SELECT SUBSTRING(StoreNumber, 2, LEN(StoreNumber)) AS StoreNumberWithoutLeadingZ
FROM myTable
Okay, but numeric data types do not support leading zeros, so there's two ways to pull this off.