Function resembling the work of Explode in MySQL

后端 未结 3 1546
旧时难觅i
旧时难觅i 2021-01-23 07:35

Is there any function in MySQL to explode data of a column and then retrieve it? Like if a column data is P:12 , then can the data be exploded on \':\' and then read?

3条回答
  •  长发绾君心
    2021-01-23 08:31

    You can write a Split function in MYSQL check this link

    From the Link

    CREATE FUNCTION SPLIT_STR(
      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, '');
    

    Usage

    SELECT SPLIT_STR(string, delimiter, position)
    

    Example

    SELECT SPLIT_STR('a|bb|ccc|dd', '|', 3) as third;
    
    +-------+
    | third |
    +-------+
    | ccc   |
    +-------+
    

提交回复
热议问题