在MySQL中将Varchar拆分为字符 [英] Split Varchar into Character in MySQL

查看:124
本文介绍了在MySQL中将Varchar拆分为字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个长度为6的varchar字段。我想在MySQL中拆分为单个字符。目前,我已经尝试使用以下脚本:

I have a varchar field with length 6. I want to split into single characters in MySQL. Currently I have tried with following script:

col = '123456';
{
select SUBSTRING(col,1, 1),
SUBSTRING(col, 2,1),
SUBSTRING(col, 3,1),
SUBSTRING(col, 4,1),
SUBSTRING(col, 5,1),
SUBSTRING(col, 6,1)
from tbl_table
}

上面的脚本有效,但是还有其他解决方案。

The above script works but is there any other solution for this.

谢谢。

推荐答案

MySQL中没有字符串拆分功能。因此您必须创建自己的函数。使用下面的链接。这将帮助您

There is no string split function in MySQL. so you have to create your own function. Use below link.This will help you

分割定界字符串

以下内容示例函数接受3个参数,使用SQL函数执行操作,然后返回结果。

The following example function takes 3 parameters, performs an operation using an SQL function, and returns the result.

函数

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, '');

用法

SELECT SPLIT_STR(string, delimiter, position)

示例

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

+-------+
| third |
+-------+
| ccc   |
+-------+

信用: http://blog.fedecarg.com/2009/02/22/mysql-split-字符串功能/

这篇关于在MySQL中将Varchar拆分为字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