I have this sql:
select * from employees
where
lastname like '%smith%' and firstname like '%bob%' and middle like '%%'
The problem is the values of the name, particularly the middle name, can be null. So the statement above will find:
smith, bob mike
but will not find
smith, bob
because Bob’s middle name is null (he has no middle name.)
I have this which works but I thought there was a better way and wanted input:
select * from employees
where
lastname like '%smith%'
and firstname like '%bob%'
and (middle like '%%' or middle is null)
edit:
middle is not always blank. Sometimes it will have a value so I have to keep the test for middle name in. It could be populated or null.
Thanks. I am using Oracle 11g if that matters.
You can use nvl:
But this is just the same as not checking the middle name at all as like ‘%%’ will match anything other than null.