Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mysql, Concat on specified length

Tags:

mysql

I was trying to find a way in mysql functions do the the following:

'ssssssttttttttrrrriiiinnnngggg' // I have this string

I want to take only 10 letters then concate '...'(3 dots), But i only want to concat to strings with over 10 letters so for example:

'123456string' // Will result: '123456stri...'
'strin6' // Will result: 'strin6'
'str123456ing' // Will result: 'str123456i...'
'asd23456' // Will result: 'asd23456'

I was trying substr, lpad and concat combinations, If anyone got the correct functions combination i will be very thankful, Thank you all and have a nice day.

like image 753
Aviel Fedida Avatar asked Feb 22 '26 19:02

Aviel Fedida


1 Answers

  select case when length(mystring) <= 10 then 
      mystring 
  else 
      concat(left(mystring, 7), '...') 
  end as mycol
  ...
like image 59
davek Avatar answered Feb 24 '26 09:02

davek



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!