Pratap
Pratap

Reputation: 25

Padding to a fixed length

I am trying to do padding (with *) to my select statement list so that all values will have same length upon retrieving.

Assuming my query is 'select distinct city from all_cities' and the list of values are boston, seattle, san francisco, san jose.

I want the output as

  1. boston*********
  2. seattle********
  3. san francisco**
  4. san jose*******

Can anyone provide inputs on this?

Thanks

Upvotes: 0

Views: 406

Answers (2)

KevinDTimm
KevinDTimm

Reputation: 14376

select @MaxLength = max(len(city)) from all_cities; 
select rpad(city, @MaxLength, '*') from all_cities;

As I'm clearly not a SQL whiz, I'm sure there's a way to combine this into a single select (not to mention my knowledge of built-in SQL functions is minimal)

Upvotes: 0

Vamsi Prabhala
Vamsi Prabhala

Reputation: 49260

You can use rpad.

select rpad(city,15,'*') from all_cities;

Upvotes: 2

Related Questions