regexoracle-databaseoracle11gansi-sql-92

Oracle case on REGEXP_LIKE return false instead true


I have this query

select
    documentat2_.*
    ,case
            when (
                REGEXP_LIKE(documentat2_.VALUE,'^(\\*)$')
            ) then 1
            else 0
        END AS x
from
    DOCUMENT_ATTRIBUTE_BANK documentat1_
inner join
    DOCUMENT_ATTRIBUTE documentat2_
        on documentat1_.DOCUMENT_ATTRIBUTE_ID=documentat2_.DOCUMENT_ATTRIBUTE_ID
where
    exists  (
       select
            documenten0_.DOCUMENT_ID as document_id2_8_
            FROM 
             DOCUMENT documenten0_
        where
            documenten0_.APPLICATION_CODE='xxx' AND 
        documentat1_.DOCUMENT_ID=documenten0_.DOCUMENT_ID
        and documentat1_.DOCUMENT_VERSION=documenten0_.DOCUMENT_VERSION
    )
    and documentat1_.DOCUMENT_VERSION=16
    and documentat1_.BANK_ID='xxxx'
    and documentat2_.CHECKLIST_ID=7
    and documentat2_.NAME='NEW'
    AND documentat2_.VALUE='*'

The query selects records that match field VALUE=' * ' and it works ok, but REGEXP_LIKE return 0! My expectation is 1, not 0! It works with REGEXP_LIKE(documentat2_.VALUE,'(\\ *)') but not with REGEXP_LIKE(documentat2_.VALUE,'^(\\ *)$'), why? In other cases REGEXP_LIKE(documentat2_.VALUE, '^(NO)$|^(\\*)$') it works, because some records has VALUES with "NO" and others with "*". There are no spaces in fields i suppose, so i cant understand why it works so?

What's wrong, or i'm wrong? I'm confused..

Thanks


Solution

  • ^(\\*)$ matches:

    It doesn't match a * character.

    If you want to match a * then use a single backslash to escape the * character:

    select documentat2_.*
         , case
           when (REGEXP_LIKE(documentat2_.VALUE,'^\*$'))
           then 1
           else 0
           END AS x
    from   -- ...
           DOCUMENT_ATTRIBUTE documentat2_
    where  -- ...
           documentat2_.VALUE='*'
    

    Which, for the sample data:

    CREATE TABLE DOCUMENT_ATTRIBUTE (value) AS
    SELECT '*' FROM DUAL UNION ALL
    SELECT '  *  ' FROM DUAL UNION ALL
    SELECT 'NO' FROM DUAL;
    

    Outputs:

    VALUE X
    * 1

    fiddle