문자열 내장 함수


26.2.1 CONCAT()

문자열을 결합할 수 있습니다. 결합하고자 하는 컬럼 필드 또는 문자들은 콤마 (,)로 구분 합니다.

| 콘솔 실습 화면 |

mysql> select * from members;
+----+----------+-----------+-------------+-------+---------+
| Id | LastName | FirstName | Address     | City  | Country |
+----+----------+-----------+-------------+-------+---------+
|  1 | hojin    | lee       | shinchon    | seoul | korea   |
|  2 | jiny     | NULL      | NULL        | NULL  | korea   |
|  3 | james    | kim       | youngdun-po | seoul | korea   |
|  4 | eric     | jung      | seocho-gu   | seoul | korea   |
+----+----------+-----------+-------------+-------+---------+
4 rows in set (0.00 sec)

mysql> select concat(FirstName, ' ', LastName, '님') as customer from members;
+--------------+
| customer     |
+--------------+
| lee hojin님  |
| NULL         |
| kim james님  |
| jung eric님  |
+--------------+
4 rows in set (0.00 sec)

그냥 문자를 넣는 경우에는 작은따옴표(‘)로 감싸서 넣습니다.

26.2.2 LEFT( )/RIGHT()

컬럼의 데이터를 왼쪽/오른쪽부터 지정한 숫자만큼만 출력합니다.

| 콘솔 실습 화면 |

// 왼쪽부터 2글자만 출력합니다.
mysql> select left(country,2) from members;
+-----------------+
| left(country,2) |
+-----------------+
| ko              |
| ko              |
| ko              |
| ko              |
+-----------------+
4 rows in set (0.00 sec)

// 오른쪽부터 3글자만 출력합니다.
mysql> select right(country,3) from members;
+------------------+
| right(country,3) |
+------------------+
| rea              |
| rea              |
| rea              |
| rea              |
+------------------+
4 rows in set (0.00 sec)

26.2.3 LENGTH()

문자열의 길이를 측정합니다.

| 콘솔 실습 화면 |

mysql> select name, length(name) from products;
+--------+--------------+
| name   | length(name) |
+--------+--------------+
| S-827  |            5 |
| S-532D |            6 |
| R-530  |            5 |
+--------+--------------+
3 rows in set (0.00 sec)

26.2.4 공백 제거 함수

LTRIM()은 좌측의 공백을 제거합니다. RTRIM()은 우측의 공백을 제거합니다.

| 콘솔 실습 화면 |

mysql> select ltrim(' jiny ');
+-----------------+
| ltrim(' jiny ') |
+-----------------+
| jiny            |
+-----------------+
1 row in set (0.00 sec)

mysql> select rtrim(' jiny ');
+-----------------+
| rtrim(' jiny ') |
+-----------------+
|  jiny           |
+-----------------+
1 row in set (0.00 sec)

26.2.5 SUBSTRING( )/MID()

컬럼의 데이터의 일부분만 출력합니다.

| 콘솔 실습 화면 |

mysql> select substring(Country, 2,2) from members;
+-------------------------+
| substring(Country, 2,2) |
+-------------------------+
| or                      |
| or                      |
| or                      |
| or                      |
+-------------------------+
4 rows in set (0.00 sec)

26.2.6 REPLACE()

문자열의 일부분을 치환할 수 있습니다.

| 콘솔 실습 화면 |

mysql> select * from products;
+----+--------+-------+------+-------+
| Id | name   | price | vat  | stock |
+----+--------+-------+------+-------+
|  1 | S-827  | 20000 | 2000 |     5 |
|  2 | S-532D | 36000 | 3600 |     7 |
|  3 | R-530  | 25000 | 2500 |    10 |
+----+--------+-------+------+-------+
3 rows in set (0.00 sec)

mysql> select replace(name, 'S-', 'shiny S-') from products;
+---------------------------------+
| replace(name, 'S-', 'shiny S-') |
+---------------------------------+
| shiny S-827                     |
| shiny S-532D                    |
| R-530                           |
+---------------------------------+
3 rows in set (0.00 sec)

26.2.7 UPPER( )/UCASE()

입력된 문자열을 대문자로 전환합니다.

| 콘솔 실습 화면 |

mysql> select UCASE('abcde'), UPPER('abcde');
+----------------+----------------+
| UCASE('abcde') | UPPER('abcde') |
+----------------+----------------+
| ABCDE          | ABCDE          |
+----------------+----------------+
1 row in set (0.01 sec)

26.2.8 LCASE()

입력된 문자열을 소문자로 전환합니다.

| 콘솔 실습 화면 |

mysql> select name, LCASE(name) from products;
+--------+-------------+
| name   | LCASE(name) |
+--------+-------------+
| S-827  | s-827       |
| S-532D | s-532d      |
| R-530  | r-530       |
+--------+-------------+
3 rows in set (0.01 sec)

mysql>

26.2.9 REPEAT (문자, 반복 횟수)

