Solved

SQL Server Right Padding spaces

Posted on 2015-01-20
5
176 Views
Last Modified: 2015-01-20
I need to right pad a sql Server field with spaces.  I have tried:
 Select Field + replicate(char(32), 10)
 Select Field + space(10)
Select Cast(Field AS CHAR) + replicate(char(32), 10)
Etc
The field is NVARCHAR(50)

I can right pad with any character BUT space.

I tried ANSI_PADDING both OFF and ON
0
Comment
Question by:tim44202
  • 2
  • 2
5 Comments
 
LVL 65

Expert Comment

by:Jim Horn
ID: 40560507
How about...
Declare @str nvarchar(50) = '123456'
SELECT @str + SPACE(50 - LEN(@str)) + 'looky here, a 50 character string'

Open in new window

0
 
LVL 50

Accepted Solution

by:
Lowfatspread earned 500 total points
ID: 40560569
sorry how is it failling?

you do know that len(columnname) ignores trailing spaces...?

use datalength(columnname)  for the true length .... (it gives the true  number of bytes consumed...)
0
 

Author Closing Comment

by:tim44202
ID: 40560839
Great job of seeing through the fog!
0
 
LVL 65

Expert Comment

by:Jim Horn
ID: 40561079
So ... what was the answer?
0
 

Author Comment

by:tim44202
ID: 40561121
The answer is that there was no problem,  The use of LEN(columnName) made it look as if there were a problem.The recommended use of datalength(ColumnName) gives the real length in bytes.
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to shrink a transaction log file down to a reasonable size.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now