Yes, you can turn Excel data into SQL INSERT statements without any special software: add a formula in an empty column that concatenates your values into a SQL string, then copy the formula down and paste the results into your database tool. This is the fastest reliable method and works in any version of Excel. Below is the full process, plus what to check before you run the generated SQL.
What You'll Need
- Excel (any recent version; the steps are the same on Windows and Mac)
- Your data arranged with headers in row 1, one record per row
- A database tool to run the finished SQL, such as MySQL Workbench, SQL Server Management Studio, pgAdmin, or a command-line client
- The exact column names and types of the target table
Step-by-Step
- Clean your data first. Remove empty rows, merge duplicates, and make sure each column contains only one kind of value. Fix dates so they are real dates, and check for stray spaces in text cells.
- Match your columns to the table. Put the columns in your sheet in the same order as the columns you will insert into, or keep a written list of the mapping. Note which columns are numbers (no quotes in SQL) and which are text or dates (need quotes).
- Add a helper column. In the first empty column after your data, click the cell in row 2 (the first data row) and enter a CONCATENATE-style formula. Suppose columns A to C hold an ID (number), a name (text), and a hire date (date). In D2 type:
="INSERT INTO employees (id, name, hire_date) VALUES ("&A2&", '"&SUBSTITUTE(B2,"'","''")&"', '"&TEXT(C2,"yyyy-mm-dd")&"');" - Press Enter and check the output. Cell D2 should show a complete statement, for example:
INSERT INTO employees (id, name, hire_date) VALUES (1, 'Anna Smith', '2023-04-15');If the text is wrong, fix the formula before copying it. - Copy the formula down. Select D2, then double-click the small square fill handle in the cell's bottom-right corner. The formula fills down alongside all your data rows.
- Copy the results as text. Select all the generated cells in column D, copy them (Ctrl+C), then in your SQL editor paste (Ctrl+V). Each row is one INSERT statement ending in a semicolon.
- Test on a few rows first. Before running the whole batch, highlight only the first two or three statements and execute those. Check the table (with a SELECT) to confirm the values landed correctly, especially dates and any text with apostrophes.
- Run the full batch. If the test rows look right, execute the remaining statements. Commit the transaction if your tool requires an explicit COMMIT.
- Remove the helper column. Delete column D from your sheet so the formulas do not get confused with real data later.
Notes on the formula: the SUBSTITUTE part doubles any single quote inside a text value (O'Brien becomes O''Brien), which is how SQL escapes apostrophes. TEXT(C2,"yyyy-mm-dd") forces dates into ISO format that every major database accepts. On a Mac the keystrokes are Command instead of Ctrl; the formula syntax is identical.
Common Mistakes
- Forgetting quotes around text and date values, which causes a syntax error or, worse, treats the value as a column name.
- Leaving quotes around numeric values that go into number columns. Some databases accept it, others do not, so keep numbers unquoted.
- Not escaping apostrophes in names and addresses. One O'Brien will break the whole statement.
- Date formats. Excel may show 15/04/2023 while the database expects 2023-04-15. Always format dates in the formula.
- Inserting into an auto-increment (identity) column. If the ID is generated by the database, leave that column out of both the column list and the VALUES list.
- Pasting formulas instead of values into the SQL editor. Copy the cells, not the formula bar text.
Tips
- Use TEXTJOIN if you have many columns and the CONCATENATE string gets unreadable.
- For very large loads (tens of thousands of rows), skip INSERT queries entirely and use your database's bulk import tool, such as MySQL's LOAD DATA, SQL Server's Import and Export Wizard, or PostgreSQL's COPY command. Save the sheet as a CSV first.
- Add a WHERE NOT EXISTS clause or a unique constraint check if you might run the batch twice and do not want duplicates.
- Wrap the batch in a transaction (BEGIN ... COMMIT) where your database supports it, so a failure partway through does not leave half the rows inserted.
- Keep a copy of the original sheet before you start editing; generated SQL is disposable, your source data is not.
Troubleshooting
- Syntax error near a value: almost always an unescaped apostrophe or a missing quote. Look at the exact row the error names and compare it to a working row.
- Dates insert as NULL or 0000-00-00: the TEXT format string was not applied, or the source cell is text rather than a real date. Re-enter the date in Excel, then regenerate the formula.
- Formula shows the formula text, not the result: the cell is formatted as Text. Change the cell format to General and re-enter the formula.
- Duplicate key errors: rows with the same primary key already exist in the table. Either skip those statements or delete the existing rows first if that is intended.
- Only some rows imported: some tools stop at the first error. Fix that statement, then run the remaining ones, or enable the continue-on-error option if your client has one.
Frequently Asked Questions
Can Excel generate INSERT statements without formulas?
Not natively. The concatenation formula method is the built-in way. Alternatives are exporting a CSV and using your database's import tool, or a small script in Python or PowerShell.
Does this work for MySQL, SQL Server and PostgreSQL?
Yes. The INSERT syntax used here is standard SQL and works in all three, plus SQLite and most other databases. Only the bulk-import alternatives differ per system.
How do I insert multiple rows in one statement?
Change the formula to output just the bracketed values group, add a comma between rows, and write the INSERT INTO ... part once at the top. Most databases accept multi-row VALUES lists.
What about empty cells?
An empty cell becomes an empty string in the formula. If the column should be NULL, wrap that part in an IF, for example IF(E2="","NULL","'"&E2&"'").
Is it safe to run generated SQL on a production database?
Test on a development or staging copy first, back up the target table, and run the batch inside a transaction so you can roll back if something goes wrong.