SmartQueryTools

Coalesce Columns in CSV Files Online

Fill null values in CSV files from other columns — return the first non-null value across up to 8 columns in priority order. Add the result as a new column. Runs entirely in your browser.

How to coalesce Columns in CSV files

  1. Drop your file onto the upload area. The first two columns are placed in slots 1 and 2 as a starting point, and the first 200 rows are shown.
  2. Set the columns in priority order. Slot 1 is checked first, then slot 2, and so on. Use "+ Add column" for up to 8 columns, or the cross to remove one.
  3. Name the output column. The default is "coalesced".
  4. Click Coalesce Columns. The new column is added at the end and every original column is kept.
  5. Check the preview and download the file in the same format you uploaded.

Your file is processed locally in your browser and is never uploaded. The free limit is 50 MB per file; larger files work if your device has the memory for them.

Worked example

A sports club membership list has three phone columns filled in unevenly. The coach needs one phone number per member for a text alert, preferring mobile, then work, then home.

Input (CSV)

member_id,mobile,work_phone,home_phone
M-01,021 555 0142,,09 555 7710
M-02,,04 555 3301,04 555 9120
M-03,,,03 555 6604
M-04,,,

Settings

  • Columns (in priority order): 1. mobile, 2. work_phone, 3. home_phone
  • Output column name: contact_phone

Result

member_idmobilework_phonehome_phonecontact_phone
M-01021 555 0142NULL09 555 7710021 555 0142
M-02NULL04 555 330104 555 912004 555 3301
M-03NULLNULL03 555 660403 555 6604
M-04NULLNULLNULLNULL

Each row takes the first phone number that is present, checking mobile, then work_phone, then home_phone. M-01 has a home number too, but mobile wins because it is first. M-02 has no mobile, so the work number is used. M-04 has no numbers at all, so contact_phone is empty. The three source columns are left as they were.

Working with CSV files

In a CSV, an empty field between two commas loads as NULL, which is exactly what COALESCE skips. A field holding a single space, "N/A", "-" or "null" as text is a real value, and it will win over later columns. If your export uses placeholders like these, run Find & Replace or Trim Whitespace on the source columns first so they become truly empty.

Type inference matters more than people expect. A phone column written as 0215550142 loads as text because of the leading zero, while a column of plain 7-digit numbers may load as an integer. Mixing a text column and a number column in one chain fails with "Cannot mix values of type VARCHAR and BIGINT in COALESCE". Cast the columns to the same type with Cast Column Types first. The CSV you download has the new column as the last field in the header. The winning value is copied exactly as it loaded, spaces and punctuation included, so "021 555 0142" stays in that form. Normalise formats first if the sources write the same kind of value differently.

Frequently Asked Questions

Why does coalesce not skip cells that say N/A in my CSV?

Only empty fields are NULL. Text such as "N/A", "null" or a single space is a real value. Replace those placeholders with empty values first.

Why do I get a "Cannot mix values" error on my CSV?

The columns in the chain loaded with different types, for example one as text and one as a number. Use Cast Column Types to give them the same type, then coalesce.

How many columns can I coalesce at once?

Between 2 and 8. The button stays disabled until at least two slots have a column picked.

Does coalesce change the original columns?

No. It adds one new column at the end and leaves every original column untouched. Drop the source columns afterwards with Manage Columns if you no longer need them.

Can I put a default value at the end of the chain?

Not in this tool, which only accepts columns. Run Fill Empty Values on the coalesced column afterwards to replace the remaining empty cells with a fixed value such as "unknown".

Related Tools