0

My subject field is (maybe) bigger than 100 characters. I want to use LENGTH if subject length is bigger that 100 char in below mysql command and attach ... to end of SUBSTR subject.

SELECT id ,
     IF LENGTH(`subject`) <=100 then SUBSTR( `subject`, 1, 100 ) AS subject
     ELSE `subject`
     END IF
FROM `contents`
1

2 Answers 2

2

You might be looking for CONCAT function in MySQL.

SELECT id ,
     CASE WHEN LENGTH(`subject`) >=100 then CONCAT(SUBSTR( `subject`, 1, 100 ),'...')
     ELSE `subject`
     END AS `subject`
FROM `contents`

Sample fiddle

Have a look here as well.

Sign up to request clarification or add additional context in comments.

2 Comments

i get this error:#1064 - You have an error in your SQL syntax;check the manual that corresponds to your MySQL server version for the right syntax to use near 'LENGTH(subject) <=100 then CONCAT(SUBSTR( subject, 1, 100 ),'...') AS subjec' at line 2
thanks i get this error error:#1064 - You have an error in your SQL syntax;check the manual that corresponds to your MySQL server version for the right syntax to use near 'LENGTH(subject) <=100 then CONCAT(SUBSTR( subject, 1, 100 ),'...') AS subjec at line 2
0

Rather another simpler way could be you can fetch the subject using your simple mysql query. and can display your subject this way!!

For ex: $subject = substr($data['subject'], 1, 100)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.