MariaDBでは、LEFT() は、文字列の左端から指定された数の文字を返す組み込みの文字列関数です。
LEFT() 2つの引数を受け入れます。文字列、およびその文字列の左側から返される文字数。
構文
構文は次のようになります:
LEFT(str,len)
str は文字列であり、len 文字列の左側から抽出する文字数です。
例
基本的な例は次のとおりです。
SELECT LEFT('Aerospace', 4); 結果:
+----------------------+
| LEFT('Aerospace', 4) |
+----------------------+
| Aero |
+----------------------+ データベースの例
データベース列の値の左側を取得する例を次に示します。
SELECT
LEFT(ProductDescription, 15) AS "Short Desc",
ProductDescription AS "Full Desc"
FROM Products; 結果:
+-----------------+-----------------------------------------+ | 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. | +-----------------+-----------------------------------------+
特定の長さのテキストを切り捨て、切り捨てられたテキストのみに省略記号を追加する例については、省略記号を使用してテキストを切り捨てる方法を参照してください。
ヌル引数
引数のいずれか(またはすべて)がnullの場合 、LEFT() 関数はnullを返します :
SELECT
LEFT(null, 3),
LEFT('Coffee', null),
LEFT(null, null); 結果:
+---------------+----------------------+------------------+
| LEFT(null, 3) | LEFT('Coffee', null) | LEFT(null, null) |
+---------------+----------------------+------------------+
| NULL | NULL | NULL |
+---------------+----------------------+------------------+ 引数がありません
LEFT()を呼び出す 引数を渡さないとエラーが発生します:
SELECT LEFT(); 結果:
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ')' at line 1