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)
 Adding Zeros to String

Author  Topic 

Brittney10
Posting Yak Master

154 Posts

Posted - 2013-04-01 : 18:24:13
I need to add zeros to the front of two words, or numbers, that are before or after an asterisk depending on the length of each string . The digits before the asterisk should have a total length of 5 and the digits after the asterisk should have a total count of 6. To makeup the appropriate 5 or 6 length when digits/numbers are missing, a zero should be appended to the front of the string. The asterisk should also be completely stripped from the string in the final output.

For example:

1234*12345 would become 01234012345.

123*1234 would become 00123001234.

Thanks for the help in advance.

James K
Master Smack Fu Yak Hacker

3873 Posts

Posted - 2013-04-01 : 21:56:51
[code] RIGHT('00000'+LEFT(YourColumn,CHARINDEX('*',YourColumn)-1),5) +
+RIGHT('00000'+STUFF(YourColumn,1,CHARINDEX('*',YourColumn),''),5)[/code]
Go to Top of Page
   

- Advertisement -