What if SQL fuzzy query encounters a null value ?, SQL fuzzy query is blank
Author: iamlaosong
The SQL query statement uses % for fuzzy query. in the program, users are generally required to enter part of the information and perform fuzzy query based on the information. For example, if you enter 340104, the following statement queries all emails starting with 340104 yesterday:
select * from tb_evt_mail_clct t where t.clct_date = trunc(sysdate - 1) and t.sender_cust_code like '340104%'
When the user does not enter any information to query all emails of yesterday, the following statement cannot query all information. This statement can only find the mail information of all major accounts, the Mail Information of retail investors cannot be found:
select * from tb_evt_mail_clct t where t.clct_date = trunc(sysdate - 1) and t.sender_cust_code like '%'
This is because the retail customer code is null. The following statement can take into account both of the above two cases (0000 represents a null value ):
Limited value:
select * from tb_evt_mail_clct t where t.clct_date = trunc(sysdate - 1) and nvl(t.sender_cust_code,'0000') like '340104%'
Unlimited value:
select * from tb_evt_mail_clct t where t.clct_date = trunc(sysdate - 1) and nvl(t.sender_cust_code,'0000') like '%'
When the limit value is 0000, all retail investors are returned:
select * from tb_evt_mail_clct t where t.clct_date = trunc(sysdate - 1) and nvl(t.sender_cust_code,'0000') like '0000%'
How can I prevent null values from being involved in Fuzzy queries in SQL?
SELECT
SPClearCommunication,
SPKnowledge,
FROM
TblNASurveyResults
WHERE
(LastName = ''+ rtrim (@ LastName) +'') and
(FirstName = ''+ rtrim (@ FirstName) +'') and
(Email like '%' + rtrim (@ Email) + '% ')
And isnull (@ Email, '') <> '')
I don't know if this is the result you want. Add and isnull (@ Email, '') <>'' To make the blank @ email not involved in the query.
How can I ignore a field that is null in a multi-condition fuzzy query of an SQL statement?
Select distinct t. from (select number, company, ticket number, order number, date, null as item name, null as specification, null as quantity, null as unit price from a) union all (select header, null, item name, specification, quantity, unit price from B) t where t. company like '% variable % '~