check the column is numeric or data [message #641068] |
Mon, 10 August 2015 07:32  |
 |
arun888
Messages: 100 Registered: June 2015 Location: INDIA
|
Senior Member |
|
|
hi all,
I have a doubt on how to check the column is field is numeric or character. I have tried to use the below example. But still the '' is concerned on my procedure. any help please. since the '' is missing in my procedure the function is not executed.
Example :
CREATE FUNCTION is_number (p_string IN VARCHAR2)
RETURN INT
IS
v_new_num NUMBER;
BEGIN
v_new_num := TO_NUMBER(p_string);
RETURN 1;
EXCEPTION
WHEN VALUE_ERROR THEN
RETURN 0;
END is_number;
This new function called is_number would return 1 if the value is numeric and 0 if the value is NOT numeric. You could execute the is_number function as follows:
SELECT is_number('123') FROM dual;
Result: 1
SELECT is_number('123b') FROM dual;
Result: 0
My procedure :
create or replace procedure Main
AS
cursor data
IS
select COMPANY_ID,INTERNAL_CONTACT, ZIP_CODE, ZIP_EXTENSION from temp_tmbl;
v_zipcode NUMBER;
BEGIN
for i in data loop
select is_number(i.ZIP_CODE) into v_zipcode from dual;
IF v_zipcode=0 then
DBMS_OUTPUT.PUT_LINE ('Datas is not numeric'||i.company_id);
END IF;
end loop;
|
|
|
|
Re: check the column is numeric or data [message #641076 is a reply to message #641070] |
Mon, 10 August 2015 08:10   |
 |
arun888
Messages: 100 Registered: June 2015 Location: INDIA
|
Senior Member |
|
|
Problem is eventhough the field is numeric and it throws message as no numeric. any help.
cursor data
IS
select CHAIN_ID,INTERNAL_CONTACT, ZIP_CODE, ZIP_EXTENSION from keerthi_stg;
v_zipcode NUMBER;
BEGIN
for i in data loop
select is_number('i.ZIP_CODE') into v_zipcode from dual;
IF v_zipcode=1 THEN
DBMS_OUTPUT.PUT_LINE ('Datas are numeric'|| v_zipcode);
else
DBMS_OUTPUT.PUT_LINE ('Datas are not numeric'|| v_zipcode);
END IF;
Function :
CREATE FUNCTION is_number (p_string IN VARCHAR2)
RETURN INT
IS
v_new_num NUMBER;
BEGIN
v_new_num := TO_NUMBER(p_string);
RETURN 1;
EXCEPTION
WHEN VALUE_ERROR THEN
RETURN 0;
END is_number;
This new function called is_number would return 1 if the value is numeric and 0 if the value is NOT numeric. You could execute the is_number function as follows:
[Updated on: Mon, 10 August 2015 08:11] Report message to a moderator
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Re: check the column is numeric or data [message #641099 is a reply to message #641095] |
Mon, 10 August 2015 09:41   |
cookiemonster
Messages: 13966 Registered: September 2008 Location: Rainy Manchester
|
Senior Member |
|
|
arun888 wrote on Mon, 10 August 2015 15:22Do i need to convert the varchar to number by using to_number
You need to think long and hard about the difference between the two lines of code I posted and what that difference means.
|
|
|
|
|
|
|
|
|
|
|
|
Re: check the column is numeric or data [message #641584 is a reply to message #641146] |
Tue, 18 August 2015 08:41   |
 |
CraigB
Messages: 386 Registered: August 2014 Location: Utah, USA
|
Senior Member |
|
|
You could also use the NVL() function in your comparison. Eg:
CREATE FUNCTION is_number (p_string IN VARCHAR2)
RETURN INT
IS
v_new_num NUMBER;
BEGIN
v_new_num := TO_NUMBER(NVL(p_string,'A'));
RETURN 1;
EXCEPTION
...
By using the NVL() function to default NULL values to the character string "A" it will cause the TO_NUMBER() function to generate an exception and force your Function to return Zero (0).
Craig...
[Updated on: Tue, 18 August 2015 08:41] Report message to a moderator
|
|
|
|
Re: check the column is numeric or data [message #641591 is a reply to message #641589] |
Tue, 18 August 2015 13:32   |
Bill B
Messages: 1971 Registered: December 2004
|
Senior Member |
|
|
zip codes starting with zero are totally legal and in use. There are a number of sites available where you can download a list of valid zip codes. Even if you say a number between 00000 and 99999 it still may not be a legal zip code. I would make a database table using the downloaded listings (which give information like city and state) and just check to see if the entered zip code is in the table.
|
|
|
|
|
Re: check the column is numeric or data [message #641624 is a reply to message #641623] |
Wed, 19 August 2015 06:53   |
pablolee
Messages: 2882 Registered: May 2007 Location: Scotland
|
Senior Member |
|
|
EdStevens wrote on Wed, 19 August 2015 12:34pablolee wrote on Tue, 18 August 2015 15:33Quote:zip codes starting with zero are totally legal and in use.
You missed the point that Ed was making I think. 
Actually, he didn't miss it at all.
Hmmm
Quote:What about a zip code whose character string representation is '01234'? As a numeric, that will only be 1234. 4 digits. Not a valid zip code. From that I take it that you accept that zip codes can start with a zero, but if you convert to a number that leading zero is lost, leaving just 4 digits, rendering it a 'non-zipcode'. i.e. you are NOT saying that zip codes cannot start with a zero (quite the opposite in fact).
Quote:zip codes starting with zero are totally legal and in use. To me implies that Bill thought that you were implying that zip codes cannot start with a zero (I can't see a different meaning behind that, as it doesn't read like it's a confirmation of what you said, rather it reads like a contradiction.) not that any of that matters remotely as both posts effectively make the same point and Bill's provides (imvho) a rather neat and tidy solution (assuming the list is properly maintained). I'll just shutup now and get back in my cage.
|
|
|
|