Search code examples
sqlsql-serverconcatenationstring-concatenation

SQL 'Stuff' to concatenate add 2 spaces - Rextester demo attached


I have a scenario where I concatenate 3 strings together. The length of each string is 25 and so when I concatenate I want the strings to have fixed width.

Desired output format:

**String1(length 22 + 3spaces)String2(length 9 +16 spaces)String3(length8+17spaces)**

Current output:

****String1(length 22 + 3spaces)**2 additional spaces**String2(length 9 +16 spaces)**2 additional spaces**String3(length8+17spaces)**2 additional spaces**

I don't want the additional spaces to show up. Is there a way to do this?

https://rextester.com/GSWML31014

Attached rextester demo here. Any help?!


Solution

  • for a string of 25 chars before you concatenate

    SELECT (
               SELECT LEFT(t1.testdata + SPACE(25), 25)
               FROM   #temp1 t1
               FOR    XML PATH ('')
           ) AS TheOutput