sql >> Databasteknik >  >> RDS >> MariaDB

Hur man trunkerar text med en ellips i MariaDB

Ibland kanske du upptäcker att mängden text som returneras i en databaskolumn är för lång. Du kanske bara vill returnera ett kort stycke av den texten, följt av en ellips eller tre punkter.

Lyckligtvis är detta relativt enkelt att göra i MariaDB.

Tre perioder

Här är ett exempel på att lägga till tre punkter (istället för ett ellipstecken) till en kolumn närhelst antalet tecken i den kolumnen överskrider en viss längd:

SELECT 
    IF(CHAR_LENGTH(ProductDescription) > 32, 
        CONCAT(LEFT(ProductDescription, 32),"..."), 
        ProductDescription) AS "Short Desc",
    ProductDescription AS "Full Desc"
FROM Products;

Resultat:

+-------------------------------------+-----------------------------------------+
| Short Desc                          | Full Desc                               |
+-------------------------------------+-----------------------------------------+
| Purple. Includes left handed car... | Purple. Includes left handed carry box. |
| Blue. Includes right handed carr... | Blue. Includes right handed carry box.  |
| Approximate 45 minute waiting pe... | Approximate 45 minute waiting period.   |
| Approximate 30 minute waiting pe... | Approximate 30 minute waiting period.   |
| Wooden handle. Free wine glasses... | Wooden handle. Free wine glasses.       |
| Orange. Includes spare fingers.     | Orange. Includes spare fingers.         |
| Tied with vines. Very chewable.     | Tied with vines. Very chewable.         |
| Brown ceramic with solid handle.    | Brown ceramic with solid handle.        |
+-------------------------------------+-----------------------------------------+

I det här fallet använder vi CHAR_LENGTH() funktion inuti en IF() funktion för att avgöra om strängen är tillräckligt lång för att motivera förkortning. Vi använder sedan LEFT() funktion inuti CONCAT() funktion för att lägga till några punkter till den korta beskrivningen.

Använda en faktisk ellipsistecken

Och här är den igen, men med en faktisk ellipskaraktär istället för tre punkter:

SELECT 
    IF(CHAR_LENGTH(ProductDescription) > 32, 
        CONCAT(LEFT(ProductDescription, 32),"…"), 
        ProductDescription) AS "Short Desc",
    ProductDescription AS "Full Desc"
FROM Products;

Resultat:

+-------------------------------------+-----------------------------------------+
| Short Desc                          | Full Desc                               |
+-------------------------------------+-----------------------------------------+
| Purple. Includes left handed car…   | Purple. Includes left handed carry box. |
| Blue. Includes right handed carr…   | Blue. Includes right handed carry box.  |
| Approximate 45 minute waiting pe…   | Approximate 45 minute waiting period.   |
| Approximate 30 minute waiting pe…   | Approximate 30 minute waiting period.   |
| Wooden handle. Free wine glasses…   | Wooden handle. Free wine glasses.       |
| Orange. Includes spare fingers.     | Orange. Includes spare fingers.         |
| Tied with vines. Very chewable.     | Tied with vines. Very chewable.         |
| Brown ceramic with solid handle.    | Brown ceramic with solid handle.        |
+-------------------------------------+-----------------------------------------+

Ellipsen använder mindre utrymme. Detta beror på att det är ett enda tecken, till skillnad från de tre prickarna (som är tre separata tecken).

Trunkera ALLA rader, oavsett längd

Om du behöver trunkera alla rader, oavsett deras längd, behöver du inte inkludera IF() funktion.

I det här fallet kan koden förkortas till något i stil med detta:

SELECT 
    CONCAT(LEFT(ProductDescription, 15), '...') AS "Short Desc",
    ProductDescription AS "Full Desc"
FROM Products;

Resultat:

+--------------------+-----------------------------------------+
| Short Desc         | Full Desc                               |
+--------------------+-----------------------------------------+
| Purple. Include... | Purple. Includes left handed carry box. |
| Blue. Includes ... | Blue. Includes right handed carry box.  |
| Approximate 45 ... | Approximate 45 minute waiting period.   |
| Approximate 30 ... | Approximate 30 minute waiting period.   |
| Wooden handle. ... | Wooden handle. Free wine glasses.       |
| Orange. Include... | Orange. Includes spare fingers.         |
| Tied with vines... | Tied with vines. Very chewable.         |
| Brown ceramic w... | Brown ceramic with solid handle.        |
+--------------------+-----------------------------------------+

Uteslut Ellipsen

Och om du inte ens behöver ellipsen/tre menstruationer, kan du till och med förkorta den ytterligare, till något sånt här:

SELECT 
    LEFT(ProductDescription, 15) AS "Short Desc",
    ProductDescription AS "Full Desc"
FROM Products;

Resultat:

+-----------------+-----------------------------------------+
| Short Desc      | Full Desc                               |
+-----------------+-----------------------------------------+
| Purple. Include | Purple. Includes left handed carry box. |
| Blue. Includes  | Blue. Includes right handed carry box.  |
| Approximate 45  | Approximate 45 minute waiting period.   |
| Approximate 30  | Approximate 30 minute waiting period.   |
| Wooden handle.  | Wooden handle. Free wine glasses.       |
| Orange. Include | Orange. Includes spare fingers.         |
| Tied with vines | Tied with vines. Very chewable.         |
| Brown ceramic w | Brown ceramic with solid handle.        |
+-----------------+-----------------------------------------+

  1. ListView-kontroll med Ms-Access TreeView

  2. Kan en främmande nyckel vara NULL och/eller dupliceras?

  3. Hur man laddar ner och installerar SQLite-verktyg

  4. Hur förhindrar jag att en databastrigger återkommer?