Determine Oracle null == null

前端 未结 11 1791
栀梦
栀梦 2020-11-28 10:07

I wish to search a database table on a nullable column. Sometimes the value I\'m search for is itself NULL. Since Null is equal to nothing, even NULL, saying



        
相关标签:
11条回答
  • 2020-11-28 10:46

    Another alternative, which is probably optimal from the executed query point of view, and will be useful only if you are doing some kind of query generation is to generate the exact query you need based on the search value.

    Pseudocode follows.

    if (SEARCHVALUE IS NULL) {
        condition = 'MYCOLUMN IS NULL'
    } else {
        condition = 'MYCOLUMN=SEARCHVALUE'
    }
    runQuery(query,condition)
    
    0 讨论(0)
  • 2020-11-28 10:48

    This can also do the job in Oracle.

    WHERE MYCOLUMN || 'X'  = SEARCHVALUE || 'X'
    

    There are some situations where it beats the IS NULL test with the OR.

    I was also surprised that DECODE lets you check NULL against NULL.

    WITH 
    TEST AS
    (
        SELECT NULL A FROM DUAL
    )
    SELECT DECODE (A, NULL, 'NULL IS EQUAL', 'NULL IS NOT EQUAL')
    FROM TEST
    
    0 讨论(0)
  • 2020-11-28 10:49

    In Expert Oracle Database Architecture I saw:

    WHERE DECODE(MYCOLUMN, SEARCHVALUE, 1) = 1
    
    0 讨论(0)
  • 2020-11-28 10:52

    You can do the IsNull or NVL stuff, but it's just going to make the engine do more work. You'll be calling functions to do column conversions which then have to have the results compared.

    Use what you have

    where ((MYCOLUMN=SEARCHVALUE) OR (MYCOLUMN is NULL and SEARCHVALUE is NULL))
    
    0 讨论(0)
  • 2020-11-28 10:54

    If an out-of-band value is possible:

    where coalesce(mycolumn, 'out-of-band') 
        = coalesce(searchvalue, 'out-of-band')
    
    0 讨论(0)
  • 2020-11-28 11:02

    Try

    WHERE NVL(mycolumn,'NULL') = NVL(searchvalue,'NULL')
    
    0 讨论(0)
提交回复
热议问题