The oracle function to_char converts number to string

  • 2021-01-14 06:56:22
  • OfStack

In many data conversion operations, there is a need to convert 0.007007040000 to 0.70%, which can be achieved using the SQL function to_char.
This function is used to convert DATE or NUMBER data types into displayable strings in the format to_char(number_type, format_mask).
The format '999.99', 9 represents the corresponding value of one specified bit, if the value is 0, it is ignored, if the specified bit has no value, it is represented by a space.
In the format '0990.990', 0 represents the value of the corresponding one specified bit. If the value is 0, it will be displayed as 0. If there is no value, it will also be displayed as 0.
In the format 'FM990.90',FM indicates that the display of the string position number has no value and the display of whitespace is cleared, similar to ltrim.
 
SQL> select to_char(12304.560,'999.99') from dual; 
TO_CHAR(12304.560,'999.99') 
--------------------------- 
####### 
SQL> select to_char(104.560,'999.99') from dual; 
TO_CHAR(104.560,'999.99') 
------------------------- 
104.56 
SQL> select to_char(104.560,'99999.99') from dual; 
TO_CHAR(104.560,'99999.99') 
--------------------------- 
104.56 
SQL> select to_char(104.560,'99999.990') from dual; 
TO_CHAR(104.560,'99999.990') 
---------------------------- 
104.560 
SQL> select to_char(104.560,'0099.990') from dual; 
TO_CHAR(104.560,'0099.990') 
--------------------------- 
0104.560 

 
SQL> 
SQL> select to_char(round(0.007007040000, 4) * 100, 'FM99999999990.90') || '%' as aa, 
2 length(to_char(round(0.007007040000, 4) * 100, 'FM99999999990.90') || '%') as bb 
3 from dual; 
AA BB 
---------------- ---------- 
0.70% 5 
SQL> 
SQL> select to_char(round(0.007007040000, 4) * 100, '99999999990.90') || '%' as aa, 
2 length(to_char(round(0.007007040000, 4) * 100, '99999999990.90') || '%') as bb 
3 from dual; 
AA BB 
---------------- ---------- 
0.70% 16 

This is a common operation for to_char to convert numbers to strings, and there are other formats, as shown in the format template for to_char(numeric).
 
 The template   describe  
9  Value with the specified number of bits  
0  The value of the leading zero  
.  (period)   The decimal point  
,  (comma)   Group (thousand) delimiters  
PR  Negative values in Angle brackets  
S  Negative values with a negative sign (using localization)  
L  Currency symbols (use localization)  
D  Decimal point (use localization)  
G  Grouping delimiters (using localization)  
MI  A minus sign at the indicated position (if a number  < 0 )  
PL  A plus sign at the indicated position (if a number  > 0 )  
SG  Positive at the indicated position / Minus sign  
RN  Roman numerals (enter in  1  and  3999  Between)  
TH or th  Convert to an ordinal number  
V  mobile  n  Bits (decimal) (see note)  
EEEE  Scientific counting. It is not supported now.  

It can also convert time-formatted data into strings, but in a more complex format.

Related articles: