Skip to Main Content
  • Questions
  • counting of numeric value in Varchar2 datatype

Breadcrumb

Question and Answer

Tom Kyte

Thanks for the question, Sachin.

Asked: October 11, 2007 - 11:47 pm UTC

Last updated: October 14, 2007 - 12:07 pm UTC

Version: 10.2.0.1.0

Viewed 1000+ times

You Asked

Hi Tom ,

I have a column in table which datatype is varchar2 , user have entered numeric and char value in that column.

i just want to counting number of numberic value and charactor value as well
and list of numeric and charactor value by Sql Query.

Thanks in Advance,

Sachin
India

and Tom said...

Allow me to correct you - they have NOT entered numeric data. They have input a string, the string might consist only of numbers - but they have entered a STRING, not a number. A string.

So, you want to count the number of rows whereby the string could safely be converted to a number vs the rows where that is not true.... (can you guess a pet peeve of mine? the storing of numbers or dates in strings and pretending they are numbers or dates.... they are not, they are just strings)


ops$tkyte%ORA10GR2> create or replace function is_number( p_string in varchar2 ) return varchar2
  2  is
  3          not_a_number exception;
  4          pragma exception_init( not_a_number, -6502 );
  5  begin
  6          if ( cast(p_string as number) is not null )
  7          then
  8                  return 'Y';
  9          end if;
 10  exception
 11          when not_a_number
 12          then
 13                  return 'N';
 14  end is_number;
 15  /

Function created.

ops$tkyte%ORA10GR2> column is_number format a10
ops$tkyte%ORA10GR2>
ops$tkyte%ORA10GR2> create table t as select decode( mod(rownum,2), 0, object_name, dbms_random.random ) string from all_objects;

Table created.

ops$tkyte%ORA10GR2> select is_number( string ) is_number, count(*) from t group by is_number( string );

IS_NUMBER    COUNT(*)
---------- ----------
Y               25129
N               25129



Rating

  (1 rating)

Is this answer out of date? If it is, please let us know via a Comment

Comments

Sachin, October 17, 2007 - 12:55 am UTC

thanks

More to Explore

PL/SQL demos

Check out more PL/SQL tutorials on our LiveSQL tool.

PL/SQL docs

PL/SQL reference manual from the Oracle documentation library