How to Remove the Right Padding on the Day Name in Oracle

In Oracle Database, when using the TO_CHAR() function to return the day name from a date, padding will be appended to the day name if it’s shorter than the longest valid day name for the given language and calendar.

Well, that’s the default behaviour. However, you can change this if you wish.

To suppress this padding, all you need to do is prepend the day name format element with fm.

Example

Suppose we run the following code:

SELECT 
    TO_CHAR(date '2030-03-08', 'Day, DD Month YYYY')
FROM DUAL;

Result:

Friday   , 08 March     2030

We can see that trailing spaces have been appended to the day name and the month name.

We can use the fm format modifier to suppress this space:

SELECT 
    TO_CHAR(date '2030-03-08', 'fmDay, DD Month YYYY')
FROM DUAL;

Result:

Friday, 8 March 2030

Problem solved.

Notice that this also had the side benefit of suppressing the trailing spaces from the month name.

It also suppressed the leading zero from the day number.

We can use multiple fm modifiers to be more specific about which padding we want to suppress. Each fm in our format model switches fill mode on and off.

Suppose we want to suppress the trailing spaces from the day and month names, but we want to retain the leading zero on the day number. In that case we can do the following:

SELECT 
    TO_CHAR(date '2030-03-08', 'fmDay, fmDD fmMonth YYYY')
FROM DUAL;

Result:

Friday, 08 March 2030

The reason we used a third fm (in front of MONTH) is because the second fm re-enabled padding for all subsequent format elements, which would resulted in the trailing spaces being retained for the month.

Here’s what I mean:

SELECT 
    TO_CHAR(date '2030-03-08', 'fmDay, fmDD Month YYYY')
FROM DUAL;

Result:

Friday, 08 March     2030 

In this case, padding was suppressed from the day name, and then re-enabled for the day number, month, and year.

Fortunately, we can add as many fm modifiers as we need to get the desired outcome.