Checking that the value of the database column contains only numbers in select

Asked

Viewed 4,141 times

1

I need to find out if the value of a given column of the database contains only numbers in the select itself, because if it is numerical, I need to display the value of another column.

I use an ORACLE database.

Could someone help me with this problem?

1 answer

0


Guys, I was able to solve it using the following:

SELECT 
LENGTH(TRIM(TRANSLATE(TABELA.MEU_CAMPO_A_TESTAR, '0123456789',' '))) TESTE
FROM DUAL;

If the TEST returns null, the field contains only numbers.

With this I made the following CASE to fetch information from one or another column according to the test.

CASE WHEN ( LENGTH(TRIM(TRANSLATE(TABELA.MEU_CAMPO_A_TESTAR, '0123456789',' '))) IS NULL ) THEN TABELA.CAMPO_CASO_NUMERICO ELSE TABELA.MEU_CAMPO_A_TESTAR END AS CAMPO_APRESENTADO

That’s how I solved my problem.

I hope it will help those who seek a similar solution.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.