我有这样的数据:80X71X120CM @ 95KG,80X71X120CM @ 96KG X 2,80X71X107CM @ 76KG X 5。
如何获得重量的总和(95 + 96 * 2 + 76 * 5)?
@
分隔它?我把它当作3行,所以这是您可以做的,尽管非常笨拙...` declare @table as table(stringdata nvarchar(100))
insert into @table values ('80X71X120CM@95KG')
insert into @table values ('80X71X120CM@96KG X 2')
insert into @table values ('80X71X107CM@76KG X 5')
;with cte as (
select
substring(stringdata,charindex('@', stringdata) +1,len(stringdata) -charindex('@', stringdata)) as WeightData
from @table
), cte2 as (
select
left(WeightData, charindex('KG', WeightData)-1) as weight,
case when charindex('X', WeightData) > 0 then
convert(integer, (substring(WeightData,charindex('X', WeightData) +1,len(WeightData) -charindex('X', WeightData))))
else
1
end as NumberOfPersons,
case when charindex('X', WeightData) > 0 then
convert(integer, (substring(WeightData,charindex('X', WeightData) +1,len(WeightData) -charindex('X', WeightData)))) * convert(integer, left(WeightData, charindex('KG', WeightData)-1) )
else
convert(integer, left(WeightData, charindex('KG', WeightData)-1) )
end as RowWeight
from cte
)
select
sum(RowWeight) as TotalWeight
from cte2 `