Link to home
Start Free TrialLog in
Avatar of pvsbandi
pvsbandiFlag for United States of America

asked on

Adding spaces to the string

Hi,

  I have a string field with values as long as 50 characters and as small as 2 characters or some times, a blank value.
  But i need to display this field so that it displays 10 characters only.
 But if it is less than 10 characters, then the first space characters should be appended to the actual field value.
Example : Field = 'ABCDEFGHIJKL'.
Then it should be substr(Field,1,10) which gives me 'ABCDEFGHIJ'.
But if Field = 'ABCD'
Then it should display '      ABCD' ( 6 spaces followed by the field value).
If the field has a null value, the it should still maintain those 10 spaces.

Please help.
Avatar of momi_sabag
momi_sabag
Flag of United States of America image

try

select  left ( '           ' || coalesce(your_column, ''), 10)
from your_table
SOLUTION
Avatar of momi_sabag
momi_sabag
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Avatar of Kent Olsen
Kent Olsen
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of pvsbandi

ASKER

You guys rock!

ohoh...

Upon further review.....

Momi's simpler query for the right justified string is a better solution than what I posted.  Sorry.


SELECT case when length (string) >= 10 then left (string, 10)
                      else right ('          ', coalesce (string, ''))
             end


Kent