입력된 문자열을 지정 횟수만큼 반복 출력합니다.

| 콘솔 실습 화면 |

mysql> select * from products;
+----+--------+-------+------+-------+
| Id | name   | price | vat  | stock |
+----+--------+-------+------+-------+
|  1 | S-827  | 20000 | 2000 |     5 |
|  2 | S-532D | 36000 | 3600 |     7 |
+----+--------+-------+------+-------+
2 rows in set (0.00 sec)

mysql> select repeat('*',stock) from products;
+-------------------+
| repeat('*',stock) |
+-------------------+
| *****             |
| *******           |
+-------------------+
2 rows in set (0.00 sec)

재고 숫자만큼 *를 출력합니다.

26.2.10 REVERSE()

입력된 문자열을 거꾸로 뒤집습니다.

| 콘솔 실습 화면 |

mysql> select reverse(Country) from members;
+------------------+
| reverse(Country) |
+------------------+
| aerok            |
| aerok            |
| aerok            |
| aerok            |
+------------------+
4 rows in set (0.00 sec)

26.2.11 ASCII(문자)

입력된 단일 문자의 아스키 코드값을 반환합니다.

| 콘솔 실습 화면 |

mysql> select ascii('a');
+------------+
| ascii('a') |
+------------+
|         97 |
+------------+
1 row in set (0.00 sec)

26.2.12 INSTR(‘문자열’, ‘찾는 문자열’)

입력된 문자열 중 찾는 문자열의 위치 값을 출력합니다.

| 콘솔 실습 화면 |

mysql> select email, instr(email,'@') from members;
+-------------------+------------------+
| email             | instr(email,'@') |
+-------------------+------------------+
| hojin@jinyphp.com |                6 |
| jiny@jinyphp.com  |                5 |
| james@jinyphp.com |                6 |
| NULL              |             NULL |
| NULL              |             NULL |
| NULL              |             NULL |
| NULL              |             NULL |
+-------------------+------------------+
7 rows in set (0.00 sec)

mysql>

26.2.13 IF(논리식, 참일 때 값, 거짓일 때 값)

논리식이 참이면 참일 때 값을 출력하고 논리식이 거짓이면 거짓일 때 값을 출력합니다.

| 콘솔 실습 화면 |

mysql> select * from products;
+----+--------+-------+------+-------+
| Id | name   | price | vat  | stock |
+----+--------+-------+------+-------+
|  1 | S-827  | 20000 | 2000 |     5 |
|  2 | S-532D | 36000 | 3600 |     7 |
|  3 | R-530  | 25000 | 2500 |    10 |
+----+--------+-------+------+-------+
3 rows in set (0.00 sec)

mysql> select stock, if(stock>5,'safeStock','Please order this') from products;
+-------+---------------------------------------------+
| stock | if(stock>5,'safeStock','Please order this') |
+-------+---------------------------------------------+
|     5 | Please order this                           |
|     7 | safeStock                                   |
|    10 | safeStock                                   |
+-------+---------------------------------------------+
3 rows in set (0.00 sec)

mysql>

26.2.14 IFNULL(값1, 값2)

값1이 NULL이면 값2로 대치하고 그렇지 않으면 값1을 출력합니다.

| 콘솔 실습 화면 |

mysql> select * from members;
+-----+----------+-----------+-------------+----------+-------+---------+-----------+-------------------+
| Id  | LastName | FirstName | Address     | address2 | City  | Country | manager   | email             |
+-----+----------+-----------+-------------+----------+-------+---------+-----------+-------------------+
|   1 | hojin    | lee       | shinchon    | NULL     | seoul | Korea   | infohojin | hojin@jinyphp.com |
|   2 | lee      | hojin     | NULL        | NULL     |       | Korea   | infohojin | jiny@jinyphp.com  |
|   3 | james    | kim       | youngdun-po | NULL     | seoul | Korea   | info      | james@jinyphp.com |
|   5 | 1234     | jiny      | NULL        | NULL     |       | Korea   | NULL      | NULL              |
|   6 | 1234     | jiny      | NULL        | NULL     |       | Korea   | NULL      | NULL              |
|   7 | 123400   | jiny      | NULL        | NULL     |       | Korea   | NULL      | NULL              |
| 100 | dfasdf   | NULL      | NULL        | NULL     |       | NULL    | NULL      | NULL              |
+-----+----------+-----------+-------------+----------+-------+---------+-----------+-------------------+
7 rows in set (0.00 sec)

mysql> select email, ifnull(email,'empty') from members;
+-------------------+-----------------------+
| email             | ifnull(email,'empty') |
+-------------------+-----------------------+
| hojin@jinyphp.com | hojin@jinyphp.com     |
| jiny@jinyphp.com  | jiny@jinyphp.com      |
| james@jinyphp.com | james@jinyphp.com     |
| NULL              | empty                 |
| NULL              | empty                 |
| NULL              | empty                 |
| NULL              | empty                 |
+-------------------+-----------------------+
7 rows in set (0.01 sec)