Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

ORA-01481: invalid number format model (convert number to char)

I’m confused as to why I’m getting this error. I use a function that stores a NUMBER variable and returns a NUMBER (e.g. 123456).

I want to convert this to a format of my choosing, so I would use to_char:

 select to_char(total_count_prods('apple'),'1234-56') from dual;

And I keep getting that error. What’s the issue? If I run

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

select total_count_prods('apple') from dual;

Then I get the following output:

 123456

How can I convert that to:

 1234-56

>Solution :

You can use SUBSTR:

SELECT SUBSTR(total_apples, 1, 4) || '-' || SUBSTR(total_apples, 5)
FROM   (
  SELECT total_count_prods('apple') AS total_apples
  FROM   DUAL
);

Or REGEXP_REPLACE:

SELECT REGEXP_REPLACE(
         total_count_prods('apple'),
         '(\d{2})$',
         '-\1'
       )
FROM   DUAL;

Or, divide by 100 and use TO_CHAR and set the decimal separator to -:

SELECT TO_CHAR(
         total_count_prods('apple')/100,
         'FM9999D99',
         'NLS_NUMERIC_CHARACTERS=-,'
       )
FROM   dual;

fiddle

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading