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
 Right Align text in sql

Author  Topic 

MonikaAhujaRBS
Starting Member

9 Posts

Posted - 2013-12-31 : 00:13:05
Hi,

Is there any built in fuction or any way to right align text in sql with out hardcoding spaces.

for eg : Select cast('fssss' as char(10))+ CAST('ts' as char(15))
result : fssss ts

But as i want result as
Result: fssss ts

Please help

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2013-12-31 : 00:25:48
can you do this in your client ? rather than in SQL ?


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

MonikaAhujaRBS
Starting Member

9 Posts

Posted - 2013-12-31 : 00:41:10
No, i don't have option to write it at client. Need to do in sql only
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2013-12-31 : 00:58:20
[code]SELECT RIGHT ( REPLICATE(' ', 20) + 'the text', 20)[/code]


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

MonikaAhujaRBS
Starting Member

9 Posts

Posted - 2013-12-31 : 04:15:34
Thanks...this worked :)
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-12-31 : 13:10:23
quote:
Originally posted by MonikaAhujaRBS

Thanks...this worked :)


FYI above method also you're hardcoding spaces

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

MonikaAhujaRBS
Starting Member

9 Posts

Posted - 2014-01-01 : 23:27:59
:).... :p...but this a bit indirect hardcoding in my case...as i know the fixed length of my string...so spaces no is same..So acceptable ;-)....but true this is hardcoding spaces...Please suggest some way if you have some better idea ;-):p....
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2014-01-02 : 12:23:56
quote:
Originally posted by MonikaAhujaRBS

:).... :p...but this a bit indirect hardcoding in my case...as i know the fixed length of my string...so spaces no is same..So acceptable ;-)....but true this is hardcoding spaces...Please suggest some way if you have some better idea ;-):p....


My question is why do you want to do this in sql. Right aligning is a presentation need and you've lots of ways to do this in your front end code which is where this is to be done. SQL is not place where you do formatting and presentation related logic.

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
   

- Advertisement -