What if SQL fuzzy query encounters a null value ?, SQL fuzzy query is blank

Source: Internet
Author: User

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 % '~

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.