Skip to content
Advertisement

Split a CSV field into different rows in SQL

A colleague of mines encountered this problem while working on a COBOL program and eventually solved it at the application level. Still I am curious if it is possible to solve it on the data access level, with SQL. This is somehow related to this other question, but I’d like to use only ANSI SQL.

I’m looking for a single SQL select query that acts on a VARCHAR field that contains variable length CSV rows. The purpose of the query is to split every CSV field in its own result set row.

Here is an example with schema and data (and here is the fiddle):

Here is the output I’d like to have from the query:

The CSV separator used is the comma, and for now I wouldn’t worry about escaping.

Advertisement

Answer

As far as I can tell, this is ANSI SQL:

This assumes that the values in field are unique.

Here is a running example

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement