In a more general case, when the goal is to "translate" a comma separated list of positive integer values into a list of corresponding strings, in the same order as the original list of values, and without introducing extraneous "commata" and spaces...
We can wield a horrendous expression to achieve that.
As a demonstration.
SELECT op.OP2RevisionIndikation
, CONCAT_WS(', '
, ELT(SUBSTRING_INDEX(SUBSTRING_INDEX(CONCAT(op.OP2RevisionIndikation,REPEAT(',',21)),',', 1 ),',',-1)+0,'Innere Hernie (Meso)','Innere Hernie (PETERSEN)','Weight Regain','Weight Loss Failure','Anastomosenstenose','Dysphagie','Reflux','Gallenreflux','Malnutrition','Diarrhö','Gastrogastrale Fistel','Ulcusperforation','Chronische Ulcus','Chronische Schmerzen','Ileus','Choledocholithiasis nach Magen-Bypass','Leakage')
, ELT(SUBSTRING_INDEX(SUBSTRING_INDEX(CONCAT(op.OP2RevisionIndikation,REPEAT(',',21)),',', 2 ),',',-1)+0,'Innere Hernie (Meso)','Innere Hernie (PETERSEN)','Weight Regain','Weight Loss Failure','Anastomosenstenose','Dysphagie','Reflux','Gallenreflux','Malnutrition','Diarrhö','Gastrogastrale Fistel','Ulcusperforation','Chronische Ulcus','Chronische Schmerzen','Ileus','Choledocholithiasis nach Magen-Bypass','Leakage')
, ELT(SUBSTRING_INDEX(SUBSTRING_INDEX(CONCAT(op.OP2RevisionIndikation,REPEAT(',',21)),',', 3 ),',',-1)+0,'Innere Hernie (Meso)','Innere Hernie (PETERSEN)','Weight Regain','Weight Loss Failure','Anastomosenstenose','Dysphagie','Reflux','Gallenreflux','Malnutrition','Diarrhö','Gastrogastrale Fistel','Ulcusperforation','Chronische Ulcus','Chronische Schmerzen','Ileus','Choledocholithiasis nach Magen-Bypass','Leakage')
, ELT(SUBSTRING_INDEX(SUBSTRING_INDEX(CONCAT(op.OP2RevisionIndikation,REPEAT(',',21)),',', 4 ),',',-1)+0,'Innere Hernie (Meso)','Innere Hernie (PETERSEN)','Weight Regain','Weight Loss Failure','Anastomosenstenose','Dysphagie','Reflux','Gallenreflux','Malnutrition','Diarrhö','Gastrogastrale Fistel','Ulcusperforation','Chronische Ulcus','Chronische Schmerzen','Ileus','Choledocholithiasis nach Magen-Bypass','Leakage')
, ELT(SUBSTRING_INDEX(SUBSTRING_INDEX(CONCAT(op.OP2RevisionIndikation,REPEAT(',',21)),',', 5 ),',',-1)+0,'Innere Hernie (Meso)','Innere Hernie (PETERSEN)','Weight Regain','Weight Loss Failure','Anastomosenstenose','Dysphagie','Reflux','Gallenreflux','Malnutrition','Diarrhö','Gastrogastrale Fistel','Ulcusperforation','Chronische Ulcus','Chronische Schmerzen','Ileus','Choledocholithiasis nach Magen-Bypass','Leakage')
, ELT(SUBSTRING_INDEX(SUBSTRING_INDEX(CONCAT(op.OP2RevisionIndikation,REPEAT(',',21)),',', 6 ),',',-1)+0,'Innere Hernie (Meso)','Innere Hernie (PETERSEN)','Weight Regain','Weight Loss Failure','Anastomosenstenose','Dysphagie','Reflux','Gallenreflux','Malnutrition','Diarrhö','Gastrogastrale Fistel','Ulcusperforation','Chronische Ulcus','Chronische Schmerzen','Ileus','Choledocholithiasis nach Magen-Bypass','Leakage')
, ELT(SUBSTRING_INDEX(SUBSTRING_INDEX(CONCAT(op.OP2RevisionIndikation,REPEAT(',',21)),',', 7 ),',',-1)+0,'Innere Hernie (Meso)','Innere Hernie (PETERSEN)','Weight Regain','Weight Loss Failure','Anastomosenstenose','Dysphagie','Reflux','Gallenreflux','Malnutrition','Diarrhö','Gastrogastrale Fistel','Ulcusperforation','Chronische Ulcus','Chronische Schmerzen','Ileus','Choledocholithiasis nach Magen-Bypass','Leakage')
, ELT(SUBSTRING_INDEX(SUBSTRING_INDEX(CONCAT(op.OP2RevisionIndikation,REPEAT(',',21)),',', 8 ),',',-1)+0,'Innere Hernie (Meso)','Innere Hernie (PETERSEN)','Weight Regain','Weight Loss Failure','Anastomosenstenose','Dysphagie','Reflux','Gallenreflux','Malnutrition','Diarrhö','Gastrogastrale Fistel','Ulcusperforation','Chronische Ulcus','Chronische Schmerzen','Ileus','Choledocholithiasis nach Magen-Bypass','Leakage')
, ELT(SUBSTRING_INDEX(SUBSTRING_INDEX(CONCAT(op.OP2RevisionIndikation,REPEAT(',',21)),',', 9 ),',',-1)+0,'Innere Hernie (Meso)','Innere Hernie (PETERSEN)','Weight Regain','Weight Loss Failure','Anastomosenstenose','Dysphagie','Reflux','Gallenreflux','Malnutrition','Diarrhö','Gastrogastrale Fistel','Ulcusperforation','Chronische Ulcus','Chronische Schmerzen','Ileus','Choledocholithiasis nach Magen-Bypass','Leakage')
, ELT(SUBSTRING_INDEX(SUBSTRING_INDEX(CONCAT(op.OP2RevisionIndikation,REPEAT(',',21)),',', 10 ),',',-1)+0,'Innere Hernie (Meso)','Innere Hernie (PETERSEN)','Weight Regain','Weight Loss Failure','Anastomosenstenose','Dysphagie','Reflux','Gallenreflux','Malnutrition','Diarrhö','Gastrogastrale Fistel','Ulcusperforation','Chronische Ulcus','Chronische Schmerzen','Ileus','Choledocholithiasis nach Magen-Bypass','Leakage')
, ELT(SUBSTRING_INDEX(SUBSTRING_INDEX(CONCAT(op.OP2RevisionIndikation,REPEAT(',',21)),',', 11 ),',',-1)+0,'Innere Hernie (Meso)','Innere Hernie (PETERSEN)','Weight Regain','Weight Loss Failure','Anastomosenstenose','Dysphagie','Reflux','Gallenreflux','Malnutrition','Diarrhö','Gastrogastrale Fistel','Ulcusperforation','Chronische Ulcus','Chronische Schmerzen','Ileus','Choledocholithiasis nach Magen-Bypass','Leakage')
, ELT(SUBSTRING_INDEX(SUBSTRING_INDEX(CONCAT(op.OP2RevisionIndikation,REPEAT(',',21)),',', 12 ),',',-1)+0,'Innere Hernie (Meso)','Innere Hernie (PETERSEN)','Weight Regain','Weight Loss Failure','Anastomosenstenose','Dysphagie','Reflux','Gallenreflux','Malnutrition','Diarrhö','Gastrogastrale Fistel','Ulcusperforation','Chronische Ulcus','Chronische Schmerzen','Ileus','Choledocholithiasis nach Magen-Bypass','Leakage')
, ELT(SUBSTRING_INDEX(SUBSTRING_INDEX(CONCAT(op.OP2RevisionIndikation,REPEAT(',',21)),',', 13 ),',',-1)+0,'Innere Hernie (Meso)','Innere Hernie (PETERSEN)','Weight Regain','Weight Loss Failure','Anastomosenstenose','Dysphagie','Reflux','Gallenreflux','Malnutrition','Diarrhö','Gastrogastrale Fistel','Ulcusperforation','Chronische Ulcus','Chronische Schmerzen','Ileus','Choledocholithiasis nach Magen-Bypass','Leakage')
, ELT(SUBSTRING_INDEX(SUBSTRING_INDEX(CONCAT(op.OP2RevisionIndikation,REPEAT(',',21)),',', 14 ),',',-1)+0,'Innere Hernie (Meso)','Innere Hernie (PETERSEN)','Weight Regain','Weight Loss Failure','Anastomosenstenose','Dysphagie','Reflux','Gallenreflux','Malnutrition','Diarrhö','Gastrogastrale Fistel','Ulcusperforation','Chronische Ulcus','Chronische Schmerzen','Ileus','Choledocholithiasis nach Magen-Bypass','Leakage')
, ELT(SUBSTRING_INDEX(SUBSTRING_INDEX(CONCAT(op.OP2RevisionIndikation,REPEAT(',',21)),',', 15 ),',',-1)+0,'Innere Hernie (Meso)','Innere Hernie (PETERSEN)','Weight Regain','Weight Loss Failure','Anastomosenstenose','Dysphagie','Reflux','Gallenreflux','Malnutrition','Diarrhö','Gastrogastrale Fistel','Ulcusperforation','Chronische Ulcus','Chronische Schmerzen','Ileus','Choledocholithiasis nach Magen-Bypass','Leakage')
, ELT(SUBSTRING_INDEX(SUBSTRING_INDEX(CONCAT(op.OP2RevisionIndikation,REPEAT(',',21)),',', 16 ),',',-1)+0,'Innere Hernie (Meso)','Innere Hernie (PETERSEN)','Weight Regain','Weight Loss Failure','Anastomosenstenose','Dysphagie','Reflux','Gallenreflux','Malnutrition','Diarrhö','Gastrogastrale Fistel','Ulcusperforation','Chronische Ulcus','Chronische Schmerzen','Ileus','Choledocholithiasis nach Magen-Bypass','Leakage')
, ELT(SUBSTRING_INDEX(SUBSTRING_INDEX(CONCAT(op.OP2RevisionIndikation,REPEAT(',',21)),',', 17 ),',',-1)+0,'Innere Hernie (Meso)','Innere Hernie (PETERSEN)','Weight Regain','Weight Loss Failure','Anastomosenstenose','Dysphagie','Reflux','Gallenreflux','Malnutrition','Diarrhö','Gastrogastrale Fistel','Ulcusperforation','Chronische Ulcus','Chronische Schmerzen','Ileus','Choledocholithiasis nach Magen-Bypass','Leakage')
, ELT(SUBSTRING_INDEX(SUBSTRING_INDEX(CONCAT(op.OP2RevisionIndikation,REPEAT(',',21)),',', 18 ),',',-1)+0,'Innere Hernie (Meso)','Innere Hernie (PETERSEN)','Weight Regain','Weight Loss Failure','Anastomosenstenose','Dysphagie','Reflux','Gallenreflux','Malnutrition','Diarrhö','Gastrogastrale Fistel','Ulcusperforation','Chronische Ulcus','Chronische Schmerzen','Ileus','Choledocholithiasis nach Magen-Bypass','Leakage')
, ELT(SUBSTRING_INDEX(SUBSTRING_INDEX(CONCAT(op.OP2RevisionIndikation,REPEAT(',',21)),',', 19 ),',',-1)+0,'Innere Hernie (Meso)','Innere Hernie (PETERSEN)','Weight Regain','Weight Loss Failure','Anastomosenstenose','Dysphagie','Reflux','Gallenreflux','Malnutrition','Diarrhö','Gastrogastrale Fistel','Ulcusperforation','Chronische Ulcus','Chronische Schmerzen','Ileus','Choledocholithiasis nach Magen-Bypass','Leakage')
, ELT(SUBSTRING_INDEX(SUBSTRING_INDEX(CONCAT(op.OP2RevisionIndikation,REPEAT(',',21)),',', 20 ),',',-1)+0,'Innere Hernie (Meso)','Innere Hernie (PETERSEN)','Weight Regain','Weight Loss Failure','Anastomosenstenose','Dysphagie','Reflux','Gallenreflux','Malnutrition','Diarrhö','Gastrogastrale Fistel','Ulcusperforation','Chronische Ulcus','Chronische Schmerzen','Ileus','Choledocholithiasis nach Magen-Bypass','Leakage')
) AS translated
FROM ( SELECT '' AS OP2RevisionIndikation
UNION ALL SELECT '17'
UNION ALL SELECT '3'
UNION ALL SELECT '3, 17'
UNION ALL SELECT '17, 3, 7'
UNION ALL SELECT '17 , ,, ,3, flurb, 747, 17'
UNION ALL SELECT ', x,,,,000017,, 3x ,,x, 01,,17'
) AS op
returns
OP2RevisionIndikation translated
------------------------------- -------------------------------------------------------
17 Leakage
3 Weight Regain
3, 17 Weight Regain, Leakage
17, 3, 7 Leakage, Weight Regain, Reflux
17 , ,, ,3, flurb, 747, 17 Leakage, Weight Regain, Leakage
^^ ^ ^^
, x,,,,000017,, 3x ,,x, 01,,17 Leakage, Weight Regain, Innere Hernie (Meso), Leakage
^^ ^ ^ ^^
Notes and caveats:
Each of the ELT
lines is an exact replica except for one integer that basically specifies which nth element of the comma separated list of integers to extract, the 1st, the 4th, the 5th, etc.
The expression processes only a finite number of elements in the list; a separate ELT expression is required for each element. The example handles up to 20 elements in the comma separated list; this could be extended to handle more elements.
This "works" only for comma separated lists of positive integer values. Each value between commas will be evaluated as an integer. That means an element 'x17'
will evaluate to 0
. Element '-6.2'
will evaluate to -6
. Element
'007jamesbond'
will evaluate to 7
. And so on.
The integer value from the evaulation is used as an index to "lookup" a string from a list of strings in the ELT
function.
Elements that evaluate to an integer values that don't correspond to a string in the list (e.g. 0
, -6
) are ignored, as if the element had not been present in the original list.
The last two row rows in returned in the example demonstrate the behavior with comma separated lists we would probably consider to be malformed. The translation expression is a "best effort", it returns what can be translated. The expression doesn't throw an error, or return NULL or an empty string when the comma separated list is malformed.