• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 621
  • Last Modified:

SQL Where clause to return numeric values only

Hi Experts,
I would like to write an sql query which returns only numeric values from the Emp_ID column that have the length of 6 numbers.
e.g. 342387
If the Emp_ID is shorter than 6 or it contains letters I don't want them to be returned.
Thanks for your help.
0
romsom
Asked:
romsom
  • 6
  • 6
  • 3
  • +2
3 Solutions
 
mbizupCommented:
Try this:

SELECT Emp_ID FROM YourTable WHERE LENGTH(Emp_ID) < 6
0
 
romsomAuthor Commented:
I've tried this but unfortunately it also returns employee IDs which contain letters.
I would like only the ones without letters, e.g. 123456
If it's B12345 I don't want it to be returned
0
 
slightwv (䄆 Netminder) Commented:
Try this:

regexp_like(Emp_ID,'[0-9]{6}')
0
Improve Your Query Performance Tuning

In this FREE six-day email course, you'll learn from Janis Griffin, Database Performance Evangelist. She'll teach 12 steps that you can use to optimize your queries as much as possible and see measurable results in your work. Get started today!

 
romsomAuthor Commented:
This gives me a syntax error: Data type "emp_id" does not match a Defined Type name
0
 
slightwv (䄆 Netminder) Commented:
That doesn't look like an Oracle error.

Can you provide more information?

Here's a complete test case:
drop table tab1 purge;
create table tab1(emp_id varchar2(40));

insert into tab1 values('12345');
insert into tab1 values('B12345');
insert into tab1 values('123456');
insert into tab1 values('Bob');
commit;


select emp_id from tab1 
where regexp_like(emp_id,'[0-9]{6}')
/

Open in new window

0
 
Kyle AbrahamsSenior .Net DeveloperCommented:
SELECT Emp_ID FROM YourTable WHERE LENGTH(Emp_ID) = 6  and isnumeric(Emp_ID) = 1
0
 
slightwv (䄆 Netminder) Commented:
isnumeric is not an Oracle function.
0
 
Kyle AbrahamsSenior .Net DeveloperCommented:
My apologies didn't realize it was oracle:

you can use this function definition for it (taken from: http://www.geekinterview.com/question_details/34549)

CREATE OR REPLACE FUNCTION Isnumeric(p_num VARCHAR2) RETURN NUMBER 
AS
 a NUMBER;
BEGIN
 a := p_num;
 RETURN 1;
EXCEPTION WHEN OTHERS THEN
 RETURN 0;
END;

Open in new window

0
 
romsomAuthor Commented:
The isnumeric(Emp_ID) = 1 gave me the following error: 'The format or data contains a bad character'

LENGTH(Emp_ID) = 6 is working fine, but unfortunately it also returns values like K34325
0
 
slightwv (䄆 Netminder) Commented:
Why create your own function when it isn't necessary?
0
 
slightwv (䄆 Netminder) Commented:
>>'The format or data contains a bad character'

This also doesn't look like an Oracle error.  

Is this really for Oracle?
0
 
romsomAuthor Commented:
I'm using SQL Assistant for the query, and it's Teradata
0
 
romsomAuthor Commented:
Thank you for removing the Oracle Database Zone.
Sorry for the confusion
0
 
mbizupCommented:
How about this?

SELECT Emp_ID 
FROM YourTable
WHERE Emp_ID >= '000000' AND Emp_ID <= '999999'

Open in new window


Or

SELECT Emp_ID 
FROM YourTable
WHERE Emp_ID BETWEEN '000000' AND '999999'

Open in new window

0
 
slightwv (䄆 Netminder) Commented:
Check out this previously asked question:
http://www.experts-exchange.com/Database/Miscellaneous/Q_22059012.html

If you cannot create a UDF, check the between syntax in the last post.  That with the length should work.
0
 
DavidSenior Oracle Database AdministratorCommented:
Romsom, maybe:
case when Regex_INSTR(Column,'[0-9]{6}')  > 0
          then column_data
          end column1
0
 
mbizupCommented:
Also include the length:


SELECT Emp_ID
FROM YourTable
WHERE Emp_ID >= '000000' AND Emp_ID <= '999999' AND LENGTH(Emp_ID) = 6
0
 
romsomAuthor Commented:
Thank you very much everyone!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Improve Your Query Performance Tuning

In this FREE six-day email course, you'll learn from Janis Griffin, Database Performance Evangelist. She'll teach 12 steps that you can use to optimize your queries as much as possible and see measurable results in your work. Get started today!

  • 6
  • 6
  • 3
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now