I am trying to check for a value in my named query and that value is an integer parameter.
The parameter is called partNo
The name query is where I am checking for the word SN and the parameter partNo integer value.
As an example, the serial number is "SN 1527"
SELECT * FROM orderData
WHERE serialNo Like '%SN% :partNo'
I am not able to find the correct syntax for this and I tried so many different syntaxes but none f them worked so far. I would appreciate your help in this regard.
I think the issue is the query itself? Best case scenario, you query would be
SELECT * FROM orderData
WHERE serialNo Like '%SN% 1527'
which I’m not sure makes sense. Or maybe I am not well versed enough on the LIKE operator, but normally a % is used in the beginning or the end to allow any wildcard characters before and after, so to then put a hardcoded value after that seems counter to allowing wildcard. But maybe this is valid.
Does this SELECT * FROM orderData WHERE serialNo Like '%SN% 1527' query work correctly if you run it directly?
Thank you very much!
The parameter is dynamic so it will not be always 1527 and the operator change that in a vision view.
I even tried ‘%SN% 1527’ but it did not return any rows.
Right! concat should work!
Now I do not get any errors but I still cannot see any rows returned.
So the complete example is:
serialNo: SN 1527 Pallet20
I still need to have % for pallet20, right?
SELECT * FROM orderData
WHERE serialNo Like concat('%SN%', :partNo, '%')
The % character used in this manner is valid. You are saying return any value begining with any combination of characters followed by SN followed by any combination of characters and ending with 1527.
That should work so long as you're only concerned with the partNo matching.
Using LIKE is not always the best practice for queries. I would make your db serial column hold only the integer and not the SN part. Then you would have another column named pallet. This will make your life much easier and your queries more efficient and accurate. Then if you want to display it as SN 1527 Pallet20 you would use
select concat('SN ', serial, ' Pallet ', pallet) as serialNo
from orderData
where serial = :partNo