SELECT REGEXP_SUBSTR('STRING_EXAMPLE','[^_]+',1,1) from dual
- the correct answer as published by user1717270
If you use INSTR , it will give you a position for the line, which assumes that it contains "_" in it. What if this is not so? Well, the answer will be 0. Therefore, when you want to print a line, it will print a NULL . Example. If you want to remove the domain from "host.domain". In some cases, you will only have a short name, that is, "host". Most likely you want to print "host". Well, with INSTR it will give you NULL , because it did not find any ".", That is, it will print from 0 to 0. With REGEXP_SUBSTR you will get the correct answer in all cases:
SELECT REGEXP_SUBSTR('HOST.DOMAIN','[^.]+',1,1) from dual;
Host
and
SELECT REGEXP_SUBSTR('HOST','[^.]+',1,1) from dual;
Host
Loquillo Amigo May 23 '15 at 9:07 2015-05-23 21:07
source share