Yes, you can turn rows in an Excel spreadsheet into SQL INSERT statements without any add-ins: you build a formula in an empty column that concatenates your values into a single SQL string, then fill it down and copy the results into your database tool. This is the fastest reliable way to migrate a spreadsheet of data into MySQL, SQL Server, PostgreSQL, or SQLite when you only need a one-off import and do not want to deal with CSV import wizards.
What You'll Need
- Excel (desktop version recommended; the steps work in Excel for Microsoft 365, Excel 2019/2021, and Excel for the web)
- A spreadsheet where each row is one record and the first row holds column headers
- Access to your database through a tool such as MySQL Workbench, SSMS, pgAdmin, or a command-line client
- The exact table and column names in your target database
Step-by-Step
- Clean your data first. Remove empty rows, merge or split cells so every record sits on exactly one row, and make sure dates and numbers are consistent. Fixing data after import is far more painful than fixing it before.
- Confirm the target table. Write down the table name and the ordered list of columns you will insert, for example: customers (first_name, last_name, email, signup_date).
- Add a helper column. In the first empty column to the right of your data, give the header cell a name like sql.
- Build the INSERT formula. Assuming your data starts in row 2 with first names in column A, last names in B, emails in C, and dates in D, type this into the helper cell in row 2:
="INSERT INTO customers (first_name, last_name, email, signup_date) VALUES ('" & A2 & "', '" & B2 & "', '" & C2 & "', '" & TEXT(D2,"yyyy-mm-dd") & "');"
This produces a complete statement like: INSERT INTO customers (first_name, last_name, email, signup_date) VALUES ('Anna','Smith','anna@example.com','2024-03-14'); - Handle numbers without quotes. Numeric columns must not be wrapped in single quotes. For a numeric column, use plain concatenation:
& E2 &instead of'" & E2 & "'. - Escape single quotes in text. Names like O'Brien will break the SQL. Wrap text values with a substitute:
SUBSTITUTE(A2,"'","''")inside the concatenation so any apostrophe becomes two apostrophes, which SQL reads as a literal quote. - Fill the formula down. Select the helper cell, then double-click the small square fill handle at the cell's bottom-right corner to copy the formula to every row. Alternatively drag it down or press Ctrl+D after selecting the range.
- Copy the generated statements. Select all the formula cells, copy them (Ctrl+C), then in your SQL client paste the results. Each cell contains one complete INSERT statement ending in a semicolon.
- Run in batches. For large imports, paste a few hundred statements at a time rather than thousands at once. Very large pastes can choke the client or the undo history.
- Verify the import. Run a quick check such as
SELECT COUNT(*) FROM customers;and spot-check a few rows against the spreadsheet.
Menu names and dialog wording can differ slightly between Excel versions and between Windows and Mac, but the formulas above behave identically everywhere, including Excel for the web.
Common Mistakes
- Quoting numeric values. Putting quotes around a number sometimes works but can cause type errors or silent conversions depending on the database. Leave numbers unquoted.
- Forgetting to escape apostrophes. One stray apostrophe in a text field will throw a syntax error and can abort the whole batch.
- Wrong date format. Excel dates displayed as 3/14/2024 will not be accepted by most databases. Always format dates as yyyy-mm-dd with TEXT(), as shown above.
- Trailing spaces. Copy-pasted data often carries invisible leading or trailing spaces. Wrap text values with TRIM() in the formula if this is a risk.
- Inserting into the wrong column order. The values in your formula must match the column list exactly. If you reorder columns in the sheet, rebuild the formula.
- Not checking for an auto-increment primary key. If your table has an auto-numbered ID column, leave it out of both the column list and the values.
Tips
- Build the formula in row 2 and test the generated statement against your database before filling it down, so you fix quoting problems once instead of after generating thousands of rows.
- Use Excel's TEXT() function for any column that must be formatted specifically, such as currency or ISO timestamps:
TEXT(E2,"yyyy-mm-dd hh:mm:ss"). - For NULL values, use a conditional:
IF(D2="","NULL","'" & TEXT(D2,"yyyy-mm-dd") & "'")so empty cells become SQL NULL instead of an empty string. - If you do this regularly, consider the proper alternative: export the sheet as CSV and use your database's import tool (the MySQL LOAD DATA statement, SQL Server Import and Export Wizard, or PostgreSQL's COPY command). It is faster and safer for large datasets.
- Paste the generated SQL into a plain-text editor first if your SQL client mangles line breaks; each cell should paste as one line.
Troubleshooting
- Syntax error near a value. Almost always an unescaped apostrophe. Add the SUBSTITUTE() wrapper to that column.
- Dates import as numbers like 45365. Excel stored the date as a serial number and you concatenated the raw cell. Wrap it in TEXT() with the format shown above.
- Formula shows as text instead of a result. The cell is formatted as Text. Change the cell format to General (Home tab, Number group), then re-enter the formula.
- Only the first statement runs. Your SQL client may execute only the selected statement. Select all pasted statements, or enable the option to run the whole script (wording varies by tool).
- Duplicate key errors. The table already contains some of the rows, or your spreadsheet has duplicates. Remove duplicates in Excel (Data tab, Remove Duplicates) or decide whether to use your database's upsert syntax instead.
- Nothing pastes into the SQL client. Some clients block large pastes. Break the statements into smaller batches, or save the copied cells to a .sql file via a text editor and open it in the client.
Frequently Asked Questions
Can Excel run SQL queries directly?
Excel can run queries against external databases through its built-in query tools (Data tab, Get Data), but those are mainly for pulling data into Excel, not for generating INSERT statements. The formula method above is the simplest way to produce INSERTs from sheet data.
Is there an Excel function that generates SQL automatically?
No built-in function does this. CONCAT or CONCATENATE with the pattern shown above is the standard approach, since it gives you full control over quoting and formatting.
Does this work with Google Sheets too?
Yes. The same formulas, including TEXT() and SUBSTITUTE(), work in Google Sheets, so you can follow these steps there if you do not have Excel.
What is the safest way to import a very large spreadsheet?
Skip the formula approach and use your database's CSV import mechanism instead, such as LOAD DATA in MySQL or the COPY command in PostgreSQL. Generated INSERT statements are best for hundreds to a few thousand rows.
Why do I need two apostrophes instead of one?
In SQL, a single quote ends a text string. Writing two apostrophes in a row is the standard way to include a literal apostrophe inside a string, so 'O''Brien' is stored as O'Brien.
Can I generate UPDATE statements the same way?
Yes. The same concatenation technique works for any SQL statement: build the fixed text around your cell references, being sure to include a correct WHERE clause that references a unique key column.