Solved

SQL Where clause to return numeric values only

Posted on 2013-11-05
19
508 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
Comment Utility
Try this:

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

Author Comment

by:romsom
Comment Utility
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)
Comment Utility
Try this:

regexp_like(Emp_ID,'[0-9]{6}')
0
 

Author Comment

by:romsom
Comment Utility
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)
Comment Utility
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 39

Expert Comment

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

Expert Comment

by:slightwv (䄆 Netminder)
Comment Utility
isnumeric is not an Oracle function.
0
 
LVL 39

Assisted Solution

by:Kyle Abrahams
Kyle Abrahams earned 100 total points
Comment Utility
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
Comment Utility
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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 76

Expert Comment

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

Expert Comment

by:slightwv (䄆 Netminder)
Comment Utility
>>'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
Comment Utility
I'm using SQL Assistant for the query, and it's Teradata
0
 

Author Comment

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

Expert Comment

by:mbizup
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thank you very much everyone!
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
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…

763 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now