Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL: if cannot convert to_number set as null

Tags:

sql

oracle

I have a table with this values:

ID      VALUE
-----------------------
23559   200
23562   -1 & {14376}#-1

and I want to do to a select that if I cannot convert to number set NULL.

like image 270
macwadu Avatar asked Jun 24 '11 16:06

macwadu


1 Answers

I generally use translate for this because it is such an odd corner case:

SELECT 
  CASE
     WHEN NOT TRIM(TRANSLATE(COLUMN_NAME, '1234567890', ' ')) IS NULL THEN NULL
     ELSE COLUMN_NAME
  END AS "NUMERIC_COLUMN"
FROM
  TABLE_NAME;

If necessary, that can be turned into a procedure, but I'm not sure that there would be terribly much benefit performance-wise.

like image 200
cwallenpoole Avatar answered Oct 18 '22 11:10

cwallenpoole