There are many functions that are included in both MySQL and SQL Server. However, just because both DBMSs share functions of the same name, doesn’t mean that those functions work exactly the same way.
Take CHAR()
for example. Both MySQL and SQL Server include this function. Well technically, for SQL Server it’s actually a T-SQL function, but that’s beside the point. The point is that the MySQL CHAR()
function provides more functionality than the T-SQL/SQL Server CHAR()
function.
In particular, the MySQL version accepts multiple integers, whereas, the T-SQL version only accepts a single integer. The MySQL version also accepts a USING
clause that allows you to specify which character set to use (the T-SQL version doesn’t have this clause).
Below are some examples of these differences.
Multiple Integers
Here’s what happens if we supply multiple integers when using MySQL:
SELECT CHAR(67, 97, 116) AS 'MySQL Result';
Result:
+--------------+ | MySQL Result | +--------------+ | Cat | +--------------+
So it successfully interprets each integer and returns the results.
Here’s what happens if we do the same thing using SQL Server:
SELECT CHAR(67, 97, 116) AS 'SQL Server Result';
Result:
The char function requires 1 argument(s).
To achieve the same outcome as the MySQL example, we’d need to concatenate multiple CHAR()
functions. Something like this:
SELECT CHAR(67) + CHAR(97) + CHAR(116) AS 'Concatenated Result';
Result:
+-----------------------+ | Concatenated Result | |-----------------------| | Cat | +-----------------------+
The USING
Clause
The MySQL version of CHAR()
accepts a USING
clause, whereas the T-SQL version doesn’t. Here’s an example where I specify the unicode code point using MySQL:
SELECT CHAR(0x027FE USING ucs2) AS 'MySQL Result';
Result:
+--------------+ | MySQL Result | +--------------+ | ⟾ | +--------------+
And here’s what happens when I switch to SQL Server:
SELECT CHAR(0x027FE USING ucs2) AS 'SQL Server Result';
Result:
Incorrect syntax near 'USING'.
Example 3 – Multiple Result Bytes
In MySQL, arguments larger than 255 are automatically converted into multiple result bytes. In SQL Server, arguments larger than 255 return NULL
.
Here’s an example using MySQL:
SELECT HEX(CHAR(1799)) AS 'MySQL Result';
Result:
+--------------+ | MySQL Result | +--------------+ | 0707 | +--------------+
And here, we try to do the same thing in SQL Server:
SELECT CONVERT(VARCHAR(1000), CHAR(1799), 2) AS 'SQL Server Result';
Result:
+---------------------+ | SQL Server Result | |---------------------| | NULL | +---------------------+