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
1条答案
按热度按时间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.
I tried this with sample data.
Expression in Else is of uniqueidentifier type:
Expression in Else is of varchar type:
db<>fiddle .