有如下一个表: 序号 名称
1 合金
2 合页
1-4 钢球
2-5 镶件
11-3 球节
22-1 皂化油
1-2 肥皂如何用sql语句生成如下的排序表:
序号 名称
1 合金
1-2 肥皂
1-4 钢球
2 合页
2-5 镶件
11-3 球节
22-1 皂化油请各位大侠指教
1 合金
2 合页
1-4 钢球
2-5 镶件
11-3 球节
22-1 皂化油
1-2 肥皂如何用sql语句生成如下的排序表:
序号 名称
1 合金
1-2 肥皂
1-4 钢球
2 合页
2-5 镶件
11-3 球节
22-1 皂化油请各位大侠指教
insert @a select '1', '合金'
union all select '2', '合页'
union all select '1-4', '钢球'
union all select '2-5', '镶件'
union all select '11-3', '球节'
union all select '22-1', '皂化油'
union all select '1-2', '肥皂'select 序号,名称,
x=cast((case when charindex('-',序号)>0 then left(序号,charindex('-',序号)-1) else 序号 end) as int),
y=cast((case when charindex('-',序号)>0 then right(序号,len(序号)-charindex('-',序号)) else 序号 end) as int) from @a order by 3,4
y=cast((case when charindex('-',序号)>0 then right(序号,len(序号)-charindex('-',序号)) else 0 end) as int)
序号: 2-1
如果用楼上的方法:
排序为:
2-1
2
2-5
这样的结果应该不符合要求
改为下面的语句可能更好些:
select 序号,名称,
x=cast((case when charindex('-',序号)>0 then left(序号,charindex('-',序号)-1) else 序号 end) as int),
y=cast((case when charindex('-',序号)>0 then right(序号,len(序号)-charindex('-',序号)) else 0 end) as int) from @a order by 3,4