SQL Server to MySQL Converter: Syntax Guide & Examples
A complete reference for converting SQL Server (T-SQL) queries to MySQL. Covers TOP to LIMIT, IDENTITY to AUTO_INCREMENT, NVARCHAR handling, and every major syntax difference.
Why Convert SQL Server to MySQL?
Teams migrate from SQL Server to MySQL to reduce licensing costs, move to open-source infrastructure, or adopt platforms that run on MySQL such as WordPress, Shopify integrations, or PlanetScale. MySQL is also the default for many cloud-native stacks and has a massive ecosystem of tools and hosting providers.
MySQL's simpler licensing model (GPL or commercial) eliminates per-core or per-CAL costs that SQL Server requires. For startups and small teams, this cost savings is significant. MySQL also offers excellent read performance for web applications with simple query patterns.
The main challenges are converting T-SQL-specific syntax like TOP, IDENTITY, ISNULL, GETDATE(), and stored procedures. This guide covers every difference.
Key Syntax Differences
| Feature | SQL Server | MySQL |
|---|---|---|
| Row limiting | SELECT TOP 10 | LIMIT 10 |
| Pagination | OFFSET ... FETCH NEXT | LIMIT ... OFFSET |
| Auto-increment | IDENTITY(1,1) | AUTO_INCREMENT |
| Identifier quoting | [brackets] | `backticks` |
| String concatenation | a + b | CONCAT(a, b) |
| NULL replacement | ISNULL(a, b) | IFNULL(a, b) |
| Current datetime | GETDATE() | NOW() |
| Boolean type | BIT | TINYINT(1) |
| Conditional | IIF(cond, a, b) | IF(cond, a, b) |
| String aggregation | STRING_AGG(col, ',') | GROUP_CONCAT(col) |
Common Conversions with Examples
TOP to LIMIT
-- SQL Server
SELECT TOP 10 * FROM products ORDER BY price DESC;
SELECT * FROM products
ORDER BY price DESC
OFFSET 20 ROWS FETCH NEXT 10 ROWS ONLY;
-- MySQL
SELECT * FROM products ORDER BY price DESC LIMIT 10;
SELECT * FROM products ORDER BY price DESC LIMIT 10 OFFSET 20;
IDENTITY to AUTO_INCREMENT
-- SQL Server
CREATE TABLE users (
id INT IDENTITY(1,1) PRIMARY KEY,
name NVARCHAR(100) NOT NULL
);
-- MySQL
CREATE TABLE users (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100) NOT NULL
) CHARACTER SET utf8mb4;
Date Function Differences
-- SQL Server
SELECT GETDATE();
SELECT DATEADD(DAY, 30, created_at) FROM orders;
SELECT DATEDIFF(DAY, start_date, end_date) FROM projects;
SELECT FORMAT(created_at, 'yyyy-MM') FROM orders;
-- MySQL
SELECT NOW();
SELECT DATE_ADD(created_at, INTERVAL 30 DAY) FROM orders;
SELECT DATEDIFF(end_date, start_date) FROM projects;
SELECT DATE_FORMAT(created_at, '%Y-%m') FROM orders;
ISNULL to IFNULL
-- SQL Server
SELECT ISNULL(nickname, name) AS display_name FROM users;
SELECT IIF(age >= 18, 'Adult', 'Minor') AS category FROM users;
-- MySQL
SELECT IFNULL(nickname, name) AS display_name FROM users;
SELECT IF(age >= 18, 'Adult', 'Minor') AS category FROM users;
String Concatenation
-- SQL Server (+ operator)
SELECT first_name + ' ' + last_name AS full_name FROM users;
-- MySQL (CONCAT function)
SELECT CONCAT(first_name, ' ', last_name) AS full_name FROM users;
In SQL Server, the + operator returns NULL if any operand is NULL. MySQL's CONCAT also returns NULL if any argument is NULL, but you can use CONCAT_WS (with separator) which skips NULLs.
STRING_AGG to GROUP_CONCAT
-- SQL Server 2017+
SELECT department, STRING_AGG(name, ', ') WITHIN GROUP (ORDER BY name)
FROM employees
GROUP BY department;
-- MySQL
SELECT department, GROUP_CONCAT(name ORDER BY name SEPARATOR ', ')
FROM employees
GROUP BY department;
Data Type Mapping
| SQL Server Type | MySQL Type | Notes |
|---|---|---|
BIT | TINYINT(1) | Boolean |
INT | INT | Same |
BIGINT | BIGINT | Same |
FLOAT | DOUBLE | 8-byte float |
NVARCHAR(n) | VARCHAR(n) | Use utf8mb4 charset |
NVARCHAR(MAX) | LONGTEXT | Up to 4GB |
VARBINARY(MAX) | LONGBLOB | Binary data |
DATETIME2 | DATETIME(6) | Microsecond precision |
DATETIMEOFFSET | TIMESTAMP | MySQL TIMESTAMP stores UTC |
MONEY | DECIMAL(19,4) | No MONEY type in MySQL |
UNIQUEIDENTIFIER | CHAR(36) | Store UUID as string |
XML | TEXT | No native XML type in MySQL |
Step-by-Step Migration Guide
- Export the schema. Use SQL Server Management Studio to script out the database objects. Select all tables, views, and stored procedures.
- Convert data types. Replace
NVARCHARwithVARCHAR(with utf8mb4),BITwithTINYINT(1),DATETIME2withDATETIME(6),IDENTITYwithAUTO_INCREMENT, and[brackets]with backticks. - Export data. Use BCP (Bulk Copy Program) to export as CSV, then import with MySQL's
LOAD DATA INFILE. For smaller databases, generate INSERT statements. - Convert queries. Replace
TOPwithLIMIT,ISNULLwithIFNULL,GETDATE()withNOW(),DATEADDwithDATE_ADD, andIIFwithIF. - Rewrite stored procedures. T-SQL procedures must be rewritten in MySQL's procedure syntax. Key differences:
DECLARE @varbecomesDECLARE var,TRY...CATCHbecomesDECLARE HANDLER, andBEGIN TRANSACTIONbecomesSTART TRANSACTION. - Test and validate. Run your application against MySQL and compare outputs. Pay attention to string collation, date rounding, and NULL behavior differences.
For SQL fundamentals, see our What is SQL guide. For query examples, visit SQL Query Examples.
Convert Automatically with AI2SQL
Stop translating T-SQL syntax manually. Paste your SQL Server query into the SQL Converter tool and get the correct MySQL query with LIMIT, IFNULL, NOW(), and proper data types automatically.
Try the converter free. See also: MySQL to SQL Server. Browse all converter tools.
Frequently Asked Questions
How do I convert SQL Server TOP to MySQL?
Replace SELECT TOP N with LIMIT N at the end of the query. SQL Server: SELECT TOP 10 * FROM users ORDER BY id. MySQL: SELECT * FROM users ORDER BY id LIMIT 10. For OFFSET-FETCH, convert to LIMIT with OFFSET: OFFSET 20 ROWS FETCH NEXT 10 ROWS ONLY becomes LIMIT 10 OFFSET 20.
How do I convert SQL Server IDENTITY to MySQL?
Replace IDENTITY(1,1) with AUTO_INCREMENT. SQL Server: id INT IDENTITY(1,1) PRIMARY KEY. MySQL: id INT AUTO_INCREMENT PRIMARY KEY. If the IDENTITY has a non-default seed, set the starting value: ALTER TABLE users AUTO_INCREMENT = 1000.
What happens to NVARCHAR columns when converting to MySQL?
NVARCHAR stores Unicode text in SQL Server. MySQL's VARCHAR with utf8mb4 character set handles the same Unicode data. Replace NVARCHAR(n) with VARCHAR(n) and ensure your MySQL table uses CHARACTER SET utf8mb4. NVARCHAR(MAX) maps to LONGTEXT in MySQL.