Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Appending leading 0 to existing varchar

Author  Topic 

meef
Posting Yak Master

113 Posts

Posted - 2014-06-12 : 12:22:16
I have a table with a field called "pro_nums" and needs to have a leading zero appended to it. The field is a varchar datatype and I tried doing an update that simply said '0' + pro_num but that didn't work. What do I need to do to append this leading zero?

James K
Master Smack Fu Yak Hacker

3873 Posts

Posted - 2014-06-12 : 12:31:17
quote:
Originally posted by meef

I have a table with a field called "pro_nums" and needs to have a leading zero appended to it. The field is a varchar datatype and I tried doing an update that simply said '0' + pro_num but that didn't work. What do I need to do to append this leading zero?

That should work unless that would cause the length to exceed the max length of the column. What is the query you used?
SELECT '0'+pro_num FROM YourTable; -- if you want to query
UPDATE YourTable SET pro_num = '0'+pro_num; -- if you want to update the table
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2014-06-12 : 12:35:33
quote:
Originally posted by meef

but that didn't work.



Did it error? If so, please provide the error. If it didn't error, then show us what it did.

Tara Kizer
SQL Server MVP since 2007
http://weblogs.sqlteam.com/tarad/
Go to Top of Page

meef
Posting Yak Master

113 Posts

Posted - 2014-06-12 : 15:51:32
Actually what I posted did work... for some reason I used an actual value instead of the column name in the query, but here I posted the way I wanted to do it in my head. In other words I did '0' + '456789'
instead of just labeling it the column name. I guess I wanted to only use one pro number to test with, but it came back and said 1 row affected despite not having done anything that I could detect.

All is well now though as it worked the way I originally posted. Thanks.
Go to Top of Page
   

- Advertisement -