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
 Other Forums
 Other Topics
 Right Justification of Character Fields

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2003-07-02 : 07:19:05
Will writes "How would you right justify a character field in query?"

setbasedisthetruepath
Used SQL Salesman

992 Posts

Posted - 2003-07-02 : 09:33:05
Why would you want to? Formatting is a presentation layer issue.

If you really really want to, however, read up on REPLICATE() in BOL.

Jonathan
{0}
Go to Top of Page

mohdowais
Sheikh of Yak Knowledge

1456 Posts

Posted - 2003-07-02 : 09:34:13
You should really be doing this at the presentation layer. But anyway something like this will do it:

USE Northwind
SELECT space(50 - len(companyname)) + companyname FROM customers

Owais

Go to Top of Page

nr
SQLTeam MVY

12543 Posts

Posted - 2003-07-02 : 12:35:07
or
right justification
select right(replicate(' ',50) + companyname,50) from customers
left justification
select left(companyname + replicate(' ',50),50) from customers

replace the char in the replicate statement with your padding character.
This sort of thing will work in most languages.

==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page
   

- Advertisement -