MySQL 関数 ( 2 )

August 05, 2018

文字列関数

連結や置換に関する関数

CONCAT()
mysql> SELECT CONCAT(NAME, 'AAA') FROM City LIMIT 1;  
+---------------------+  
| CONCAT(NAME, 'AAA') |  
+---------------------+  
| KabulAAA            |  
+---------------------+  
1 row in set (0.02 sec)  
CONCAT_WS()
mysql> SELECT CONCAT_WS('---', Name, 'AAA') FROM City LIMIT 1;  
+-------------------------------+  
| CONCAT_WS('---', Name, 'AAA') |  
+-------------------------------+  
| Kabul---AAA                   |  
+-------------------------------+  
1 row in set (0.00 sec)  
INSERT()
mysql> SELECT INSERT(Name, 3, 5, 'what') FROM City LIMIT 1;  
+----------------------------+  
| INSERT(Name, 3, 5, 'what') |  
+----------------------------+  
| Kawhat                     |  
+----------------------------+  
1 row in set (0.01 sec)  
REPLACE()
mysql> SELECT REPLACE(Name, 'bul', 'AAA') FROM City LIMIT 1;  
+-----------------------------+  
| REPLACE(Name, 'bul', 'AAA') |  
+-----------------------------+  
| KaAAA                       |  
+-----------------------------+  
1 row in set (0.00 sec)  

変換関数

LOWER()
mysql> SELECT LOWER(Name) FROM City LIMIT 1;  
+-------------+  
| LOWER(Name) |  
+-------------+  
| kabul       |  
+-------------+  
1 row in set (0.00 sec)  
UPPER()
mysql> SELECT UPPER(Name) FROM City LIMIT 1;  
+-------------+  
| UPPER(Name) |  
+-------------+  
| KABUL       |  
+-------------+  
1 row in set (0.00 sec)  
REPEAT()
mysql> SELECT REPEAT(Name, 5) FROM City LIMIT 1;  
+---------------------------+  
| REPEAT(Name, 5)           |  
+---------------------------+  
| KabulKabulKabulKabulKabul |  
+---------------------------+  
1 row in set (0.00 sec)  
REVERSE()
mysql> SELECT REVERSE(Name) FROM City LIMIT 1;  
+---------------+  
| REVERSE(Name) |  
+---------------+  
| lubaK         |  
+---------------+  
1 row in set (0.00 sec)  

検索関数

LOCATE(), POSITION(), INSTR()
mysql> SELECT LOCATE('bu', Name) FROM City LIMIT 1;  
+--------------------+  
| LOCATE('bu', Name) |  
+--------------------+  
|                  3 |  
+--------------------+  
1 row in set (0.01 sec)  
STRCMP()
mysql> SELECT STRCMP('Kebul', Name) FROM City LIMIT 1;  
+-----------------------+  
| STRCMP('Kebul', Name) |  
+-----------------------+  
|                     1 |  
+-----------------------+  
1 row in set (0.00 sec)  
  
mysql> SELECT STRCMP('Kabul', Name) FROM City LIMIT 1;  
+-----------------------+  
| STRCMP('Kabul', Name) |  
+-----------------------+  
|                     0 |  
+-----------------------+  
1 row in set (0.01 sec)  

 © 2023, Dealing with Ambiguity