Remove leading zeros in SQL task

Hi all.

We have a table (myTable) that has a field (PO_Number) that may have leading zeros.

For example:

PO_Number:
00123
04560
000ABC

I want to know if there is a way to remove any leading zeros, the number of leading zeros may vary, some may not have any leading zeros.

I will use this in a SQL DTS.

Thank you in advance!
printmediaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

bchoorCommented:
REPLACE(LTRIM(REPLACE([PO_Number], '0', ' ')), ' ', '0')

1. It replaces all 0s with spaces
2. Trims the leading spaces using LTRIM
3. replaces the spaces with 0s (in case there were any other zeros in the original text that were not leading)

HTH
BC
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Lee SavidgeCommented:
Try this.

Lee
declare @x nvarchar(100)

--select @x = '87980123'
--select @x = '0087980123'
--select @x = '000087980123'
--select @x = '000087980123'
--select @x = 'asdasd87980123'
--select @x = '000asdasda87980123'
--select @x = 'adsa000adsd87980123'
select @x = 'sdadsa87980123'


while left(@x, 1) = '0'
begin
    select @x = right(@x, len(@x) - 1)
end

select @x

Open in new window

0
Lee SavidgeCommented:
Nice trick BC. Like it.

Lee
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Anthony PerkinsCommented:
>>I will use this in a SQL DTS.<<
Using T-SQL or using an ActiveX Script Task?
0
printmediaAuthor Commented:
Thanks BC, I will give it a try and let you know.

ACperkins: I'll be using T-SQL
0
printmediaAuthor Commented:
Worked great. Thanks!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.