Sharktooth Sharktooth - 5 months ago 12
SQL Question

How does % operator in AND evaluate

Found this in the AND condition of LEFT OUTER JOIN between AA & DD

AND DD.EMPL_ID LIKE '%' + AA.[Emp_Id]


Can anyone explain what this is evaluating and how?

Answer

The LIKE operator does a string comparison but treats % and _ as wildcards matching any string and any single character, respectively. This is analoguous to * and ? in the shell.

DD.EMPL_ID LIKE '%' + AA.[Emp_Id]

looks like your DBM uses + for string concatenation (standard SQL would be ||). So if AA.[Emp_id] is 'hello', then that condition becomes

DD.EMPL_ID LIKE '%' + 'hello'

which is

DD.EMPL_ID LIKE '%hello'

which checks whether DD.EMPL_ID is an arbitrary string followed by hello, i.e. whether DD.EMPL_ID ends with hello.

Comments