Solved

SQL Where clause to return numeric values only

Posted on 2013-11-05
19
514 Views
Last Modified: 2013-11-05
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
Comment
Question by:romsom
  • 6
  • 6
  • 3
  • +2
19 Comments
 
LVL 61

Expert Comment

by:mbizup
ID: 39625321
Try this:

SELECT Emp_ID FROM YourTable WHERE LENGTH(Emp_ID) < 6
0
 

Author Comment

by:romsom
ID: 39625327
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
 
LVL 76

Expert Comment

by:slightwv (䄆 Netminder)
ID: 39625350
Try this:

regexp_like(Emp_ID,'[0-9]{6}')
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

Author Comment

by:romsom
ID: 39625368
This gives me a syntax error: Data type "emp_id" does not match a Defined Type name
0
 
LVL 76

Expert Comment

by:slightwv (䄆 Netminder)
ID: 39625385
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
 
LVL 40

Expert Comment

by:Kyle Abrahams
ID: 39625390
SELECT Emp_ID FROM YourTable WHERE LENGTH(Emp_ID) = 6  and isnumeric(Emp_ID) = 1
0
 
LVL 76

Expert Comment

by:slightwv (䄆 Netminder)
ID: 39625402
isnumeric is not an Oracle function.
0
 
LVL 40

Assisted Solution

by:Kyle Abrahams
Kyle Abrahams earned 100 total points
ID: 39625405
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
 

Author Comment

by:romsom
ID: 39625410
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
 
LVL 76

Expert Comment

by:slightwv (䄆 Netminder)
ID: 39625413
Why create your own function when it isn't necessary?
0
 
LVL 76

Expert Comment

by:slightwv (䄆 Netminder)
ID: 39625416
>>'The format or data contains a bad character'

This also doesn't look like an Oracle error.  

Is this really for Oracle?
0
 

Author Comment

by:romsom
ID: 39625429
I'm using SQL Assistant for the query, and it's Teradata
0
 

Author Comment

by:romsom
ID: 39625452
Thank you for removing the Oracle Database Zone.
Sorry for the confusion
0
 
LVL 61

Expert Comment

by:mbizup
ID: 39625458
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
 
LVL 76

Assisted Solution

by:slightwv (䄆 Netminder)
slightwv (䄆 Netminder) earned 100 total points
ID: 39625460
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
 
LVL 23

Expert Comment

by:David
ID: 39625464
Romsom, maybe:
case when Regex_INSTR(Column,'[0-9]{6}')  > 0
          then column_data
          end column1
0
 
LVL 61

Accepted Solution

by:
mbizup earned 300 total points
ID: 39625469
Also include the length:


SELECT Emp_ID
FROM YourTable
WHERE Emp_ID >= '000000' AND Emp_ID <= '999999' AND LENGTH(Emp_ID) = 6
0
 

Author Closing Comment

by:romsom
ID: 39625517
Thank you very much everyone!
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I annotated my article on ransomware somewhat extensively, but I keep adding new references and wanted to put a link to the reference library.  Despite all the reference tools I have on hand, it was not easy to find a way to do this easily. I finall…
This article explains all about SQL Server Piecemeal Restore with examples in step by step manner.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question