Concatenate text and tochar(): Why is there a space between the two

oracle

I want to construct an ID from a textual prefix and a number (more info here).

For example, I want to concatenate ABC with 1, while padding the 1 to 6 digits: ABC000001

However, when I try this, it puts a space between the two:

SELECT 
    'ABC' || to_char('1','000000') AS ID
FROM 
    DUAL;

Result: ABC 000001

Why does it put a space in there?

Best Answer

Because to_char() pads the result with spaces (I never understood why).

You need to use the "fill mode":

'ABC' || to_char('1','FM000000')