[英]How to split a multiple string using t-sql
I have tried to split this string but unsuccessful because of the way the string is arranged in the column.我试图拆分此字符串但由于字符串在列中的排列方式而未成功。 The Null value keeps appearing on the Make and model column and the actually data goes to the wrong column Null 值不断出现在 Make 和 model 列中,实际数据转到了错误的列
Sample data:样本数据:
MakeModelColor制作模型颜色 |
---|
Apple - iphone 12苹果-iphone 12 |
Apple - iphone 12 pro max - black -128gb Apple - iphone 12 pro max - 黑色 -128gb |
Samsung - galaxy A12三星-银河A12 |
This is the result I am looking for:这是我正在寻找的结果:
Make制作 | Model Model |
---|---|
Apple苹果 | iphone 12苹果 12 |
Apple苹果 | iphone 12 pro max苹果 12 专业最大 |
Samsung三星 | Galaxy A12银河A12 |
Actual result am looking for ]( https://i.stack.imgur.com/BvzYN.png )实际结果正在寻找]( https://i.stack.imgur.com/BvzYN.png )
Just another option using a bit of JSON只是使用一点 JSON 的另一种选择
Example例子
Select Make = trim(JSON_VALUE(JS,'$[0]'))
,Model = trim(JSON_VALUE(JS,'$[1]'))
From YourTable A
Cross Apply (values ('["'+replace(string_escape([MakeModelColor],'json'),'-','","')+'"]') ) B(JS)
Results结果
Make Model
Apple iphone 12
Apple iphone 12 pro max
Samsung galaxy A12
That could be done in a number of ways.这可以通过多种方式完成。 ie: IE:
with data(makeModelColor, part, ordinal) as (
select makeModelColor, ltrim(rtrim(value)), ordinal
from devices
cross apply (select * from String_Split(devices.makeModelColor,'-',1)) t)
select makeModelColor,
max(case when ordinal = 1 then part end) as Make,
max(case when ordinal = 2 then part end) as Model,
max(case when ordinal = 3 then part end) as Color,
max(case when ordinal = 4 then part end) as Other
from data
group by makeModelColor;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.