A veces puede encontrar que la cantidad de texto devuelto en una columna de la base de datos es demasiado larga. Es posible que desee devolver un fragmento breve de ese texto, seguido de puntos suspensivos o tres puntos.
Afortunadamente, esto es relativamente fácil de hacer en MariaDB.
Tres Períodos
Aquí hay un ejemplo de agregar tres puntos (en lugar de un carácter de puntos suspensivos) a una columna siempre que la cantidad de caracteres en esa columna exceda una cierta longitud:
SELECT
IF(CHAR_LENGTH(ProductDescription) > 32,
CONCAT(LEFT(ProductDescription, 32),"..."),
ProductDescription) AS "Short Desc",
ProductDescription AS "Full Desc"
FROM Products;
Resultado:
+-------------------------------------+-----------------------------------------+ | 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. | +-------------------------------------+-----------------------------------------+
En este caso usamos el CHAR_LENGTH()
función dentro de un IF()
función para determinar si la cadena es o no lo suficientemente larga como para justificar su acortamiento. Luego usamos LEFT()
función dentro de CONCAT()
función para agregar algunos puntos a la breve descripción.
Uso de un carácter de puntos suspensivos reales
Y aquí está de nuevo, pero con un carácter de puntos suspensivos en lugar de tres puntos:
SELECT
IF(CHAR_LENGTH(ProductDescription) > 32,
CONCAT(LEFT(ProductDescription, 32),"…"),
ProductDescription) AS "Short Desc",
ProductDescription AS "Full Desc"
FROM Products;
Resultado:
+-------------------------------------+-----------------------------------------+ | 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. | +-------------------------------------+-----------------------------------------+
Los puntos suspensivos ocupan menos espacio. Esto se debe a que es un solo carácter, a diferencia de los tres puntos (que son tres caracteres separados).
Truncar TODAS las filas, independientemente de la longitud
Si necesita truncar todas las filas, independientemente de su longitud, entonces no necesita incluir el IF()
función.
En este caso, el código se puede acortar a algo como esto:
SELECT
CONCAT(LEFT(ProductDescription, 15), '...') AS "Short Desc",
ProductDescription AS "Full Desc"
FROM Products;
Resultado:
+--------------------+-----------------------------------------+ | 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. | +--------------------+-----------------------------------------+
Omita los puntos suspensivos
Y si ni siquiera necesita los puntos suspensivos/tres puntos, puede acortarlos aún más, a algo como esto:
SELECT
LEFT(ProductDescription, 15) AS "Short Desc",
ProductDescription AS "Full Desc"
FROM Products;
Resultado:
+-----------------+-----------------------------------------+ | 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. | +-----------------+-----------------------------------------+