[MySQL] String Functions

김민지·2022년 2월 28일
0

SQL 기본

목록 보기
5/10

1. CONCAT

concatenate : Combine Data For Cleaner Output

CONCAT(column, 'text', anotherColumn, 'more text')

예시

SELECT
  CONCAT(author_fname, ' ', author_lname)
FROM books;

**CONCAT_WS: Concatenate With Seperator

예시: 구분자 ' - '를 사이에 넣어 데이터 결합하기

SELECT 
    CONCAT_WS(' - ', title, author_fname, author_lname) 
FROM books;

2. SUBSTRING

**SUBSTRING() = SUBSTR()

예시: 'Hello World' 의 1번째부터 4번째 캐릭터 출력하기

SELECT SUBSTRING('Hello World', 1, 4);

+--------------------------------+
| SUBSTRING('Hello World', 1, 4) |
+--------------------------------+
| Hell                           |
+--------------------------------+

예시: 'Hello World' 의 7번째 부터 끝까지 출력하기

SELECT SUBSTRING('Hello World', 7);

+-----------------------------+
| SUBSTRING('Hello World', 7) |
+-----------------------------+
| World                       |
+-----------------------------+

예시: 'Hello World' 의 뒤에서 3번째부터 끝까지 출력

SELECT SUBSTRING('Hello World', -3);

+------------------------------+
| SUBSTRING('Hello World', -3) |
+------------------------------+
| rld                          |
+------------------------------+

3. REPLACE

예시:

SELECT
  REPLACE('cheese bread coffee milk', ' ', ' and ');
  
+---------------------------------------------------+
| REPLACE('cheese bread coffee milk', ' ', ' and ') |
+---------------------------------------------------+
| cheese and bread and coffee and milk              |
+---------------------------------------------------+

4. REVERSE

SELECT REVERSE('Hello World');

+------------------------+
| REVERSE('Hello World') |
+------------------------+
| dlroW olleH            |
+------------------------+

5. CHAR_LENGTH

Counts Characters in String

SELECT CHAR_LENGTH('Hello World');

+----------------------------+
| CHAR_LENGTH('Hello World') |
+----------------------------+
|                         11 |
+----------------------------+

6. UPPER() and LOWER()

change a string's case

SELECT UPPER('Hello World');
+----------------------+
| UPPER('Hello World') |
+----------------------+
| HELLO WORLD          |
+----------------------+


SELECT LOWER('Hello World');
+----------------------+
| LOWER('Hello World') |
+----------------------+
| hello world          |
+----------------------+
profile
Marketer

0개의 댓글