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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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

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

Lee SavidgeCommented:
Nice trick BC. Like it.

Lee
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

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

ACperkins: I'll be using T-SQL
printmediaAuthor Commented:
Worked great. Thanks!
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.