Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Including local variable in concat string in MySQL

Tags:

sql

mysql

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?

like image 479
El Tigre Avatar asked Oct 18 '16 17:10

El Tigre


People also ask

Does concat work in MySQL?

CONCAT() function in MySQL is used to concatenating the given arguments. It may have one or more arguments. If all arguments are nonbinary strings, the result is a nonbinary string. If the arguments include any binary strings, the result is a binary string.

How concatenate MySQL in SQL?

MySQL CONCAT() Function The CONCAT() function adds two or more expressions together. Note: Also look at the CONCAT_WS() function.


1 Answers

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)
like image 173
CLAbeel Avatar answered Sep 25 '22 14:09

CLAbeel