Oracle introduces the BAR_CHART SQL function to make query results easier to read and understand. This function converts numeric or Boolean values into visual bars using characters.
How Does BAR_CHART Work?
The function uses a value to determine the length of a bar. You can also customize its scale, width, and display character.
The general syntax is:
BAR_CHART(value, maximum, minimum, width, symbol)
The function accepts five arguments:
value: The numeric or Boolean value to display as a bar.
maximum: The upper limit of the scale. Default: 100 for numeric values and 1 for Boolean values.
minimum: The lower limit of the scale. Default: 0.
width: The maximum bar length in characters. Default: 10 for numeric values and 1 for Boolean values.
symbol: The character used to draw the bar.
Only the first argument is required.
Displaying Tablespace Utilization
One practical use of this function is monitoring tablespace utilization.
For example:
SQL> SELECT tablespace_name, ROUND(used_percent, 2) AS used_percent, BAR_CHART(used_percent) AS utilization, CASE WHEN used_percent >= 95 THEN 'CRITICAL' WHEN used_percent >= 85 THEN 'WARNING' ELSE 'NORMAL' END AS status FROM dba_tablespace_usage_metrics ORDER BY used_percent DESC; TABLESPACE_NAME USED_PERCENT UTILIZATION STATUS __________________ _______________ ______________ ___________ TBS2 99.06 █████████▉ CRITICAL TBS1 93.01 █████████▎ WARNING TBS3 20.48 ██ NORMAL SYSTEM 16.01 █▋ NORMAL SYSAUX 6.97 ▊ NORMAL UNDOTBS1 0 NORMAL TEMP 0 NORMAL 7 rows selected. In this example, BAR_CHART displays tablespace utilization as a visual bar. A tablespace with higher usage gets a longer bar, making it easier to identify tablespaces that are running out of space.
Customizing the Bar
You can customize the appearance of the bar by specifying its width and symbol.
For example:
SQL> SELECT tablespace_name, 2 ROUND(used_percent, 2) AS used_percent, 3 BAR_CHART(used_percent, 100, 0, 20, '#') AS utilization, 4 CASE 5 WHEN used_percent >= 95 THEN 'CRITICAL' 6 WHEN used_percent >= 85 THEN 'WARNING' 7 ELSE 'NORMAL' 8 END AS status 9 FROM dba_tablespace_usage_metrics 10* ORDER BY used_percent DESC; TABLESPACE_NAME USED_PERCENT UTILIZATION STATUS __________________ _______________ _______________________ ___________ TBS2 99.06 #################### CRITICAL TBS1 93.01 ################### WARNING TBS3 20.48 #### NORMAL SYSTEM 16.01 ### NORMAL SYSAUX 6.97 # NORMAL UNDOTBS1 0 NORMAL TEMP 0 NORMAL 7 rows selected. 
Here:
100 defines the maximum value.
0 defines the minimum value.
20 sets the bar width to 20 characters.
'#' specifies the character used to draw the bar.
You can also use a CASE expression to select different symbols based on a condition:
SELECT tablespace_name, ROUND(used_percent, 2) AS used_percent, BAR_CHART( used_percent, 100, 0, 30, CASE WHEN used_percent >= 95 THEN '!' WHEN used_percent >= 85 THEN '+' ELSE '#' END ) AS utilization FROM dba_tablespace_usage_metrics ORDER BY used_percent DESC; 
This allows you to represent different utilization levels with different characters.
Combining Multiple Bars
Another useful capability is combining multiple BAR_CHART results using the concatenation operator (||).
SQL> SELECT tablespace_name, 2 ROUND(used_percent, 2) AS used_percent, 3 BAR_CHART(used_percent, 70, 0, 10, '=') || 4 BAR_CHART(used_percent, 85, 70, 10, '+') || 5 BAR_CHART(used_percent, 100, 85, 10, '#') 6 AS utilization 7 FROM dba_tablespace_usage_metrics 8* ORDER BY used_percent DESC; TABLESPACE_NAME USED_PERCENT UTILIZATION __________________ _______________ _________________________________ TBS2 99.06 ==========++++++++++######### TBS1 93.01 ==========++++++++++##### TBS3 20.48 === SYSTEM 16.01 == SYSAUX 6.97 = UNDOTBS1 0 TEMP 0 7 rows selected. 
This example creates a bar with three different sections, each representing a specific range of tablespace utilization. The first section represents values from 0 to 70, the second from 70 to 85, and the third from 85 to 100.


