Oracle LIKE Operator

I am a C#, MS SQL developer who is new to Oracle PL/SQL.

My question is probably simple to you experts.

I created a function that searches for a substring within a string and returns the substring if found or 'none' if not found.

I am calling the function in a select statement using the dual table.

The function:

CREATE OR REPLACE FUNCTION area_code (phone_number IN VARCHAR2)

RETURN VARCHAR2 AS

BEGIN

  IF phone_number LIKE '___-___-____' THEN

    --we have a phone number with an area code.

    RETURN SUBSTR(phone_number,1,3);

  ELSE

    --there is no area code

    RETURN 'none';

  END IF;

END;

/

SQL Statement:
select area_code('716') from dual ;
Returns 'none'
select area_code('716-123-456') from dual ;
Also returns 'none'

What am I missing?

Thanks,
DovbermanAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

sdstuberCommented:
area_code('716-123-456')

your second example doesn't have 4 digits in the last portion

try this...


SELECT area_code('716-123-4567') FROM DUAL;


The underscore  for LIKE is a wildcard of exactly one character.

So ___-___-_____  means

exactly 3 characters, dash, exactly 3 characters, dash, exactly 4 characters

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
DovbermanAuthor Commented:
Never mind, I found the error.

Changed IF phone_number LIKE '___-___-____' THEN

to IF phone_number             LIKE '___-___-_____' THEN

Needed 4 dashes as place holders to match the pattern specified in the function.

All programming languages are picky.
DovbermanAuthor Commented:
I've requested that this question be closed as follows:

Accepted answer: 0 points for Dovberman's comment #a40299552

for the following reason:

I found my error.
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

mcsweenSr. Network AdministratorCommented:
The LIKE operator works exactly the same as SQL.

Select area_code from dual where somefield LIKE '716%';

Open in new window

sdstuberCommented:
you might want to try using regular expressions then you can check for digits rather than just any characters

IF REGEXP_LIKE(phone_number, '[0-9]{3}-[0-9]{3}-[0-9]{4}') THEN
sdstuberCommented:
No, the problem wasn't the number of dashes, you already had 3-3-4

the problem was your input data didn't look like a phone number.

The first post identified the problem.

Your suggested solution actually creates an additional problem because it has 5 underscores.  

This means

123-123-12345 would be accepted as a phone number even though it's not correct

Also, you may want to use my followup post anyway since it's a more thorough check than simple wildcarding
sdstuberCommented:
The example query in

http:#a40299553

has addtional problems because it checks only for starting with 3 digits

'716%'  would match  these strings which are not correct for that function

'716'
'716-123-'
'716-scoobydoo'
DovbermanAuthor Commented:
It was not as simple or complete as I had anticipated.

Thanks for the additional analyses.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Oracle Database

From novice to tech pro — start learning today.