Joseph Lasso Joseph Lasso - 11 months ago 42
SQL Question

SQL combine two rows into one value

I need to combine the values of two columns and place the value into the 3rd column. For example,
I have a table like so:

Name Age Key

Joe 4
Mike 10
Larry 20

I want an output where the key is a combination of both name and age columns.

Name Age Key

Joe 4 Joe/4
Mike 10 Mike/10
Larry 20 Larry/20

I need to only combine into the Key field if Name is not an empty value. Combine with a / in between.

I have tried GROUP_CONCAT with no success, I have also tried concat with no success...

select Name, Age, (CASE WHEN table.Name IS NULL OR table.Name = '' then else concat(table.Name, '/', table.Age)) as Key from table

Answer Source

There are two mistakes in your query

       ( CASE
           WHEN TABLE.Name IS NULL
                 OR TABLE.Name = '' THEN '' -- Missing then result 
           ELSE Concat(TABLE.Name, '/', TABLE.Age)
         END ) AS KEY --END missing