7

I'm looking to take a specified string and query a table where a concat of 2 fields is equal to the string.

set @fab = "36013-601301-11";
set @job = substring_index(@fab, '-', 1);
set @fabnumba = trim(leading LEFT(@fab,char_length(@job)+1) from @fab);

select * from (select JobNumber, concat(JobNumber, '-', LotNumber) as bomfab from qiw_powerbi) base
where bomfab LIKE concat(@job,"-", @fabnumba)

If I try the following it fails:

WHERE bombfab LIKE "36013-601301-11"

However, this attempt works:

WHERE bombfab LIKE "36013-%601301-11"

How can I concat() with the variables @job and @fabnumba to do this?

2
  • Edit your post and show some sample (not live values) of the data you are trying to retrieve out to clarify the context of your query. Use spaces and not tabs when posting sample data, structures, etc. Commented Oct 18, 2016 at 17:37
  • Updated with an example of what works and an example of what doesn't, to hopefully clarify better. Commented Oct 18, 2016 at 17:45

1 Answer 1

6

Are you sure that the LotNumber values from qiw_powerbi are what you are expecting? They don't have any leading spaces?

What happens if you try adding a TRIM function to LotNumber:

select * from (select JobNumber, concat(JobNumber, '-', TRIM(LotNumber)) as bomfab from qiw_powerbi) base
where bomfab LIKE concat(@job,"-", @fabnumba)
Sign up to request clarification or add additional context in comments.

4 Comments

Aha! With the SQL front-end I'm using it strips out the leading blank characters, so I forgot all about that. Thank you very much for the reminder!
Did that solve the problem? Were there leading blanks?
I could actually use:
I can now use WHERE bomfab LIKE @fab with the TRIM. Thanks again!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.