Skip to content
Advertisement

trim function not working on result of to_char

The query

gives the result 3 as expected but the query:

gives 003

I need the result to be as the first query (‘3’) but I also need the ‘TO_CHAR’ (I’m using it to create a virtual column from a NUMBER column).

I can not figure why and how to fix it. Also, I can not use regex_replace instead.

Advertisement

Answer

You want to use the FM format model to not include the leading white space.

Which outputs:

| TRIM(BOTH'0'FROMTO_CHAR(3,'FM000')) |
| :---------------------------------- |
| 3                                   |

If you want a generated column then:

Then:

Outputs:

VALUE | TRIMMED_VALUE
----: | :------------
    0 | null         
    3 | 3            
   30 | 3            
  100 | 1            

db<>fiddle here

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement