?
Solved

SQL Server Right Padding spaces

Posted on 2015-01-20
5
Medium Priority
?
343 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 66

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 2000 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 66

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

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.
Suggested Courses

762 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