1

我需要将一串字母转置(我希望转置这个词是正确的)一列中的两个字母

我的数据如下:基本上是两个字母的代码混在一起,很乱

ColumnID
HG34
HG/WK
HG\34
54/OF
AS\MM
AC54
AC54MM

以及我想将上面转换为下面的内容:

ColumnID
HG
34
WK
54
OF
AS
MM
AC

有没有可以做到这一点的查询?

我不介意结果是否重复,因为我以后可以随时清理它

谢谢

4

2 回答 2

4
select substring(ColumnID, 1, 2) from your_table
union
select substring(replace(replace(ColumnID,'\\',''),'/',''), 3, 2) from your_table
union
select substring(replace(replace(ColumnID,'\\',''),'/',''), 5, 2) from your_table
where length(replace(replace(ColumnID,'\\',''),'/','')) = 6

SQLFiddle 演示

于 2013-11-14T10:54:23.140 回答
1
select distinct substring(col, x*2-1, 2)
from your_table
cross apply (select replace(replace(ColumnID,'\',''),'/',''))c(col)
inner join (values(1),(2),(3))x(x) on len (col) >= x * 2 -- as much pairs as needed
于 2013-11-14T11:02:15.240 回答