Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

numbers to K and/or M like: 2000 -> 2K and 12000000 -> 1.2M

SmithJohn45Jun 5 2024 — edited Jun 5 2024

during my search for mentioned in topic title, i found a stackoverflow link which is more than 10 years old and i am using 21c, it is working fine but i want to know is there any other approach / new function by oracle which can help to achieve this using SQL command? this is required to generate Charts/Graphs in Oracle Apex.

https://stackoverflow.com/questions/19522582/1000000-to-1m-and-1000-to-1k-in-oracle-query

WITH DATA AS (SELECT power(10, ROWNUM) num FROM dual CONNECT BY LEVEL <= 9)
SELECT num,
      CASE
         WHEN num >= 1e6 THEN
          round(num / 1e6) || 'M'
         WHEN num >= 1e3 THEN
          round(num / 1e3) || 'k'
         ELSE to_char(num)
      END conv
 FROM DATA;

please help.

regards

i can use round(figure, 2) to get 1.2M , currently it will show 1M for 1200000

Comments
Post Details
Added on Jun 5 2024
9 comments
198 views