DataSwitch Pro Private, in-browser data converter

How to Convert a Column to a Comma-Separated List (Excel, Sheets, SQL)

By the DataSwitch Pro team · · 4 min read

Quick answer

Paste the column into a comma separator, or use a formula: =TEXTJOIN(", ", TRUE, A2:A100) in Excel 2019, Microsoft 365 or Google Sheets joins every non-empty cell with a comma and a space. For a SQL WHERE … IN list, wrap text values in single quotes, double any apostrophe inside them (O''Brien), and put the whole list in parentheses.

A column is how spreadsheets, CSV exports and query results store a list: one value per row. A comma-separated list is how SQL filters, programming languages and many web forms want the same values: all on one line. Converting between the two comes up constantly — building an IN (…) filter from a list of IDs, pasting tags into a CMS, or turning a client's spreadsheet into an array for a script.

Method 1: the comma separator tool (fastest)

  1. Copy the column from Excel, Google Sheets or a text file.
  2. Paste it into the left box of the Comma Separator.
  3. Choose a preset: SQL IN ( ), JavaScript / JSON array, Python list or a plain comma list.
  4. Copy the result. Duplicates, blank lines and stray spaces are removed on the way.

It also works in reverse: paste a comma-separated list on the right and convert it back to one value per line. Nothing is uploaded — the conversion runs in your browser.

Method 2: Excel and Google Sheets formulas

TEXTJOIN is available in Excel 2019, Excel for Microsoft 365 and Google Sheets. The second argument, TRUE, skips empty cells:

=TEXTJOIN(", ", TRUE, A2:A100)

To produce a quoted list for SQL, add the quotes around the delimiter and at both ends:

="('" & TEXTJOIN("', '", TRUE, A2:A100) & "')"

This simple version does not escape apostrophes, so a value like O'Brien will break the query; wrap the range in SUBSTITUTE(A2:A100, "'", "''") (as an array formula in older Excel) or use the tool, which escapes automatically. Google Sheets also accepts =JOIN(", ", A2:A100), but JOIN keeps empty cells.

Older Excel versions without TEXTJOIN can use =CONCATENATE with a helper column, but for more than a few dozen rows a tool is quicker. Remember that a single Excel cell holds at most 32,767 characters — a very long list may not fit in one cell.

Method 3: let the database build the list

If the values already live in a table, aggregate them in SQL instead of copying them out:

-- PostgreSQL
SELECT string_agg(DISTINCT customer_id::text, ', ') FROM orders;

-- SQL Server 2017+ and Azure SQL
SELECT STRING_AGG(CAST(customer_id AS varchar(20)), ', ') FROM orders;

-- MySQL / MariaDB
SELECT GROUP_CONCAT(DISTINCT customer_id SEPARATOR ', ') FROM orders;

MySQL truncates GROUP_CONCAT output at group_concat_max_len (1,024 bytes by default); raise it with SET SESSION group_concat_max_len = 1000000; before running a long list.

Quoting values correctly for WHERE IN

Numbers can be listed as they are: WHERE id IN (1042, 1043, 1044). Text must be in single quotes, and a single quote inside a value is escaped by doubling it:

WHERE last_name IN ('Smith', 'O''Brien', 'Nguyen')

Be careful with codes that look like numbers. ZIP codes, phone numbers and product codes with leading zeros (02134) should stay quoted; unquoted, the database compares them as the number 2134. If the list comes from user input in an application, use parameterised queries instead of building the SQL string yourself.

Very long lists

Oracle rejects more than 1,000 expressions in one IN list (error ORA-01795). Other databases accept more, but huge literal lists are slow to parse. For thousands of values, load them into a temporary table — the CSV to SQL converter writes the CREATE TABLE and INSERT statements — and join against it. The CSV import guide covers the bulk-load options for each database.

Lists for code

JavaScript, JSON and Python all accept the same form: ["red", "green", "blue"], with double quotes and backslash escapes. Numbers can stay unquoted. If you need the values as a full JSON document rather than a simple list, convert the spreadsheet with CSV to JSON or Excel to JSON instead.

Try it on your own file. Free, private, and processed in your browser.

Frequently asked questions

What is the formula to make a comma-separated list in Excel?

=TEXTJOIN(", ", TRUE, A2:A100) in Excel 2019 or Microsoft 365. Older versions do not have TEXTJOIN; use a tool or a helper column.

How do I add quotes around each value?

Join with "', '" as the delimiter and add ' at both ends, or use the SQL preset of the Comma Separator, which also escapes apostrophes.

How do I remove duplicates from the list?

In Excel 365 wrap the range in UNIQUE(): =TEXTJOIN(", ", TRUE, UNIQUE(A2:A100)). The Comma Separator removes duplicates automatically.

How do I split a comma-separated list back into a column?

Use Data › Text to Columns in Excel (then transpose), =TRANSPOSE(SPLIT(A1, ",")) in Google Sheets, or paste the list into the right box of the Comma Separator and convert it to a column.

More guides