sql - 将逗号分隔的值拆分为多列
解决方案
您的样本数据可能不需要任何拆分。您希望根据找到的值将数据移动到列中。您可以比拆分数据更简单一些。这适用于您的示例数据。
declare @Something table
(
Combined_Column varchar(10)
)
insert @Something values
('1,2,3')
, ('2')
, ('1,3')
, ('1,2,3,4')
, ('1,3,4')
, ('1')
, ('4')
select *
, col1 = case when charindex('1', s.Combined_Column) > 0 then 1 end
, col2 = case when charindex('2', s.Combined_Column) > 0 then 2 end
, col3 = case when charindex('3', s.Combined_Column) > 0 then 3 end
, col4 = case when charindex('4', s.Combined_Column) > 0 then 4 end
from @Something s
推荐阅读
- firebase - 是否可以从函数返回 Firestore 数据?
- zsh - 将 zsh 命令行参数传递到 xargs 引号中
- swiftui - SwiftUI 中某些设备上出现 No ObservableObject of type XXX 的错误
- php - 合并或组合两个数组 - 仅当指定的 id 存在时
- c++ - C++:外部类中静态内部类实例的构造和销毁
- git - 如果我的 GitHub 存储库也是已发布的 npm 模块,我可以归档它吗?
- javascript - 在javascript中将sort和uniq函数合并为1
- python - 将列表值转换为数据框
- pandas - 对均值 pandas 的 T 检验
- javascript - 在 Angular 中的单选按钮上选择所有实现