Got the following pl/sql function that extracts numbers from the string:
CREATE FUNCTION extract_number (
in_number VARCHAR2)
RETURN NUMBER IS
BEGIN
RETURN regexp_replace(in_number, '[^[:digit:]]', '');
EXCEPTION
WHEN NO_DATA_FOUND THEN
RETURN NULL;
END;
Need to put NULL where no numeric value was found (instead of just empty row). Example:
Got a table:
create table tab2 (val varchar2(100));
insert into tab2 (val) values ('2133jdhfjshd4');
insert into tab2 (val) values ('afafaf');
insert into tab2 (val) values ('skdjfj6787887hhh');
insert into tab2 (val) values ('324824898');
insert into tab2 (val) values ('4jh4jhh4');
commit;
This is an output from function:
21334
6787887
324824898
444
On the second row I need 'NULL' to be placed.
NO_DATA_FOUND not working. Please advise what should I do?
NULL, why do you want to replace that with a four-letter string? As you saw already, using one of the proposed solutions, you will have to change the data type of the return - from number to varchar2. Presumably a function like this would be used to extract numbers from strings, to be used in further computations - as numbers. Change the function to return varchar2 instead, and a lot of other things will break. If you need to display "NULL" on reports, do it in the reporting layer, not in the function itself.