Importing Excel Data into SQL Server Without Losing Its Meaning

Image retained from the original article.

Originally published 27 July 2023; rewritten and reviewed on 15 October 2026.

The quickest way to corrupt data in SQL Server is to trust Excel’s automatic type guessing. Everything looks green after the import, and then someone notices that Selangor postcodes like 08000 turned into 8000, Malaysian phone numbers like 0123456789 lost their leading zero, and 04/07/2023 (4 July) became 7 April.

Getting rows from .xls or .xlsx into SQL Server is easy. Getting them in with their actual meaning intact requires choosing the right ingestion path and staging everything as text before touching your live tables.

Why OPENROWSET for .xlsx usually causes headaches

You will often see old T-SQL tutorials suggesting OPENROWSET('Microsoft.ACE.OLEDB.12.0', ...) to query an .xlsx file straight from SQL Server. In practice, that approach breaks in modern environments for three reasons:

  1. Driver and bitness mismatches: 64-bit SQL Server needs the 64-bit Access Database Engine (ACE.OLEDB) installed on the database server itself, and on Linux-hosted SQL Server it isn’t available at all.
  2. Security surface: It requires enabling Ad Hoc Distributed Queries via sp_configure, which many DBAs rightly block.
  3. Silent type inference: The Excel OLE DB driver scans only the first 8 rows (TypeGuessRows) to decide a column’s type. If the first 8 rows happen to be numbers and row 9 contains an alphanumeric code like INV-009, row 9 quietly imports as NULL.

If this is a one-off ad-hoc import on Windows, the SQL Server Import and Export Wizard works fine—just click Edit Mappings and force ID, phone, and postcode columns to nvarchar instead of accepting the defaults. For anything repeatable or automated, export the sheet to UTF-8 CSV first (via Excel, LibreOffice, or a small C#/Python script using ClosedXML or openpyxl) and use BULK INSERT.

Stage everything as NVARCHAR first

Never BULK INSERT directly into a production table with strict DATE, INT, or DECIMAL columns. One stray "N/A" or "RM 1,250.00" in a numeric column will abort the batch or leave you hunting for line numbers.

Create a staging table where every column is NVARCHAR, then load the CSV:

CREATE TABLE dbo.CustomerImportStage (
    CustomerCode NVARCHAR(50),
    FullName     NVARCHAR(200),
    Phone        NVARCHAR(50),
    JoinedDate   NVARCHAR(50),
    CreditLimit  NVARCHAR(50)
);

BULK INSERT dbo.CustomerImportStage
FROM 'C:\Imports\customers.csv'
WITH (
    FORMAT = 'CSV',
    FIRSTROW = 2,
    FIELDQUOTE = '"',
    CODEPAGE = '65001',
    ROWTERMINATOR = '0x0a',
    ERRORFILE = 'C:\Imports\customers_errors.log'
);

A few details worth checking on your server:

  • FORMAT = 'CSV' (available from SQL Server 2017 onwards) properly handles quoted commas like "Kuala Lumpur, MY".
  • CODEPAGE = '65001' tells SQL Server the file is UTF-8. Make sure Excel didn’t save it as “CSV (Macintosh)” or Windows-1252, or accented and non-Latin names will turn into ? or garbled bytes.
  • The path C:\Imports\customers.csv is resolved from the SQL Server machine’s filesystem, not your laptop, and the SQL Server service account (NT Service\MSSQLSERVER or similar) needs read permission on that folder.

Validate and transform before promoting to live tables

Once the rows sit in dbo.CustomerImportStage, you can inspect anomalies with plain SQL before inserting into dbo.Customers:

-- Check for rows that would fail date or decimal conversion
SELECT *
FROM dbo.CustomerImportStage
WHERE TRY_CONVERT(DATE, LTRIM(RTRIM(JoinedDate)), 103) IS NULL
   OR TRY_CAST(REPLACE(REPLACE(CreditLimit, 'RM', ''), ',', '') AS DECIMAL(18, 2)) IS NULL;

Notice style 103 in TRY_CONVERT(DATE, ..., 103)—that explicitly parses dd/mm/yyyy (the standard format used in Malaysia and the UK) instead of letting SQL Server’s session language guess mm/dd/yyyy.

When every row passes validation, promote the clean data inside a transaction and reconcile the row count against your source file:

BEGIN TRANSACTION;

INSERT INTO dbo.Customers (CustomerCode, FullName, Phone, JoinedDate, CreditLimit)
SELECT
    LTRIM(RTRIM(CustomerCode)),
    LTRIM(RTRIM(FullName)),
    LTRIM(RTRIM(Phone)),
    TRY_CONVERT(DATE, LTRIM(RTRIM(JoinedDate)), 103),
    TRY_CAST(REPLACE(REPLACE(CreditLimit, 'RM', ''), ',', '') AS DECIMAL(18, 2))
FROM dbo.CustomerImportStage
WHERE CustomerCode IS NOT NULL;

COMMIT TRANSACTION;

Keep the original .xlsx and .csv archived with a timestamp or batch ID. When a finance user asks three weeks later why a figure looks off, having the exact source file and staging log saves hours of guesswork.