Ich habe eine Tabelle CommentsTable
mit Spalten wie, CommentA, CommentB, CommentC, CommentD, CommentE
.Mehrere Spaltenwerte in eine einzelne Spalte als kommagetrennten Wert
Alle Kommentare Spalten sind VARCHAR (200)
, standardmäßig sind alle Spalten NULL
auch.
Die Daten wie folgt aussieht:
CommentId CommentA CommentB CommentC CommentD CommentE
---------------------------------------------------------------------
12345 NULL C 001 C 002 NULL C 003
45678 C 005 NULL NULL C 007 NULL
67890 C 010 NULL C 011 C 012 NULL
36912 C 021 C 023 C 024 C 025 C 026
ich die Null-Werte zu vermeiden, müssen die restlichen Werte sind concatenate mit comma
.
also die erwartete Ausgabe wie:
CommentId CommetDetails
-------------------------------
12345 C 001, C 002, C 003
45678 C 005, C 007
67890 C 010, C 011, C 012
36912 C 021, C 023, C 024, C 025, C 026
ich mit einfacher Abfrage versucht:
SELECT CommentId, ISNULL(CommentA, '') + ', ' + ISNULL(CommentB, '') + ', ' +
ISNULL(CommentC, '') + ', ' + ISNULL(CommentD, '') + ', ' +
ISNULL(CommentE, '') [CommentDetails]
FROM CommentsTable
WHERE ...... --Some conditions
Aber die unerwünschten comma
aufgetreten sind, hinzugefügt So IIF
SELECT CommentId,
IIF(ISNULL(CommentA, '') <> '', (CommentA + ', '), '') +
IIF(ISNULL(CommentB, '') <> '', (CommentB + ', '), '') +
IIF(ISNULL(CommentC, '') <> '', (CommentC + ', '), '') +
IIF(ISNULL(CommentD, '') <> '', (CommentD + ', '), '') +
ISNULL(CommentE, '') [CommentDetails]
FROM CommentsTable
WHERE ...... --Some conditions
Aber hier auch der comma
trat zuletzt auf Position für einige Fälle (Wenn CommentD, CommetE
sind NULL
.
Gibt es einen Weg zu erreichen, um für alle Fälle zu lösen.
update Sie sql Geigenskript mit Ihrem letzten Beispiel Aussaat genau die Situation, wo Sie ein unerwünschtes Komma am Ende haben. –