• Status: Solved
  • Priority: Low
  • Security: Public
  • Views: 74
  • Last Modified:

Matching Part of a Word in a SQL Query

Hi I am in a bit of a crunch on this SQL query here so I appreciate any help.

Lets say I have this part:

HON787800F015

I need to find all parts that match above up to the F but then have an R after. For example one of the results that would match would look like this:

select * from inv_master (nolock) where part_no like 'HON787800R%'

I appreciate any help you might be able to offer

Thank you much.
0
Kelly Martens
Asked:
Kelly Martens
1 Solution
 
Éric MoreauSenior .Net ConsultantCommented:
something like this would work:
DECLARE @input VARCHAR(20) = 'HON787800F015'
DECLARE @search VARCHAR(20)
DECLARE @pos INT = CHARINDEX('f', @input, 0)
IF @pos > 0 
BEGIN
	SET @search = SUBSTRING(@input, 1, @pos-1 ) + 'R%'
	select * from @inv_master  where part_no like @search
END
ELSE
BEGIN
	SELECT TOP 0 * FROM @inv_master
END

Open in new window

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

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Tackle projects and never again get stuck behind a technical roadblock.
Join Now