MSSQL STR Function and Its Equivalent in MySQL


The STR function in MS SQL Server converts numeric values to character strings. There's no STR function in MySQL, but the same functionality can be achieved using CAST.

MSSQL

Syntax:

STR ( float_expression [ , length [ , decimal ] ] )

Where float_expression is an approximate numeric (float) expression with a decimal point, length is the total length including decimal point, sign, digits, and spaces (default 10), and decimal is the number of places to the right of the decimal point (max 16, truncated if exceeded).

Example — convert 12345.67890 to a string, rounded to 3 decimal places:

select STR(12345.67890,12,3)
--------------------------
 12345.679

STR function in MSSQL

MySQL

In MySQL, the same functionality can be achieved with CAST. If you also want to round the number to N decimal places, use ROUND as well.

mysql> select CAST(12345.67890 as CHAR);
+---------------------------+
| CAST(12345.67890 as CHAR) |
+---------------------------+
| 12345.67890                |
+---------------------------+

CAST function in MySQL

mysql> select cast(round(12345.67890,3) as char);
+-------------------------------------+
| cast(round(12345.67890,3) as char)  |
+-------------------------------------+
| 12345.679                           |
+-------------------------------------+

MSSQL STR function equivalent in MySQL

Back to Converting Functions from MSSQL to MySQL