Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Function to create a valid phone number

PugzlyJun 17 2023

I have a function that checks if a phone NUMBER is valid (is_valid_phonenumber), which is working well.

I like to use this function and wrap it with the code to create a valid phone number and return it.

Something like this. I know the psuedo code is incorrect. I was hoping someone can show me how to make this work.

While(1)

{

SELECT DISTINCT FLOOR(DBMS_RANDOM.VALUE(1e9, 1e10)) AS phone_number

FROM DUAL

If is_valid_phonenumber = 'Y'

RETURN PHONE_NUMBER

/* All digits

** Length = 10

** First and fourth characters can't be 0 or 1

*/

CREATE OR REPLACE FUNCTION is_valid_phonenumber( p_str IN VARCHAR2 )

RETURN VARCHAR2

IS

BEGIN

IF REGEXP_LIKE(p_str, '^[2-9][0-9]{2}[2-9][0-9]{6}$') THEN

RETURN 'Y';

ELSE

RETURN 'N';

END IF;

END;

/

}

This post has been answered by mathguy on Jun 17 2023
Jump to Answer
Comments
Post Details
Added on Jun 17 2023
1 comment
1,019 views