Data Loader

REPLICATE function in MS SQL and its equivalent in MySQL

The REPLICATE function returns a string by replicating a string value a specified number of times. This function is usually used to format outputs.

In MySQL the same functionality is provided by REPEAT function. Here the examples of both MSSQL and MySQL

MSSQL

Syntax

REPLICATE ( string_expression ,integer_expression )   

Example

select REPLICATE('0',4);
--------------
0000
MSSQL Replicate funciton
select REPLICATE('0',5-datalength(rtrim(productid)))+CAST(productid as varchar) ,ProductID 
from Production.Product order by ProductID 
(No column name) ProductID
00001 			1
00002 			2
00003			3
00004			4
00316 			316
00317 			317
00318 			318
REPLICATE Example MSSQL
 

MySQL

In the MySQL the REPEAT function offers the same results. Here is the example

Syntax

REPEAT(str,count)

Example

mysql> select repeat('#',4);
+---------------+
| repeat('#',4) |
+---------------+
| #### |
+---------------+
1 row in set (0.00 sec)

mysql>
MySQL REPEAT function
 

Assuming that same PRODUCT table is existing in MySQL also, then giving the following query gives the following result

select concat(repeat('0',5-char_length(productid)),productid) from product
MySQL REPEAT function Example2
 

Back to Converting Functions from MSSQL to MySQL