You are casting your column values to char(n)
, which will pad shorter strings and numbers (implicitly converted to strings) to n chars, and truncate longer values. (This is better than using varchar2(n)
, which would error with longer numbers and wouldn't make any difference to shorter strings).
You will have a problem with nulls though, as cast(null as char(n))
- or anything else - is still null, rather than n spaces as you might expect. That may be a problem for any of your columns, but particularly for your case expressions.
If any columns can be null you can use nvl
or coalesce
to treat them as a single space instead, and the cast will then pad those too:
cast(coalesce(First_name, ' ') as char(20))
Rather than casting, you could also use rpad()
:
rpad(coalesce(First_name, ' '), 20, ' ')
For the case expressions you can make the else
clause evaluate to a single space instead of null, but you also need to apply the cast to the overall case expression, not have it within one when
branch; so instead of this:
max(case when email_Rank = 1 then cast(email_address as char(100)) else null end)
you would do:
cast(max(case when email_Rank = 1 then email_address else ' ' end) as char(100))
or if you prefer:
cast(coalesce(max(case when email_Rank = 1 then email_address end), ' ') as char(100))
Your client may have been right-padding the overall string to the same length anyway (SQL*Plus will do that if you have set trimout off
, or if spooling set trimspool off
; which might be what BobC was referring to), but that doesn't really help if what you're really trying to create is fixed length fields, which cumulatively would give you a fixed length record as well - and if you didn't have fixed length fields it would impossible to interpret the data anyway.