Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

sql string space padding

Posted on 2014-10-31
15
Medium Priority
?
180 Views
Last Modified: 2014-11-03
I have a char(10) columnA with values:
George
Tom
David

problem is when I take Len(columnA) i get:
5
3
5

how to best change type or pad spaces such that Len(columnA) is always = 10?
0
Comment
Question by:prequel_server
[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
  • 6
  • 5
  • 3
  • +1
15 Comments
 
LVL 66

Expert Comment

by:Jim Horn
ID: 40416001
Just a thought ... why do you need len(ColumnA) = 10?  
Left or right padded spaces?

If you're doing this for an export, to guarantee ten spaces you can use ColumnA + SPACE(10 - Len(ColumnA))
0
 
LVL 3

Author Comment

by:prequel_server
ID: 40416049
I have a program that only works if the length of that column is 10.
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 40416054
you can use DataLength:

DECLARE @x CHAR(10)
SET @x = 'tom'
SELECT @x, LEN(@x), DATALENGTH(@x)

Open in new window

0
Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

 
LVL 70

Expert Comment

by:Scott Pletcher
ID: 40416161
LEN(columnA + '.') - 1

The potential issue with DATALENGTH is if you ever change the data type to nchar(10), you'll get "20" as the result instead of "10".


SELECT LEN(columnA + '.') - 1
FROM (
    SELECT CAST('George' AS char(10)) AS columnA UNION ALL
    SELECT CAST('Tom' AS char(10)) AS columnA UNION ALL
    SELECT CAST('David' AS char(10)) AS columnA
) AS test_data
0
 
LVL 3

Author Comment

by:prequel_server
ID: 40416234
Sorry i wasnt clear. My issue is not what len() returns its just that i want my column to be padded with trailing spaces such that it always adds up to 10 chars.

So David should be David and the 5 spaces after it
Tom would have 7 spaces after it
0
 
LVL 66

Expert Comment

by:Jim Horn
ID: 40416237
>So David should be David and the 5 spaces after it
The T-SQL I posted in the first comment does that,
0
 
LVL 70

Expert Comment

by:Scott Pletcher
ID: 40416270
LEFT(columnA + SPACE(10), 10)
0
 
LVL 3

Author Comment

by:prequel_server
ID: 40416397
@Scott, I test tried that but unfortunately it doesn't pass the litmus test. of:

LEN (  LEFT(columnA + SPACE(10), 10)  )

still getting various sizes based on the values in the columnA
0
 
LVL 66

Expert Comment

by:Jim Horn
ID: 40416402
>still getting various sizes based on the values in the columnA
Please give us a sample data set of ColumnA values, and what your expected results are.
0
 
LVL 70

Accepted Solution

by:
Scott Pletcher earned 1500 total points
ID: 40416414
>>
@Scott, I test tried that but unfortunately it doesn't pass the litmus test. of:
 LEN (  LEFT(columnA + SPACE(10), 10)  )
 still getting various sizes based on the values in the columnA
<<

>> My issue is not what len() returns its just that i want my column to be padded with trailing spaces such that it always adds up to 10 chars. <<

I corrected for the LEN() issue earlier, then you said to ignore length and just output 10 chars.

It seems you want contradictory things.  The LEN() function will always ignore trailing spaces.
0
 
LVL 3

Author Comment

by:prequel_server
ID: 40416446
Sorry i didnt know that len() ignors all trailing spaces. Is there another test i use to check?
0
 
LVL 70

Expert Comment

by:Scott Pletcher
ID: 40416450
My version is:

SELECT LEN(columnA + '.') - 1

putting a non-space at the end so SQL counts all characters from the column, including trailing spaces.  Adding a '.' of course changes the trailing spaces into embedded spaces, and thus they are no longer ignored.
0
 
LVL 3

Author Comment

by:prequel_server
ID: 40417375
thanks but I don't want an embedded space '.' at the end of the value, I only want enough spaces to fill a 10 char field.
Len() ignores trailing spaces.
is there another length function or nested functions that can count the trailing spaces in a string?
0
 
LVL 70

Expert Comment

by:Scott Pletcher
ID: 40419465
I'm subtracting 1 from the length to drop the '.' back out; the '.' is just being used to force a non-space character at the end of the value so that LEN() can count all the other chars.
0
 
LVL 3

Author Comment

by:prequel_server
ID: 40419482
Correct but not exactly what im looking for. I guess i was looking for a test to prove that the field is in fact 10 chars in length.  I thought there would be a function that does count trailing spaces but maybe im wrong. Anyway thanks for your help.
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

I have written a PowerShell script to "walk" the security structure of each SQL instance to find:         Each Login (Windows or SQL)             * Its Server Roles             * Every database to which the login is mapped             * The associated "Database User" for this …
Naughty Me. While I was changing the database name from DB1 to DB_PROD1 (yep it's not real database name ^v^), I changed the database name and notified my application fellows that I did it. They turn on the application, and everything is working. A …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

705 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