1

Is there a more simplistic way to add characters throughout an string at the time of concatenation? I'm thinking of utilizing an function to do this and can achieve it by doing a concatenation; however, is there an kind of alternative that can be done other than concatenation?

Here is what I need to do...There is Table A which has 8 columns, for this example col1, col2, col3, etc.

;WITH
 TABLE_A
 AS
 (
  SELECT 'ABC' AS Col1
         , 'COM' AS Col2
         , 'SMALL' AS Col3
         , '1234' AS Col4
         , 'ABC INC.' AS Col5
         , '123456789' AS Col6
         , 'ABC Of New England' AS Col7
         , 'NC123456' AS Col8
 )
  SELECT *
  FROM TABLE_A

I need to concatenate the values within the columns with an > between each. I can accomplish this by doing something along the lines of...

CONCAT(Col1,'>',Col2,'>',Col3,'>',Col4,'^',Col5,'>',Col6,'>',Col7,'>',Col8)

but I need it to be dynamic, so for example if col1 - Col3 has a value then the concatenation has to occur for only those CONCAT(Col1,'>',Col2,'>',Col3) and if Col1 - Col5 has values then CONCAT(Col1,'>',Col2,'>',Col3,'>',Col4,'^',Col5) and so on, the concatenation should only go as far as there are values.

Also to add another wrinkle, instead of an > between Col4 and Col5, I need to have a carrot ^ as the delimiter.

Is there an alternative method to do this without using CONCAT? Maybe a loop?

2 Answers 2

1

Here's an example of something that will work, although it only has minimal validation to meet your requirements.

;WITH
 TABLE_A
 AS
 (
  SELECT 'ABC' AS Col1
         , 'COM' AS Col2
         , 'SMALL' AS Col3
         , '1234' AS Col4
         , 'ABC INC.' AS Col5
         , '123456789' AS Col6
         , 'ABC Of New England' AS Col7
         , 'NC123456' AS Col8

 )
  SELECT ConcentenatedString = ISNULL(('>' + NULLIF(a.Col1,'')),'')
        +ISNULL(('>' + NULLIF(a.Col2,'')),'')
        +ISNULL(('>' + NULLIF(a.Col3,'')),'')
        +ISNULL(('>' + NULLIF(a.Col4,'')),'')
        +ISNULL(('^' + NULLIF(a.Col5,'')),'')
        +ISNULL(('>' + NULLIF(a.Col6,'')),'')
        +ISNULL(('>' + NULLIF(a.Col7,'')),'')
        +ISNULL(('>' + NULLIF(a.Col8,'')),'')
  FROM TABLE_A a

The NULLIFs are used to convert a blank string to a NULL so that the delimiter will be eliminated when concatenated to the NULL. Those NULLs are then converted back to blanks to prevent the rest of the string from being eliminated.

Sign up to request clarification or add additional context in comments.

3 Comments

I can accomplish this 3 different ways from what I can tell... REPLACE(REPLACE(CONCAT(a.Col1,'>',a.Col2,'>',a.Col3,'>',a.Col4,'^',a.Col5,'>',a.Col6,'>',a.Col7,'>',a.Col8),'>>',''),'^>','>') AS [GUID_V1]
, ISNULL((NULLIF(a.Col1,'')),'')+ISNULL(('>' + NULLIF(a.Col2,'')),'') +ISNULL(('>' + NULLIF(a.Col3,'')),'')+ISNULL(('>' + NULLIF(a.Col4,'')),'') +ISNULL(('^' + NULLIF(a.Col5,'')),'')+ISNULL(('>' + NULLIF(a.Col6,'')),'') +ISNULL(('>' + NULLIF(a.Col7,'')),'')+ISNULL(('>' + NULLIF(a.Col8,'')),'') AS [GUID_V2]
, CONCAT(ISNULL((NULLIF(a.Col1,'')),''),ISNULL(('>' + NULLIF(a.Col2,'')),'') ,ISNULL(('>' + NULLIF(a.Col3,'')),''),ISNULL(('>' + NULLIF(a.Col4,'')),'') ,ISNULL(('^' + NULLIF(a.Col5,'')),''),ISNULL(('>' + NULLIF(a.Col6,'')),'') ,ISNULL(('>' + NULLIF(a.Col7,'')),''),ISNULL(('>' + NULLIF(a.Col8,'')),'')) AS [GUID_V3]
0

Using cross apply with nested replace. This will take care of your requirement to have ^ between col4 and col5 when they are not null and also any leading > character.

select case when left(txt,1)='>' then right(txt, len(txt)-1) else txt end as str
from your_table
cross apply  
(select replace(replace(replace(concat(col1,'>'
                                      ,col2,'>' 
                                      ,col3,'>'
                                      ,col4, case when col4 is not null and col5 is not null then '^' else '>' end
                                      ,col5,'>'
                                      ,col6,'>'
                                      ,col7,'>'
                                      ,col8),'>>>>','>'),'>>>','>'),'>>','>') as txt) t;

DEMO

4 Comments

I am using SQL Server 2014. The concat_ws function is for SQL Server 2017 and greater.
@ItalianStallion4215 What if col4 is null. Would you still need an '^' between 3 and 5. And what is col5 is null? I'll update my answer to use the version specific solution.
No, if Col5 is null then the carrot would not be needed, it is only to be used if Col4 and Col5 both have values.
@ItalianStallion4215 Updated for SQL Server 2014. Also handles when col4 or col5 is null

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.