Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 617543
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T18:26:50+00:00 2026-05-13T18:26:50+00:00

I have a situation where I have an incoming data value that may or

  • 0

I have a situation where I have an incoming data value that may or may not have leading zeroes. I need to match that to a field/row in a SQL Server table. The field value in the SQL Server database may or may not have leading zeroes as well.

So, I might have:

  • incoming = 5042800138
    and the value in db can be any of 5042800138, 05042800138, 005042800138, 0005042800138

  • or the incoming might be 005042800138
    and the value in db can be any of 5042800138, 05042800138, 005042800138, 0005042800138

The solution I came up with was to strip off the leading zeroes (always) on the incoming data and use SQL like the following example:

-- this simulates the incoming value to check
-- i strip out the leading zeroes.
declare @tryUPC as varchar(40)
set @tryUPC = '5042800138'

-- try to find it in the database and ignore leading zeroes
select prod_uid, prod_partno, prod_upc
from products as p
where (prod_upc = @tryUPC) or 
   (
   len(prod_upc) > len(@tryUPC)
   and right(prod_upc, len(@tryUPC)) = @tryUPC
   and stuff(prod_upc, 1, len(prod_upc) - len(@tryUPC), '0') = prod_upc
   )

This seems to work. My question is, am I missing something? Does SQL Server have a better way of dealing with this? I am using SQL Server 2005.

tia,

don

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T18:26:50+00:00Added an answer on May 13, 2026 at 6:26 pm

    If you aren’t able to change existing data to strip the leading zeroes / convert to INT, it might be faster to just do something like so:

    WHERE prod_upc IN (@tryUPC, '0' + @tryUPC, '00' + @tryUPC, '000' + @tryUPC [...])
    

    That’s about as elegant as my foot, but it would be more static & legible, and (probably) more likely to get at any relevant index.

    That’s assuming there’s a finite limit on how many leading zeroes you have, mind. Converting the data to INT (or adding a new INT column and calculating it on insert) would probably be the best fix for the problem.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have situation, where running a query that filters by an indexed column in
I have situation where I need to change the order of the columns/adding new
I have situation where a user can manipulate a large set of data (presented
We have situation where say we have four engineers that are working on software
I have the following situation: There is a windows folder that has been mounted
A brief synopsis of the situation: I have a service that takes information in
I have a C program in my firestation that captures incoming packets to the
I have a client software (on Android) that listens to incoming messages. The messages
I need to handle a lot of incoming XMLs which may be rather complex.
I have an ACE reactor that accepts socket connections and listens for the incoming

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.