Home » SQL & PL/SQL » SQL & PL/SQL » check the column is numeric or data (oracle 9i)
check the column is numeric or data [message #641068] Mon, 10 August 2015 07:32 Go to next message
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 #641070 is a reply to message #641068] Mon, 10 August 2015 07:37 Go to previous messageGo to next message
Littlefoot
Messages: 21808
Registered: June 2005
Location: Croatia, Europe
Senior Member
Account Moderator
END is missing (after END LOOP) (if that's what you are asking).
Re: check the column is numeric or data [message #641076 is a reply to message #641070] Mon, 10 August 2015 08:10 Go to previous messageGo to next message
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 #641078 is a reply to message #641076] Mon, 10 August 2015 08:24 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
Give us an actual example of data it thinks isn't numeric
Re: check the column is numeric or data [message #641079 is a reply to message #641076] Mon, 10 August 2015 08:27 Go to previous messageGo to next message
LKBrwn_DBA
Messages: 487
Registered: July 2003
Location: WPB, FL
Senior Member
What about considering NULL's?
Razz
Re: check the column is numeric or data [message #641081 is a reply to message #641076] Mon, 10 August 2015 08:35 Go to previous messageGo to next message
arun888
Messages: 100
Registered: June 2015
Location: INDIA
Senior Member
zip_code:

1. NOZIP
2. NOZIP
3. 45679
4. 84894
5. NOZIP


zip_code column is decalared as varchar2(5).

Null is updated as nozip.

Currently it says for all the zip_code fields as non numeric.

[Updated on: Mon, 10 August 2015 08:35]

Report message to a moderator

Re: check the column is numeric or data [message #641082 is a reply to message #641081] Mon, 10 August 2015 08:36 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Quote:
select is_number('i.ZIP_CODE') into v_zipcode from dual;


Obviously the string "'i.ZIP_CODE'" is never a number.

Re: check the column is numeric or data [message #641084 is a reply to message #641082] Mon, 10 August 2015 08:40 Go to previous messageGo to next message
arun888
Messages: 100
Registered: June 2015
Location: INDIA
Senior Member
My actual requirment is null value should be changed as nozip and the Ip_code field should be checked whether it is numeric data or not

Zip_code :

1.4567
2.null
3.9490
4.9590
5.null.

any help how i need to change it.

[Updated on: Mon, 10 August 2015 08:41]

Report message to a moderator

Re: check the column is numeric or data [message #641085 is a reply to message #641084] Mon, 10 August 2015 08:44 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
arun888 wrote on Mon, 10 August 2015 06:40
My actual requirment is null value should be changed as nozip and the Ip_code field should be checked whether it is numeric data or not

Zip_code :

1.4567
2.null
3.9490
4.9590
5.null.

any help how i need to change it.


are below valid zip codes?
9
999999999
Re: check the column is numeric or data [message #641086 is a reply to message #641085] Mon, 10 August 2015 08:47 Go to previous messageGo to next message
arun888
Messages: 100
Registered: June 2015
Location: INDIA
Senior Member
only 5 digits value.

99999
Re: check the column is numeric or data [message #641087 is a reply to message #641086] Mon, 10 August 2015 08:53 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
arun888 wrote on Mon, 10 August 2015 06:47
only 5 digits value.

99999

somebody needs to learn how to count
arun888 wrote on Mon, 10 August 2015 06:40
My actual requirment is null value should be changed as nozip and the Ip_code field should be checked whether it is numeric data or not

Zip_code :

1.4567
2.null
3.9490
4.9590
5.null.

any help how i need to change it.

Re: check the column is numeric or data [message #641088 is a reply to message #641086] Mon, 10 August 2015 08:53 Go to previous messageGo to next message
arun888
Messages: 100
Registered: June 2015
Location: INDIA
Senior Member
its valid. since it is numeric
Re: check the column is numeric or data [message #641089 is a reply to message #641087] Mon, 10 August 2015 08:54 Go to previous messageGo to next message
arun888
Messages: 100
Registered: June 2015
Location: INDIA
Senior Member
sorry i left in the one digit in the example.

Re: check the column is numeric or data [message #641090 is a reply to message #641089] Mon, 10 August 2015 09:01 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
arun888 wrote on Mon, 10 August 2015 06:54
sorry i left in the one digit in the example.


which digit? left, right, middle, other?
I can't tell which.
Re: check the column is numeric or data [message #641091 is a reply to message #641089] Mon, 10 August 2015 09:03 Go to previous messageGo to next message
arun888
Messages: 100
Registered: June 2015
Location: INDIA
Senior Member

first

Zip_code :

1.45674
2.null
3.9490
4.9590
5.null.
Re: check the column is numeric or data [message #641092 is a reply to message #641091] Mon, 10 August 2015 09:05 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator
Michel Cadot wrote on Mon, 10 August 2015 15:36

Quote:
select is_number('i.ZIP_CODE') into v_zipcode from dual;


Obviously the string "'i.ZIP_CODE'" is never a number.

Re: check the column is numeric or data [message #641094 is a reply to message #641092] Mon, 10 August 2015 09:16 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
To hammer Michel's point home think about the difference between:
select is_number('i.ZIP_CODE')

and
select is_number(i.ZIP_CODE)
Re: check the column is numeric or data [message #641095 is a reply to message #641094] Mon, 10 August 2015 09:22 Go to previous messageGo to next message
arun888
Messages: 100
Registered: June 2015
Location: INDIA
Senior Member
Do i need to convert the varchar to number by using to_number
icon12.gif  Re: check the column is numeric or data [message #641097 is a reply to message #641092] Mon, 10 August 2015 09:30 Go to previous messageGo to next message
LKBrwn_DBA
Messages: 487
Registered: July 2003
Location: WPB, FL
Senior Member
Ha ha!
Good catch, but seems arun did not get it.

[Updated on: Mon, 10 August 2015 09:31]

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 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
arun888 wrote on Mon, 10 August 2015 15:22
Do 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 #641127 is a reply to message #641097] Mon, 10 August 2015 23:43 Go to previous messageGo to next message
arun888
Messages: 100
Registered: June 2015
Location: INDIA
Senior Member
quotes are missing in the line.

apart from that i have not able to find the difference.
Re: check the column is numeric or data [message #641129 is a reply to message #641127] Tue, 11 August 2015 00:17 Go to previous messageGo to next message
Littlefoot
Messages: 21808
Registered: June 2005
Location: Croatia, Europe
Senior Member
Account Moderator
Right; but, do you understand what that difference means?
Re: check the column is numeric or data [message #641131 is a reply to message #641129] Tue, 11 August 2015 00:48 Go to previous messageGo to next message
arun888
Messages: 100
Registered: June 2015
Location: INDIA
Senior Member
No i dont know about the difference with/without quotes. it would be great if you could explain me.
Re: check the column is numeric or data [message #641133 is a reply to message #641131] Tue, 11 August 2015 00:59 Go to previous messageGo to next message
Littlefoot
Messages: 21808
Registered: June 2005
Location: Croatia, Europe
Senior Member
Account Moderator
This:
select is_number('i.ZIP_CODE')
means that function IS_NUMBER checks whether string 'i.ZIP_CODE' is a number. Obviously, it is not. Does i.ZIP_CODE looks like a number to you? Of course not! However, that's what you tried to do in one of SELECT statements you posted previously - check whether a string is a number.

On the other hand,
select is_number(i.ZIP_CODE)
means that IS_NUMBER function checks whether data - which is stored into the ZIP_CODE column of a table whose alias is "i" (or the table itself is named "i") - is a number. At first glance you can't know whether something that is stored into that column is, or is not a number, because you just don't know what is in there. A simple test would expand the previous SELECT statement into
select i.ZIP_CODE, is_number(i.ZIP_CODE)
so that you could actually see which value you are checking and - simply by looking at the value and the result your function returns - decide whether you wrote a good function (which does what you wanted it to), or whether you have to modify/fix it.
Re: check the column is numeric or data [message #641140 is a reply to message #641133] Tue, 11 August 2015 03:15 Go to previous messageGo to next message
arun888
Messages: 100
Registered: June 2015
Location: INDIA
Senior Member
If quote is present the column data is not assigned.

select IS_NUMBER('i.ZIP_CODE') INTO v_zipcode from DUAL;

I tried without using quote

select i.ZIP_CODE, IS_NUMBER(i.ZIP_CODE)INTO v_zipcode from DUAL;

But the result is 1 for all the null values and numeric values as well. Expecting null values to be return 0 as defined in the function
Re: check the column is numeric or data [message #641142 is a reply to message #641140] Tue, 11 August 2015 03:25 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
That's because it's perfectly valid to to_number null.
If you want null to return 0 you will need to write code that explicitly checks for null.
Re: check the column is numeric or data [message #641144 is a reply to message #641142] Tue, 11 August 2015 03:31 Go to previous messageGo to next message
arun888
Messages: 100
Registered: June 2015
Location: INDIA
Senior Member
can you please help me how to check for null values.
Re: check the column is numeric or data [message #641145 is a reply to message #641144] Tue, 11 August 2015 03:34 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

what about IS NULL and IS NOT NULL test operator you will find in Database SQL Reference?

Re: check the column is numeric or data [message #641146 is a reply to message #641142] Tue, 11 August 2015 03:35 Go to previous messageGo to next message
arun888
Messages: 100
Registered: June 2015
Location: INDIA
Senior Member
HI cookiemonster,

Yes as u said only for null values as 1 . I have checked for character and it returns o.


Thanks for all your help to make understand.
Re: check the column is numeric or data [message #641584 is a reply to message #641146] Tue, 18 August 2015 08:41 Go to previous messageGo to next message
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 #641589 is a reply to message #641146] Tue, 18 August 2015 10:18 Go to previous messageGo to next message
EdStevens
Messages: 1376
Registered: September 2013
Senior Member
arun888 wrote on Tue, 11 August 2015 03:35
HI cookiemonster,

Yes as u said only for null values as 1 . I have checked for character and it returns o.


Thanks for all your help to make understand.


Now, do you understand the point BlackSwan was trying to make when pointing out numeric values of less than (or more than) 5 digits?

What about zipcode=123
What about zipcode=99999999999999999999

Both will pass a numeric check, and neither are valid US zipcodes. (BTW, what about other countries, like Canada, that use fully alphanumeric zip codes?)

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.

Re: check the column is numeric or data [message #641591 is a reply to message #641589] Tue, 18 August 2015 13:32 Go to previous messageGo to next message
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 #641592 is a reply to message #641591] Tue, 18 August 2015 15:33 Go to previous messageGo to next message
pablolee
Messages: 2882
Registered: May 2007
Location: Scotland
Senior Member
Quote:
zip codes starting with zero are totally legal and in use.

You missed the point that Ed was making I think. Smile

[Updated on: Wed, 19 August 2015 02:36]

Report message to a moderator

Re: check the column is numeric or data [message #641623 is a reply to message #641592] Wed, 19 August 2015 06:34 Go to previous messageGo to next message
EdStevens
Messages: 1376
Registered: September 2013
Senior Member
pablolee wrote on Tue, 18 August 2015 15:33
Quote:
zip codes starting with zero are totally legal and in use.

You missed the point that Ed was making I think. Smile


Actually, he didn't miss it at all. I should have spelled it out more.

TO THE OP:
- NUMBER data types will NOT retain leading zeros.
- Zip codes are not numbers. They are alphanumeric codes. In the US, the standard for those codes is to use only numeric characters. That does not make them numbers.
- Numbers (and dates) have special properties completely separate from the characters used to represent them.
- Numeric data (true numbers) should always be stored in NUMBER data types.
- Non-numeric data (regardless of the characters used to represent it) should be stored in VARCHAR2.

And this, again to the OP: Burn this into your brain: Data is not a number simply because it happens to be represented with a numeric character.
Re: check the column is numeric or data [message #641624 is a reply to message #641623] Wed, 19 August 2015 06:53 Go to previous messageGo to next message
pablolee
Messages: 2882
Registered: May 2007
Location: Scotland
Senior Member
EdStevens wrote on Wed, 19 August 2015 12:34
pablolee wrote on Tue, 18 August 2015 15:33
Quote:
zip codes starting with zero are totally legal and in use.

You missed the point that Ed was making I think. Smile


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.
Re: check the column is numeric or data [message #641627 is a reply to message #641624] Wed, 19 August 2015 08:23 Go to previous message
Bill B
Messages: 1971
Registered: December 2004
Senior Member
You also don't need a function to see if it is all numeric, use

 IF translate(i.zip_code, ' 1234567890',' ')   IS NOT NULL then
--- is not numeric zip
else
-- is numeric zip
end if


There is a space between the two quotes. Do not make it a null

[Updated on: Wed, 19 August 2015 08:24]

Report message to a moderator

Previous Topic: Challenging Query
Next Topic: How to implement Materialized view in PL/SQL using Collections
Goto Forum:
  


Current Time: Thu Apr 25 22:20:18 CDT 2024