Question : Formating Columns in SQL Server 2005

Hello everyone;

  In Oracle you can format long columns to your liking:


COL description FORMAT A13

So when you do a select on the table containing the description column  -- the description column is displayed with 13 characters.

How would you do the same thing in SQL Server 2005

Thank you for your time

Answer : Formating Columns in SQL Server 2005


I would be using something like :

select left(type_desc,10)

to select the left most 10 characters. If you really want fixed length, then you might need to pad trailing spaces :

select left(type_desc+'          ',10)

if it can be NULL, then should really be checking the nullibility of the column :

select left(isnull(type_desc,'')+'          ',10)

or you can also convert or cast as CHAR(10) as well...

select convert(char(10), type_desc)

select cast(type_desc as char(10))

It would be nice to have a format command / function, but in SQL Server we dont. However, there are a few ways of handling it per above, just a little bit "raw".
Random Solutions  
programming4us programming4us