Vb.Net - SQL Query Accounting for Leading Zeroes

Good Day Experts!

I have quite the quandary with my SQL Query. It "feels" like there should be a solution but I am not sure how to get there. I am looking for duplicate [Pro Number]'s across [Processing Table]'s.  My current query accounts for exact match [Pro Number]'s.  I have just been informed that I must account for potential leading zeroes in the target and checking [Processing Table].  

So, when I compare (A2.[Pro Number] = A1.[Pro Number]) how can I also say to first strip off the leading zeroes on both sides and compare?  Reason I say both sides is I will not know ahead of time which side has leading zeroes.

I know this sounds a bit confusing, please ask questions so I explain better.


Here is my query:

      A1.[Pro Number]
      [CUS0101].dbo.[Processing Table] A1 with (nolock)
      A1.[Process Week] = '8/21/2015'
      And Exists (Select 1 From [BOR0102].dbo.[Processing Table] A2 with (nolock)  
                Where (A2.[Pro Number] = A1.[Pro Number])
                  And A2.[Pro Code] = A1.[Pro Code]  
                And A2.[Pro Occurrence] = A1.[Pro Occurrence]  
                And A2.[BOL Number] = A1.[BOL Number]  
                And A2.[Sales Order Number] = A1.[Sales Order Number])
Who is Participating?

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

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.

Lee SavidgeCommented:
I assume [Pro Number] is not an integer field? You could just cast it.

Where (cast(cast(a2.[pro number] as int) as nvarchar(100) = cast(cast(a1.[pro number] as int) as nvarchar(100))

Open in new window

I am assuming that [pro number] is an nvarchar(100). Change that datatype to what you need. Would that work?
Jimbo99999Author Commented:

Thanks for responding...it is varchar(20).  

Will that work for this [Pro Number] value --> 0000046F66162

I am not very good at all with casting so I appreciate the assistance.

Lee SavidgeCommented:

You could do a cheeky replace.

Where (replace(ltrim(replace(a2.[pro number], '0', ' ')), ' ', '0') = replace(ltrim(replace(a1.[pro number], '0', ' ')), ' ', '0'))

Open in new window

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
PortletPaulEE Topic AdvisorCommented:
Instead of attempting to strip leading zeros you try the reverse approach e.g.

 right('00000000000000000000' + [this_column]),20)
 right('00000000000000000000' + [that_column]),20)
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
Visual Basic.NET

From novice to tech pro — start learning today.