MariaDB
 sql >> Teknologi Basis Data >  >> RDS >> MariaDB

Cara Memotong Teks dengan Ellipsis di MariaDB

Terkadang Anda mungkin menemukan bahwa jumlah teks yang dikembalikan dalam kolom database terlalu panjang. Anda mungkin hanya ingin mengembalikan potongan pendek teks tersebut, diikuti dengan elipsis atau tiga titik.

Untungnya, ini relatif mudah dilakukan di MariaDB.

Tiga Periode

Berikut adalah contoh menambahkan tiga titik (bukan karakter elipsis) ke kolom setiap kali jumlah karakter dalam kolom tersebut melebihi panjang tertentu:

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

Hasil:

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

Dalam hal ini kami menggunakan CHAR_LENGTH() fungsi di dalam IF() berfungsi untuk menentukan apakah string cukup panjang untuk menjamin pemendekannya. Kami kemudian menggunakan LEFT() fungsi di dalam CONCAT() berfungsi untuk menambahkan beberapa titik ke deskripsi singkat.

Menggunakan Karakter Ellipsis yang Sebenarnya

Dan ini dia lagi, tetapi dengan karakter elipsis yang sebenarnya, bukan tiga titik:

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

Hasil:

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

Elipsis menggunakan lebih sedikit ruang. Ini karena ini adalah satu karakter, bukan tiga titik (yang merupakan tiga karakter terpisah).

Memotong SEMUA Baris, Berapapun Panjangnya

Jika Anda perlu memotong semua baris, berapa pun panjangnya, Anda tidak perlu menyertakan IF() fungsi.

Dalam hal ini, kode dapat dipersingkat menjadi seperti ini:

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

Hasil:

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

Abaikan Ellipsis

Dan jika Anda bahkan tidak memerlukan elipsis/tiga titik, Anda bahkan dapat mempersingkatnya lebih jauh, menjadi seperti ini:

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

Hasil:

+-----------------+-----------------------------------------+
| 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. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. Menerapkan Replikasi MySQL Multicloud Aman di AWS dan GCP dengan VPN

  2. MariaDB CEIL() Dijelaskan

  3. Sumber Daya Cadangan Database MySQL &MariaDB

  4. Bagaimana SLEEP() Bekerja di MariaDB

  5. Meningkatkan Kinerja dengan Menggunakan Pemisahan Baca Tulis dari Lalu Lintas Basis Data dengan Moodle 3.9