SQL: Microsoft SQL Server vs MySQL Syntax

Concatenation of strings:

SQL Server

SELECT ('This ' + 'is ' + 'SQL Server')

MySQL

SELECT CONCAT('This ', 'is ', 'MySQL')

Get length of a string:

SQL Server

SELECT LEN(string) FROM dbo.Table

MySQL

SELECT CHARACTER_LENGTH(string) FROM Table

Limit number of records returned:

SQL Server

SELECT TOP 10 * FROM Table

MySQL

SELECT * FROM Table LIMIT 10

Select a random record:

SQL Server

SELECT TOP 1 * FROM Table ORDER BY NEWID()

MySQL

SELECT * FROM Table ORDER BY RAND() LIMIT 1

Get a unique identifier (GUID):

SQL Server

SELECT NEWID()

MySQL

SELECT UUID()

Get the current date and time:

SQL Server

SELECT GETDATE()

MySQL

SELECT NOW()

Identity (auto increment) column definition:

SQL Server

IDField INT IDENTITY PRIMARY KEY

MySQL

IDField INTEGER AUTO_INCREMENT PRIMARY KEY

Get the version of SQL Server/MySQL:

SQL Server

SELECT @@VERSION

MySQL

SELECT VERSION()

Update query (using joins):

SQL Server

UPDATE t1 SET t1.field = t2.field FROM table1 t1 JOIN table2 t2
ON t1.field1 = t2.field1 WHERE t1.field = 'value'

MySQL

UPDATE table1 t1 JOIN table2 t2 ON t1.field1 = t2.field1
SET t1.field = t2.field WHERE t1.field = 'value'

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.