Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add hyphen in a string after every 2 characters using in mysql select statement

Tags:

sql

mysql

I have a mysql table with some data. In that i have a column(mobile_number) and that column value is like mobile number (ex:1234587920).

my expected out put is : 12-34-58-79-20

I want to add hyphen after every two numbers.

like image 503
Madhu Ch Avatar asked Oct 24 '25 09:10

Madhu Ch


1 Answers

use concat() for joining strings and use substr() for split a string in mysql :

split (column,start,count) select column from start position for count charachter

concat(column1,"-",column2) will be column1-column2

SELECT CONCAT(SUBSTR(phone,1,2),'-',SUBSTR(phone,3,2),'-',SUBSTR(phone,5,2),'- 
',SUBSTR(phone,7,2),'-',SUBSTR(phone,9,2)) FROM `table`
like image 94
esnkrimi Avatar answered Oct 26 '25 00:10

esnkrimi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!