Home > MySQL > MySQL split string

MySQL split string

1. Tạo function

CREATE FUNCTION SPLIT_STRING(
  x VARCHAR(255),
  delim VARCHAR(12),
  pos INT
)
RETURNS VARCHAR(255)
RETURN REPLACE(SUBSTRING(SUBSTRING_INDEX(x, delim, pos),
       LENGTH(SUBSTRING_INDEX(x, delim, pos -1)) + 1),
       delim, '');

 

2. Cách dùng

SELECT SPLIT_STRING(string, delimiter, position)

 

3. Ví dụ

SELECT SPLIT_STR('aaa|bbb|ccc|ddd', '|', 2) as two;

+-------+
| two   |
+-------+
| bbb   |
+-------+

nhantam
Thiết kế web

Advertisements
Categories: MySQL
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: