SQL Oracle - Clob to Number

Asked

Viewed 284 times

0

I have a table which does not have to be remodeled, it is composed by a CLOB field which has decimal values separated by point ex:(123.12)

I need this field to be returned to me in the result as a number, decimal, float or any type that is real numeric.

How do I do this in select ?

I’ve tried to do:

Select TO_NUMBER(TO_CHAR(CLOB)) FROM TABLE, for example.

  • https://stackoverflow.com/questions/12860526/how-to-convert-clob-to-varchar2-inside-oracle-pl-sql converts to char and then to number , attention to the used number format https://Docs.oracle.com/cd/B19306_01/server.102/b14200/sql_elements004.htm using the correct type prevents trivial things from ...

2 answers

2

The problem is the values with point and not comma, to convert to numeric value you need to do a REPLACE, Replacing . (dot) by , (comma)

Following example:

--CRIANDO TABELA TESTE
CREATE TABLE teste_clob(
campo_clob CLOB
);

--INSERINDO DADO TESTE
INSERT INTO teste_clob
(campo_clob)
VALUES
('123.12');

--VERIFICANDO VALOR CLOB
SELECT campo_clob FROM teste_clob;

--CONVERTENDO FORMATO EXPLICATIVO
SELECT TO_NUMBER(REPLACE(valor_char,'.',',')) AS valor_number
FROM(
SELECT CAST(campo_clob AS VARCHAR2(200)) AS valor_char 
FROM teste_clob)

--CONVERTENDO FORMATO SIMPLIFICADO
SELECT TO_NUMBER(REPLACE(campo_clob,'.',',')) AS RESULTADO 
FROM teste_clob;
  • http://sqlfiddle.com/#! 4/b86141/5. Nice, there was no need to replace

  • Miguel, this depends on the database configuration.

0

Good afternoon, I thank everyone who answered, but after so much hitting my head I managed to solve my problem as follows. It’s not something beautiful, but it’s working as I expected.

TO_NUMBER(TO_CHAR(CAST(AMOUNT AS VARCHAR2(200)),'9999999999999999.999999999') AS AMOUNT,

  • You should accept the answer that is correct, this way sincerely does not make much sense. Here it is: http://sqlfiddle.com/#! 4/b86141/5

  • Okay, on the link that Voce passed worked ok. But the problem is, as soon as I do the same in the oracle database, this happens: ORA-01722: invalid number 01722. 00000 - "invalid number" *Cause: The specified number was invalid. *Action: Specify a Valid number.

  • then replace is necessary, as in the other answer I believe it works

Browser other questions tagged

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