Concatenate multiple columns, with null values

Valerica

I want to concatenate multiple columns, separated by ;, in MS-SQL Server 2008.

The problem that I have is that without the CONCAT() I don't know how to handle NULL columns and not have results like this tattoos;comics;;;

Here you have the script to create the sample data:

declare @tbl as table (
    id int
    ,kw1 varchar(15)
    ,kw2 varchar(15)
    ,kw3 varchar(15)
    ,kw4 varchar(15)
    ,kw5 varchar(15)
);

insert into @tbl values
(1, 'innocence', 'graphic novel', 'cartoon', NULL, 'comics')
,(2, 'tattoos', 'comics', NULL, NULL, NULL)
,(3, NULL, 'music', 'cartoon', 'adventure', 'film') 

And the table:

+----+-----------+---------------+---------+-----------+--------+
| id |    kw1    |      kw2      |   kw3   |    kw4    |  kw5   |
+----+-----------+---------------+---------+-----------+--------+
|  1 | innocence | graphic novel | cartoon | NULL      | comics |
|  2 | tattoos   | comics        | NULL    | NULL      | NULL   |
|  3 | NULL      | music         | cartoon | adventure | film   |
+----+-----------+---------------+---------+-----------+--------+

So my actual result is this:

+----+-----------------------------------------+
| id |                Keywords                 |
+----+-----------------------------------------+
|  1 | innocence;graphic novel;cartoon;;comics |
|  2 | tattoos;comics;;;                       |
|  3 | ;music;cartoon;adventure;film           |
+----+-----------------------------------------+

But this is what i want:

+----+----------------------------------------+
| id |                Keywords                |
+----+----------------------------------------+
|  1 | innocence;graphic novel;cartoon;comics |
|  2 | tattoos;comics                         |
|  3 | music;cartoon;adventure;film           |
+----+----------------------------------------+

Query:

SET CONCAT_NULL_YIELDS_NULL OFF;

select
    id
    ,kw1 + ';' + kw2 + ';' + kw3 + ';' + kw4 + ';' + kw5 as Keywords
FROM @tbl

Any help is appreciated!

Tanner

In the absence of CONCAT() (SQL Server 2008+) you can use ISNULL() like so:

SELECT 
    t.id ,
    ISNULL(t.kw1 + ';', '') + ISNULL(t.kw2 + ';', '') +
    ISNULL(t.kw3 + ';', '') + ISNULL(t.kw4 + ';', '') +
    ISNULL(t.kw5 + ';', '') AS Vals
FROM
    @tbl AS t;

If the column value is NULL then the joining of NULL + ';' would produce NULL, therefore giving you the empty string instead ''.

For 2008+ you'd use CONCAT() like so:

SELECT 
    t.id ,
    CONCAT(t.kw1 + ';' ,t.kw2 + ';',t.kw3 + ';' ,t.kw4 + ';', t.kw5+ ';') as Vals
FROM 
    @tbl AS t

Both produce this result:

id          Vals
----------- -----------------------------------------------
1           innocence;graphic novel;cartoon;comics;
2           tattoos;comics;
3           music;cartoon;adventure;film;

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Concatenate multiple columns of dataframe with a seperating character for Non-null values

Concatenate two columns of spark dataframe with null values

Concatenate values into multiple columns based on associated value

How do you concatenate multiple columns in a DataFrame into a another column when some values are null?

Concatenate multiple values and removing characaters when null

Get multiple columns with null values

How to concatenate two columns that might contain NULL values in a select statement?

Aggregate and concatenate multiple columns

Combine multiple columns, excluding null values

Postgres - Unique constraint with multiple columns and NULL values

MySQL multiple columns in IN clause with null values

Concatenate strings across columns that are not null

concatenate two columns values pandas

Pandas: filling null values based on values in multiple other columns

How to extract multiple rows from a table based on values from multiple columns from another table and then concatenate in SQL?

Concatenate multiple columns into one in hive

How to Concatenate multiple columns if not empty

How to Concatenate multiple columns at once?

Oracle concatenate String null values

Concatenate results with some null values

How to concatenate two columns of spark dataframe with null values but get one value

How do I replace null values of multiple columns with values from multiple different columns

concatenate columns horizontally, if matches in previous field. Multiple columns to concatenate

concatenate multiple columns but avoid 2 cell if one cell is in blank or null in sql server

Pandas: Concatenate multiple columns using another separator column and avoid extra separators for blank values

How to concatenate values from multiple pandas columns on the same row into a new column?

How to concatenate last non-blank cells values from multiple columns

Modify the contents of null columns in pandas by checking values in multiple colums

Sum of null and duplicate values across multiple columns in pyspark data framew

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    pump.io port in URL

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  5. 5

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  8. 8

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

  9. 9

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  10. 10

    How to remove the extra space from right in a webview?

  11. 11

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  12. 12

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  13. 13

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

  14. 14

    java.lang.NullPointerException: Cannot read the array length because "<local3>" is null

  15. 15

    How to use merge windows unallocated space into Ubuntu using GParted?

  16. 16

    flutter: dropdown item programmatically unselect problem

  17. 17

    Pandas - check if dataframe has negative value in any column

  18. 18

    Nuget add packages gives access denied errors

  19. 19

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  20. 20

    Generate random UUIDv4 with Elm

  21. 21

    Client secret not provided in request error with Keycloak

HotTag

Archive