Learn more about SQL Server tools

   
   























Latest from MSSQLTips















MySQL to SQL Server Coding Differences




Overview

There are certain differences between some commonly used SQL code in Microsoft SQL Server's Transact SQL and MySQL.

Explanation

The following code snippets show examples of some commons functions which are different in the two database management systems. The first code is for MySQL and the second code is for MS SQL.

Length of a string:

SELECT CHARACTER_LENGTH(string_data)
FROM TestTable

SELECT LEN(string_data)
FROM TestTable

Concatenation of strings:

SELECT CONCAT('MS','SQL','Tips')

SELECT ('MS' + 'SQL' + 'Tips')

Select first 10 records from a table:

SELECT * FROM TestTable WHERE id=12 LIMIT 10

SELECT TOP 10 * FROM TestTable WHERE id=12

Generate Globally Unique Identifier (GUID):

SELECT UUID()

SELECT NEWID()

Select a random record:

SELECT * FROM TestTable ORDER BY RAND() LIMIT 1

SELECT TOP 1 * FROM TestTable ORDER BY NEWID()

Return current date and time:

SELECT NOW()

SELECT GETDATE()

Auto increment field definition:

TestTable INTEGER AUTO_INCREMENT PRIMARY KEY

TestTable INT IDENTITY PRIMARY KEY

Database version:

SELECT VERSION()

SELECT @@VERSION

Please note that MS SQL does not support the commenting option using #, but -- and /*  */ work the same way.





 
Sponsor Information







 

Follow

Get Free SQL Tips

Twitter

LinkedIn

Google+

Facebook

Pinterest

RSS

Learning

DBAs

Developers

BI Professionals

Careers

Q and A

Today's Tip

Resources

Tutorials

Webcasts

Whitepapers

Tools

Search

Tip Categories

Search By TipID

Authors

Community

First Timer?

Pictures

Free T-shirt

Contribute

Events

User Groups

Author of the Year

More Info

Join

About

Copyright

Privacy

Disclaimer

Feedback

Advertise

Copyright (c) 2006-2015 Edgewood Solutions, LLC All rights reserved
Some names and products listed are the registered trademarks of their respective owners.