Search for part of record


I need your assistance with a query.

I have a simple query that shows me all records for a specific sales id, however, some changes have been made to the sales_id. The sales_id used to be something like this 4562, now it has change to something RTUBFGH-4562-10-896001455-AUST-7895663. The sales_id is the number between the first and second dash 4562. The first part of the sales_id is not always RTUBFGH, it changes according to requirements.

How can I modify the below query so it show me the records for sales_id 4562?

Select * from sales
Where sales_id = ‘4562’
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.

Mike EghtebasDatabase and Application DeveloperCommented:
Select * from sales
Where sales_id like '%4562%’

Anoo S PillaiCommented:
SUBSTRING function would be useful for this, See details of SUBSTRING at the link - SUBSTRING

Parameters to be passed to SUBSTRING function for sales_Id

1, Expression -  in this case is obviously column sales_id.
2. Start  - Staring position of first '-' . This can be found out using CHARINDEX function , Details of CHARINDEX is available at CHARINDEX.
3. Length - length of characters between first '-' amd second '-' . This can be found out using CHARINDEX function.

A working code part to find out the sales_id  -

select substring ( Sales_Id , /*Souce Column */  
                           CHARINDEX ( '-' ,Sales_Id  , 1 ) + 1  , /*Start position of the sales_id */
                           CHARINDEX ( '-' ,Sales_Id , CHARINDEX ( '-' ,Sales_Id  , 1 ) + 1 )  - CHARINDEX ( '-' ,Sales_Id  , 1 ) - 1 )  /* End Position of sales_Id*/

This string extrached from your salesid should be compared with 4562, something like the following  

FROM   sales
                  CHARINDEX ( '-' ,Sales_Id  , 1 ) + 1, 
                  CHARINDEX ( '-' ,Sales_Id , CHARINDEX ( '-' ,Sales_Id  , 1 ) + 1 )  - CHARINDEX ( '-' ,Sales_Id  , 1 ) - 1 ) = ‘4562’

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
Anoo S PillaiCommented:
Another method using LEFT and STUFF

FROM  sales
WHERE STUFF( LEFT(sales_id , CHARINDEX('-', sales_id  , CHARINDEX('-', sales_id ) + 1) - 1),
              CHARINDEX('-', sales_id ) , 
	      '') = '4562' 

Open in new window

The logic is to extract characters till second '-' from the LEFT and replace characters till first '-' ( including first '-') with a blank character ( '' )  . This may work faster than the previous one if your table is large.
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

Anoo S PillaiCommented:
@eghtebas -
"Select * from sales Where sales_id like '%4562%’ " could have a slight problem in this case, It doesn't select specifically for characters between first and second '-' . It would select the rows even if the string 4562 comes anywhere else.

Say for example, all the following rows would be selected if like '%4562%’  is used.  ( I think that is no the intended behavior )

jose11auAuthor Commented:
Thank you so much. This is great!
Mike EghtebasDatabase and Application DeveloperCommented:

Thank you for good explanation. In that case, the following may be a simpler solution:
Select * from sales
Where sales_id like '%-4562-%’

Open in new window

Anoo S PillaiCommented:
@eghtebas - Yours is a pretty simple solution and  may perfectly fit in this case.

But, unless and otherwise we know the string pattern of sales_id column, the validity of clause " sales_id like '%-4562-%’ " cannot be verified.  I assume that the four digit value 4562 is given for an illustration.

If the two digit value 10 is a permitted in place of 4562, then sales_id would be

OR If a big number 896001455 is a permitted in place of 4562, then sales_id would be

OR even If 4562 is a valid value in place of AUST, then sales_id would be

Am I thinking too pessimistic because of my experience as a DBA ?
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 2008

From novice to tech pro — start learning today.