Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Integer Lowest Order

Status
Not open for further replies.

IAMINFO

MIS
Feb 21, 2002
62
US
Hello everyone ,I have a number 80505 how would I use sql to show the number as 00558, not really sure where to start. I need to apply this to multiple rows.

Thank you


 
Try
Code:
declare @t table (Number int)
insert into @t values ( 80505),(7845203)

;with cte as (select T.number, SUBSTRING(ltrim(T.number),N.number,1) as [Digit]
from @t T
inner join dbo.numbers N on N.Number between 1 and LEN(T.number))

select c.Number as OriginalNumber, (select '' + LTRIM(digit)
from cte c1 where c1.Number = c.Number order by Digit
for XML path('')) as NewNumber
from cte c
group by c.Number
order by c.Number

I'm using existing dbo.Numbers (or Tally) table, which is very easy to create on the fly also.

PluralSight Learning Library
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top