String truncate on length, but no chopping up of words allowed

后端 未结 8 1689
旧巷少年郎
旧巷少年郎 2020-12-15 19:33

I want to limit a string field length in MYSQL on a certain length, but I don\'t want any chopping up of words to occur.

When I do:

SELECT SUBSTRING(         


        
8条回答
  •  借酒劲吻你
    2020-12-15 20:08

    Seems like people don't read the mysql manual:

    Original: SELECT SUBSTRING('Business Analist met focus op wet- en regelgeving', 1, 28) gives broken words.

    Modified: SELECT SUBSTRING_INDEX('Business Analist met focus op wet- en regelgeving', ' ' , 4) gives unbroken words

    SUBSTRING_INDEX(string, delimiter, number) will truncate a string by the number of times delimiter is found. Make your delimiter a space and you will get whole words only. so:

    SUBSTRING_INDEX( LEFT('Business Analist met focus op wet- en regelgeving',28), ' ' , 4) should do it.

提交回复
热议问题