I have an example table with three fields.
TableA FieldA FieldB FieldC
FieldA
has a fixed length of 9. I have not designed this table, and some applications already use it.
I want to select FieldB
and FieldC
with conditions against FieldA
.
Using this sql statement:
SELECT FieldB, FieldC FROM TableA WHERE FieldA LIKE Concat(@paramA, '%', @paramB)
I can not achieve the desired result. When I try to search with paramA 12
and paramB ''
, I get 2
results:
FieldA FieldB FieldC
because, obviously, it corresponds to 12%
, and this is not what I want. I want the parameters to match the correct row index.
If I look for paramA = '12'
and paramB = ''
, then it should not have a result. To get the fields ( FieldB
, FieldC
), I need the correct values paramA = '123'
and paramB = '456'
so that it returns XYZ
and John
. If I want to return James
, then I have to give paramA = '456'
and paramB = '345'
How can I build the SQL statement correctly for this? Any ideas? Thanks.
source share