When you enclose departmentname
in quotes, it becomes a character literal, and ceases to be a variable. Its value is ignored, and the literal '%departmentname%'
is used instead.
The simplest solution is to use CONCAT
to add '%'
as a prefix and a suffix, like this:
and DeptName LIKE CONCAT( '%', departmentname, '%')
If, for example, departmentname
is set to police
, the result becomes
and DeptName LIKE '%police%'