How to concatenate multiple columns into one on Azure Synapse

ebdffaop  于 2023-02-28  发布在  其他
关注(0)|答案(1)|浏览(120)

I'm trying do concat on some columns using WHEN condition on Azure Synapse Serverless, but there is a problem that in this new column it shows me every time only one column and the concat function doesn't work.

Here is the code:

SELECT 
    Categorie, ParentGuid,
    COALESCE(CAST(EnfantGuid_7 AS nvarchar(1000)),
             CAST(EnfantGuid_6 AS nvarchar(1000)), 
             CAST(EnfantGuid_5 AS nvarchar(1000)), 
             CAST(EnfantGuid_4 AS nvarchar(1000)), 
             CAST(EnfantGuid_3 AS nvarchar(1000)), 
             CAST(EnfantGuid_2 AS nvarchar(1000)),
             CAST(EnfantGuid_1 AS nvarchar(1000))) EnfantGuid,
    CASE 
        WHEN EnfantGuid_7 IS NOT NULL 
            THEN CONCAT_WS(', ', CAST(EnfantGuid_1 AS nvarchar(1000)), CAST(EnfantGuid_2 AS nvarchar(1000)), CAST(EnfantGuid_3 AS nvarchar(1000)), CAST(EnfantGuid_4 AS nvarchar(1000)), CAST(EnfantGuid_5 AS nvarchar(1000)), CAST(EnfantGuid_6 AS nvarchar(1000)))
        WHEN EnfantGuid_6 IS NOT NULL 
            THEN CONCAT_WS(', ', EnfantGuid_1, EnfantGuid_2, EnfantGuid_3, EnfantGuid_4, EnfantGuid_5)
        WHEN EnfantGuid_5 IS NOT NULL 
            THEN CONCAT_WS(', ',EnfantGuid_1, EnfantGuid_2, EnfantGuid_3, EnfantGuid_4)
        WHEN EnfantGuid_4 IS NOT NULL 
            THEN CONCAT_WS(', ',EnfantGuid_1, EnfantGuid_2, EnfantGuid_3)
        WHEN EnfantGuid_3 IS NOT NULL 
            THEN CONCAT_WS(', ', CAST(EnfantGuid_2 AS nvarchar(1000)), CAST(EnfantGuid_1 AS nvarchar(1000)))
        ELSE EnfantGuid_1
    END AS [Path],  
    EnfantGuid_1, EnfantGuid_2, EnfantGuid_3

All columns inside the CASE WHEN condition are of type UniqueIdentifier . I tried to cast them to string as shown below but no result..

Anyone have an idea for this problem please?

Sample of result:
| Category | ParentGuid | EnfantGuid | Path | EnfantGuid_1 | EnfantGuid_2 | EnfantGuid_3 | EnfantGuid_4 | EnfantGuid_5 | EnfantGuid_6 | EnfantGuid_7 |
| ------------ | ------------ | ------------ | ------------ | ------------ | ------------ | ------------ | ------------ | ------------ | ------------ | ------------ |
| a | 432025b5-5a5b | 36d9d9b3-5a5b | c4cae4b5-5a5b | c4cae4b5-5a5b | 66d9d9b3-5a5b | 36d9d9b3-5a5b | NULL | NULL | NULL | NULL | NULL |

In this row Path must contain EnfantGuid_1 + EnfantGuid_2 but it have only Enfant_Guid1 ==> The last CASE WHEN condition

aiazj4mn

aiazj4mn1#

Cast the datatype of EnfantGuid_1 field in else part as varchar datatype. When new column is defined using case when statement, the datatype of the column is the datatype of expression in the else section.

case when .....
        ......     
         ELSE 
            CAST(EnfantGuid_1 AS nvarchar(1000))
    END AS [Path]

I tried this with sample data.

create table tab1 ( v1 uniqueidentifier null, v2 uniqueidentifier null, v3 uniqueidentifier null, v4 uniqueidentifier null)
insert into tab1 values(newid(),newid(),newid(),newid());
insert into tab1 values(newid(),newid(),newid(),null);
insert into tab1 values(newid(),newid(),null,null);
select * from tab1
v1v2v3v4
d9a9b947-6c4f-47bb-b88b-d59d6e50a611ee3d9a9e-1b13-4189-9aa2-01a27c917cf82f0f4ae0-2251-45d2-8428-44061a6d84eb586df0a6-7663-4130-9bb1-e69cb61265d1
10e06207-39f6-4de1-b172-f8f42274c3fa0d14106c-06b7-4218-a396-574c5d48157bfbd952ef-5364-4d87-8473-b0545cf5ed59null
6f957995-8717-4bc7-8d75-0bd7d195a6c5c2128d35-5b6f-4b60-bb75-f2b4f6a28ad7nullnull

Expression in Else is of uniqueidentifier type:

select 
  case 
  when v4  is not null
   then concat_ws(', ', v1, v2, v3)
  when v3 is not null
   then concat_ws(', ', v1, v2)
  else
   v1
  end as concat_column 
from tab1
concat_column
d9a9b947-6c4f-47bb-b88b-d59d6e50a611
10e06207-39f6-4de1-b172-f8f42274c3fa
6f957995-8717-4bc7-8d75-0bd7d195a6c5

Expression in Else is of varchar type:

select 
  case when v4  is not null
   then concat_ws(', ', v1, v2,v3)
  when v3 is not null
   then concat_ws(', ', v1, v2)
  else
   cast(v1 as varchar(100))
  end as concat_column 
from tab1
concat_column
D9A9B947-6C4F-47BB-B88B-D59D6E50A611, EE3D9A9E-1B13-4189-9AA2-01A27C917CF8, 2F0F4AE0-2251-45D2-8428-44061A6D84EB
10E06207-39F6-4DE1-B172-F8F42274C3FA, 0D14106C-06B7-4218-A396-574C5D48157B
6F957995-8717-4BC7-8D75-0BD7D195A6C5

db<>fiddle .

相关问题