Oracle在比较字符串长度为零时包括包含NULL的行。

huangapple go评论53阅读模式
英文:

Oracle include NULL containing rows when compared string is zero length

问题

SELECT FIRSTNAME, LASTNAME FROM EMPLOYEES WHERE FIRSTNAME LIKE '%' || P_FIRSTNAME || '%' OR (P_FIRSTNAME = '' AND FIRSTNAME IS NULL);

英文:

I am building a function in Oracle where I would like to return a set of results based on input parameters.

Minimal select query

SELECT FIRSTNAME, LASTNAME FROM EMPLOYEES WHERE FIRSTNAME LIKE '%' || P_FIRSTNAME || '%';

where P_FIRSTNAME is a parameter coming from the function call.

This works mostly fine, however in case the incoming parameter is an empty string, I would like the select clause to also return rows where FIRSTNAME is NULL. The real query will have more conditions in the where clause.

答案1

得分: 1

添加另一个条件:

select *
from employees
where  :p_firstname is null                      --> this
  or firstname like '%' || :p_firstname ||'%';
英文:

Add another condition:

select *
from employees
where  :p_firstname is null                      --> this
  or firstname like '%' || :p_firstname ||'%';        

huangapple
  • 本文由 发表于 2023年3月3日 20:36:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/75627159.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定