I need to find if a column value contains the following pattern:
513-2400-23 - Valid
513-PBS-231 - Valid
521-PB-21 - Valid
52-12-21 - Valid
513-2321 - Not Valid
I have tried the following version and many other but they are working for one case but not for other.
SELECT CASE
WHEN REGEXP_LIKE('B12-23-43', '.-.-.') THEN 'Y'
ELSE 'N' END FROM DUAL;
Assuming that valid pattern requires exactly two dashes, this should work:
SELECT CASE
WHEN REGEXP_LIKE('B12-23-43', '^[^-]+-[^-]+-[^-]+$') THEN 'Y'
ELSE 'N' END FROM DUAL;
The pattern requires the string to start in one or more non-dashes, then to have a dash, then some more non-dash characters, and finally some more non-dash characters.
Select Case WHEN REGEXP_LIKE('B12-23-43',
'^[[:alnum:]]{1,}-[[:alnum:]]{1,}-[[:alnum:]]{1,}$') THEN 'Y'
ELSE 'N' END
FROM DUAL;
UPDATE
To also cover cases such as this one: 544-445-PBBTS-24.3
, it could be extended as shown below:
Select Case When
Regexp_Like('B12-23-43',
'^([[:alnum:]]{1,}-){2}[[:alnum:]]{1,}(-[[:alnum:]]{1,}\.[[:alnum:]]{1,})?$')
THEN 'Y'
ELSE 'N' END
FROM DUAL;
SELECT CASE
WHEN (LEN('B12-23-43') - LEN(REPLACE('B12-23-43', '-', '')) = 2) THEN 'Y'
ELSE 'N' END FROM DUAL;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.