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
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Update field to include leading zero

Author  Topic 

evanburen
Posting Yak Master

167 Posts

Posted - 2011-01-10 : 10:09:25
I have some values in an int column which when imported from another data source is dropping the leading zero. Every value should contain 16 digits. For example, 770070040800000 should be 0770070040800000. so far I've used

If LEN(columnname) = 15

to identify the bad values but I don't know how to udpate them to include the leading zero.

Thanks

jimf
Master Smack Fu Yak Hacker

2875 Posts

Posted - 2011-01-10 : 10:17:10
SELECT REPLICATE('0',16 - len(columname) + columnname

Jim

Everyday I learn something that somebody else already knew
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-01-10 : 11:11:57
please keep in mind that converting to varchar would make integer manipulations difficult.

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2011-01-10 : 12:28:16
quote:
Originally posted by evanburen

I have some values in an int column which when imported from another data source is dropping the leading zero. Every value should contain 16 digits. For example, 770070040800000 should be 0770070040800000. so far I've used


Int values do not have leading zeros. So, you can either
1. Change the data type to a non-numeric type (like varchar) (bad idea)
2. Add a computed column.
3. Keep the int column and just convert it to a string when you select out or let the front end handle/convert it.
Go to Top of Page
   

- Advertisement -