Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Leading Spaces

Author  Topic 

Vack
Aged Yak Warrior

530 Posts

Posted - 2012-05-21 : 14:24:58
How do I add leading spaces to a field?

Need to make sure the value is 20 characters long with leading spaces.

'632' needs to become ' 632'

X002548
Not Just a Number

15586 Posts

Posted - 2012-05-21 : 14:40:14
Usually it's Trailing spaces

Ist it coming from a column or a local variable?


DECLARE @x varchar(255)
SET @x = '632'
SELECT '"'+@x+'"'
SELECT '"'+RIGHT(space(20)+@x,20)+'"' AS [@x+20]

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-05-21 : 16:06:55
quote:
Originally posted by Vack

How do I add leading spaces to a field?

Need to make sure the value is 20 characters long with leading spaces.

'632' needs to become ' 632'


STR(field,20)

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2012-05-22 : 06:31:45
This is related to formatting. If you use front end application do the formation there

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -