englishteeth.co.uk

… the weblog of Ian “English Teeth” Robinson
  • rss
  • Home

Oracle “Is Numeric”

Ian | February 20, 2008

Currently Oracle (10g and earlier) does not provide a function to test if a value is numeric. However, it is relatively simple to implement.

The following clause will return the number of non numeric characters found in a string:

LENGTH(TRANSLATE(TRIM(string1), ' +-.0123456789', ' '))

A non zero result means the string is not numeric.

SELECT string_column AS Numbers
FROM table_of_strings
WHERE
LENGTH(TRANSLATE(TRIM(string_column), ' +-.0123456789', ' ')) = 0

Depending on environmental constraints, it may be preferrable to implement a function:

create or replace function getNumberFromString(inputString in varchar2)
  return number
is
  numberValue number;
begin
  numberValue := to_number(inputString);
  return numberValue ;
exception
  when VALUE_ERROR then return null;
end;

This could have been implemented as an “isNumeric” function returning a boolean result. However, this currently would not be supported in SQL that could otherwise benefit.

SELECT string_column AS Numbers
FROM table_of_strings
WHERE getNumberFromString(string_column) IS NOT NULL
Comments
2 Comments »
Categories
development, software
Tags
oracle, pl/sql, sql
Comments rss Comments rss
Trackback Trackback

Author

Ian Robinson is a relatively agile software engineer interested in things both sides of the object relational divide and beyond.

Categories

  • development (37)
  • miscellaneous (28)
  • music (7)
  • software (19)

What I'm Doing...

  • @noelfielding11 why are you in watching telly!? in reply to noelfielding11 2010-04-16
  • What was so good about Nick Drake? These "artists" are covering, music is spot on but no effect at all. Totally lacking the goose pimples. 2010-04-16
  • Some Ginger bloke's on telly covering Nick Drake in a mediocre style. 2010-04-16
  • More updates...

Posting tweet...

Powered by Twitter Tools.

Blogroll

  • Dan North
  • Dave Astels
  • Dave Wood
  • eirikso.com
  • Matt Raible
  • Object Mentor Blog
  • The Ancient Art of Programming
  • The Wisdom of Ganesh

Tags

active-mq architecture bauhaus css db eclipse esb festivals freesat gorm grails groovy hd hibernate htpc java jboss jms junit links mce media center mini music oracle osgi patterns pirsig plugins satellite soa software spring sql struts2 testing themes tools tv vmc web wordpress xml xpath xslt
rss Comments rss valid xhtml 1.1 design by jide powered by Wordpress get firefox