Solved

SQL Server Right Padding spaces

Posted on 2015-01-20
5
184 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

In this article I will describe the Backup & Restore 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.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

778 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