I'm trying to write some SQL code that will allow me to separate the individual values from this field in a database feed. I have tried a few different things and none are working properly and while I can get it to work via VBA or program it via another language I was wondering if there was a way to do it via SQL directly (with or without having to use a stored procedure). Ideally I would like to be able to populate a table with the unique values only which would require having to check the values already stored in the destination table before inserting the new records however just being able to separate the values into individual entries would be good for now.
SELECT CASE
WHEN CHARINDEX(',', vaccines) > 0
THEN SUBSTRING(vaccines, 1, LEN(vaccines) - CHARINDEX(',', REVERSE(vaccines)))
ELSE ''
END AS vax1,
CASE
WHEN CHARINDEX(',', vaccines) > 0
THEN REVERSE(SUBSTRING(REVERSE(vaccines),
1,
CHARINDEX(',', REVERSE(vaccines)) - 1))
ELSE vaccines
END AS vax2
FROM(SELECT DISTINCT vaccines FROM Covid_Vaccination_By_Country) AS Vax(vaccines);
However while this is splitting the first entry away reasonably well its not doing anything for subsequent values.
I was trying to come up with the simplest and smallest block of SQL to do this but no luck so far any ideas?
vaccine |
---|
Covaxin, Oxford/AstraZeneca |
EpiVacCorona, Sputnik V |
Johnson&Johnson |
Johnson&Johnson, Moderna, Pfizer/BioNTech |
Moderna, Oxford/AstraZeneca, Pfizer/BioNTech |
Moderna, Oxford/AstraZeneca, Pfizer/BioNTech, Sinopharm/Beijing, Sputnik V |
Oxford/AstraZeneca, Pfizer/BioNTech, Sinopharm/Beijing, Sinopharm/Wuhan, Sputnik V |
Oxford/AstraZeneca, Pfizer/BioNTech, Sinopharm/Beijing, Sputnik V |
Oxford/AstraZeneca, Pfizer/BioNTech, Sputnik V |
Pfizer/BioNTech |
Pfizer/BioNTech, Sinopharm/Beijing |
Sinopharm/Beijing |
Sinopharm/Beijing, Sinopharm/Wuhan, Sinovac |
Sinovac |
Sputnik V |