sql >> Databáze >  >> RDS >> MariaDB

Jak zkrátit text pomocí elipsy v MariaDB

Někdy můžete zjistit, že množství textu vráceného ve sloupci databáze je příliš dlouhé. Možná budete chtít vrátit krátký úryvek tohoto textu následovaný třemi tečkami nebo třemi tečkami.

Naštěstí je to v MariaDB poměrně snadné.

Tři tečky

Zde je příklad připojení tří teček (místo znaku se třemi tečkami) ke sloupci, kdykoli počet znaků v tomto sloupci překročí určitou délku:

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

Výsledek:

+-------------------------------------+-----------------------------------------+
| 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.        |
+-------------------------------------+-----------------------------------------+

V tomto případě použijeme CHAR_LENGTH() funkce uvnitř IF() funkce k určení, zda je či není řetězec dostatečně dlouhý, aby bylo možné jej zkrátit. Potom použijeme LEFT() funkce uvnitř CONCAT() funkce pro připojení několika teček ke krátkému popisu.

Použití skutečného znaku elipsy

A je to tady znovu, ale se skutečným znakem elipsy místo tří teček:

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

Výsledek:

+-------------------------------------+-----------------------------------------+
| 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.        |
+-------------------------------------+-----------------------------------------+

Elipsa zabírá méně místa. Je to proto, že se jedná o jeden znak, na rozdíl od tří teček (což jsou tři samostatné znaky).

Zkrátit VŠECHNY řádky, bez ohledu na délku

Pokud potřebujete zkrátit všechny řádky bez ohledu na jejich délku, nemusíte zadávat IF() funkce.

V tomto případě lze kód zkrátit na něco takového:

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

Výsledek:

+--------------------+-----------------------------------------+
| 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.        |
+--------------------+-----------------------------------------+

Vynechat elipsu

A pokud nepotřebujete ani elipsu/tři tečky, můžete to ještě zkrátit na něco takového:

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

Výsledek:

+-----------------+-----------------------------------------+
| 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. Nasazení a správa MySQL NDB Cluster s ClusterControl

  2. Oracle Database Explorer:bezplatné školení a akreditace

  3. Zrušit sloupec neodstraní odkazy na sloupce úplně - postgresql

  4. Převeďte razítko MySql DateTime do formátu Datum v JavaScriptu