2

Is there a way to get SQL Server to output a string with a fixed width? For example

SELECT FixedWidth(cola, 7), FixedWidth(colb, 10) ...

That would output 2 strings with 7 and 10 characters wide repsectively? Something like

'test   ' 'foobar    '
'aabbb  ' 'hello     '
'foo    ' 'bar       '
Jim Mitchener
  • 8,835
  • 7
  • 40
  • 56

2 Answers2

8
  SELECT 
       CAST(cola as char(7)) as cola, 
       CAST(colb as char(10)) as colb ....
Martin Smith
  • 438,706
  • 87
  • 741
  • 845
2

You can cast to fixed char or nchar. For example

SELECT CAST(column1 as nchar(10)) [...]
Marcin Krupowicz
  • 536
  • 6
  • 16