Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

LPAD and RPAD in SQL Server

Status
Not open for further replies.

DaHapMan

Programmer
Oct 31, 2000
20
0
0
NL
Oracle and MySQL can deal with the RPAD- and LPAD-functions, but SQL Server cannot! Is there a similar function to do the same ??

What I want is a query with fixed sized columns.
for example:

[tt]
table TEST
----------
id int
name nvarchar 10
[/tt]

When querying this table I want the field name 15 characters long .. and filled with a trailing character "#".
like:
"DaHapMan#######"

Hope someone can help. thnx

DaHapMan
 
Add trailing spaces:

1) Select Name=Cast(Name As Nchar(15))
2) Select Name=Left(Name+Replicate(N' ',15),15)

Add leading spaces:

1) Select Name=Right(Replicate(N' ',15)+Name,15)
2) Select Name=Replicate(N' ',15-len(Name))+Name Terry L. Broadbent - DBA
Computing Links:
faq183-874 contains "Suggestions for Getting Quick and Appropriate Answers" to your questions.
 
thnx .. that's what I needed

Regards,
DaHapMan
 
I'm looking for a way to take only the last word in a field.
i.e. "hello blah sql", i want "sql" only.

I've been using a combination of right() and charindex() functions.. but everything there is more than one spaces, it doesn't work anymore.

Is there a better way of doing this no matter how many spaces there are in the field?

Thanks for any replies.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top