Search for part of record

All,

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’
jose11auAsked:
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.

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

Revised...
0
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  

SELECT * 
FROM   sales
WHERE  SUBSTRING( Sales_Id , 
                  CHARINDEX ( '-' ,Sales_Id  , 1 ) + 1, 
                  CHARINDEX ( '-' ,Sales_Id , CHARINDEX ( '-' ,Sales_Id  , 1 ) + 1 )  - CHARINDEX ( '-' ,Sales_Id  , 1 ) - 1 ) = ‘4562’

Open in new window

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

SELECT * 
FROM  sales
WHERE STUFF( LEFT(sales_id , CHARINDEX('-', sales_id  , CHARINDEX('-', sales_id ) + 1) - 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.
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.

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 )

RTUBFGH-4562-10-896001455-AUST-7895663
4562FGH-1234-10-896001455-AUST-7895663
RTUBFGH-1234-10-896004562-AUST-7895663
RTUBFGH-4562-10-896001455-AUST-7894562
1
jose11auAuthor Commented:
Thank you so much. This is great!
0
Mike EghtebasDatabase and Application DeveloperCommented:
@Anoo,

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

0
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
RTUBFGH-10-10-896001455-AUST-7895663

OR If a big number 896001455 is a permitted in place of 4562, then sales_id would be
RTUBFGH-896001455-10-896001455-AUST-7895663

OR even If 4562 is a valid value in place of AUST, then sales_id would be
RTUBFGH-4562-10-896001455-4562-7895663

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

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.