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
 right justify leading zeros

Author  Topic 

Vack
Aged Yak Warrior

530 Posts

Posted - 2010-09-14 : 11:32:53
I have a table that has a field that stores excel file names.

957.xls
10049.xls


what I want to do is strip the .xls and add leading zeros to the first portion so that it is a size of 12 and update a 2nd field in the same table.

000000000957
000000010049

Table name is xlscustomer
Field that contains 957.xls is field_name
Field I want to update is cus_no


X002548
Not Just a Number

15586 Posts

Posted - 2010-09-14 : 11:42:08
[code]
DECLARE @fn varchar(50); SET @fn = '10049.xls'
SELECT RIGHT(REPLICATE('0',12) + SUBSTRING(@fn,1,CHARINDEX('.',@fn)-1),12)

[/code]


Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam



Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-09-14 : 11:43:54
update your_table
set cus_no = right('000000000000' + replace(field_name,'.xls',''),12)
where cus_no is null --condition should avoid from updating rows where the column is already filled


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-09-14 : 11:44:36

again


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2010-09-14 : 13:07:37
I like the REPLACE though..wonder if there is a perf diff between REPLACE vs. SUBSTRING & CHARINDEX

Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam



Go to Top of Page
   

- Advertisement -