您的位置:首页 > 数据库

sql 字段分割 物流费计算

2009-11-04 11:07 309 查看
问题:

有一个字段 prozise 在行中 有些有一个 有些有2个,空格隔开 里面数字单位是毫米
求长*高*宽的结果 结果单位是立方米
结果放到prozise1字段中 两个的话 把第二个放到字段prozise2中
字段prozise部分数据如下:
1940*1050*135
990*730*120
990*730*120
870*500*50
870*500*50
1260*660*15 1305*670*76
1260*660*15 1305*670*76
1010*920*190 1940*1010*110
1010*920*190 1940*1010*110
1010*920*190 1940*1010*110
1010*920*190 1940*1010*110
1975*990*110 1940*1020*110
1975*990*110 1940*1020*110

解决:http://topic.csdn.net/u/20091103/18/ba8f46db-b333-431c-8699-cd30427adbdf.html

sql表结构:

ID int
name varchar(255)
proid varchar(255)
prosize varchar(255)
prosize1 numeric(18, 4)
prosize2 numeric(18, 4)
huadong numeric(18, 4)
huanan numeric(18, 4)
huabei numeric(18, 4)
xibu_dongbei_huazhong numeric(18, 4)
huadong1 numeric(18, 4)
huanan1 numeric(18, 4)
huabei1 numeric(18, 4)
xibu_dongbei_huazhong1 numeric(18, 4)

参考:

1.

create table # (prozise varchar(100),prozise1 numeric(18,4),prozise2 numeric(18,4))
insert #(prozise) select
'1940*1050*135' union all select
'990*730*120' union all select
'990*730*120' union all select
'870*500*50' union all select
'870*500*50' union all select
'1260*660*15 1305*670*76' union all select
'1260*660*15 1305*670*76' union all select
'1010*920*190 1940*1010*110' union all select
'1010*920*190 1940*1010*110' union all select
'1010*920*190 1940*1010*110' union all select
'1010*920*190 1940*1010*110' union all select
'1975*990*110 1940*1020*110' union all select
'1975*990*110 1940*1020*110'
go

update a set
prozise1 = cast(left(lvl1,charindex('*',lvl1)-1) as money)/1000
*cast(left(stuff(lvl1,1,charindex('*',lvl1),''),
charindex('*',stuff(lvl1,1,charindex('*',lvl1),''),
charindex('*',stuff(lvl1,1,charindex('*',lvl1),'')))-1) as money)/1000
*cast(right(lvl1,charindex('*',reverse(lvl1))-1) as money)/1000,
prozise2=case when lvl2 is null then null
else
cast(left(lvl2,charindex('*',lvl2)-1) as money)/1000
*cast(left(stuff(lvl2,1,charindex('*',lvl2),''),
charindex('*',stuff(lvl2,1,charindex('*',lvl2),''),
charindex('*',stuff(lvl2,1,charindex('*',lvl2),'')))-1) as money)/1000
*cast(right(lvl2,charindex('*',reverse(lvl2))-1) as money)/1000
end
from # as a
outer apply(
select left(prozise,charindex(' ',prozise+' ')-1) as lvl1,
case when charindex(' ',prozise)>0 then
right(prozise,charindex(' ',reverse(prozise))-1)
else null end as lvl2
) as b;

select * from #;

go
drop table #

/*
prozise prozise1 prozise2
-------------------------------------------------- --------------------------------------- ---------------------------------------
1940*1050*135 0.2749 NULL
990*730*120 0.0867 NULL
990*730*120 0.0867 NULL
870*500*50 0.0217 NULL
870*500*50 0.0217 NULL
1260*660*15 1305*670*76 0.0124 0.0664
1260*660*15 1305*670*76 0.0124 0.0664
1010*920*190 1940*1010*110 0.1765 0.2155
1010*920*190 1940*1010*110 0.1765 0.2155
1010*920*190 1940*1010*110 0.1765 0.2155
1010*920*190 1940*1010*110 0.1765 0.2155
1975*990*110 1940*1020*110 0.2150 0.2176
1975*990*110 1940*1020*110 0.2150 0.2176

(13 行受影响)

*/

2.sql2005
create table # (prozise varchar(50),prozise1 numeric(18,4),prozise2 numeric(18,4))
insert #(prozise) select
'1940*1050*135' union all select
'990*730*120' union all select
'990*730*120' union all select
'870*500*50' union all select
'870*500*50' union all select
'1260*660*15 1305*670*76' union all select
'1260*660*15 1305*670*76' union all select
'1010*920*190 1940*1010*110' union all select
'1010*920*190 1940*1010*110' union all select
'1010*920*190 1940*1010*110' union all select
'1010*920*190 1940*1010*110' union all select
'1975*990*110 1940*1020*110' union all select
'1975*990*110 1940*1020*110'
go

update a set
prozise1=b.lvl1.value('(//v)[1]','money')/1000*b.lvl1.value('(//v)[2]','money')/1000
*b.lvl1.value('(//v)[3]','money')/1000,
prozise2=b.lvl2.value('(//v)[1]','money')/1000*b.lvl2.value('(//v)[2]','money')/1000
*b.lvl2.value('(//v)[3]','money')/1000
from # as a
outer apply(
select
convert(xml,'<v>'+
replace(left(prozise,charindex(' ',prozise+' ')-1),
'*','</v><v>')+'</v>') as lvl1,
case when charindex(' ',prozise)>0 then

convert(xml,'<v>'+
replace(right(prozise,charindex(' ',reverse(prozise))-1),
'*','</v><v>')+'</v>')
else null end as lvl2

) as b

select * from #;
go
drop table #

/*
prozise prozise1 prozise2
-------------------------------------------------- --------------------------------------- ---------------------------------------
1940*1050*135 0.2749 NULL
990*730*120 0.0867 NULL
990*730*120 0.0867 NULL
870*500*50 0.0217 NULL
870*500*50 0.0217 NULL
1260*660*15 1305*670*76 0.0124 0.0664
1260*660*15 1305*670*76 0.0124 0.0664
1010*920*190 1940*1010*110 0.1765 0.2155
1010*920*190 1940*1010*110 0.1765 0.2155
1010*920*190 1940*1010*110 0.1765 0.2155
1010*920*190 1940*1010*110 0.1765 0.2155
1975*990*110 1940*1020*110 0.2150 0.2176
1975*990*110 1940*1020*110 0.2150 0.2176

(13 行受影响)

*/

--更新物流费

UPDATE [wl].[dbo].[xx]
SET huadong=prosize1*200, huanan=prosize1*84.375,huabei=prosize1*240,xibu_dongbei_huazhong=prosize1*211.65

UPDATE [wl].[dbo].[xx]
SET huadong1=prosize2*200, huanan1=prosize2*84.375,huabei1=prosize2*240,xibu_dongbei_huazhong1=prosize2*211.65
where prosize2 is not null

--更新体积(立方米)
update a set
prosize1 = cast(left(lvl1,charindex('*',lvl1)-1) as money)/1000
*cast(left(stuff(lvl1,1,charindex('*',lvl1),''),
charindex('*',stuff(lvl1,1,charindex('*',lvl1),''),
charindex('*',stuff(lvl1,1,charindex('*',lvl1),'')))-1) as money)/1000
*cast(right(lvl1,charindex('*',reverse(lvl1))-1) as money)/1000,
prosize2=case when lvl2 is null then null
else
cast(left(lvl2,charindex('*',lvl2)-1) as money)/1000
*cast(left(stuff(lvl2,1,charindex('*',lvl2),''),
charindex('*',stuff(lvl2,1,charindex('*',lvl2),''),
charindex('*',stuff(lvl2,1,charindex('*',lvl2),'')))-1) as money)/1000
*cast(right(lvl2,charindex('*',reverse(lvl2))-1) as money)/1000
end
from xx as a
outer apply(
select left(prosize,charindex(' ',prosize+' ')-1) as lvl1,
case when charindex(' ',prosize)>0 then
right(prosize,charindex(' ',reverse(prosize))-1)
else null end as lvl2
) as b;

select * from xx

go

--SQL2005导出 excel 2003
--新建excel表并且excel中建好sql对应的字段
--xx是表名
insert into OPENROWSET('MICROSOFT.JET.OLEDB.4.0','Excel 5.0;HDR=YES;DATABASE=c:\wl.xls',sheet1$) select * from xx

--excel 2003导入sql2005 表tt中
--不用新建表tt
SELECT * into tt
FROM OpenDataSource( 'Microsoft.Jet.OLEDB.4.0',
'Data Source="c:\wl.xls";User ID=Admin;Password=;Extended properties=Excel 5.0')[Sheet1$]

--实例:
SELECT * into newtable
FROM OpenDataSource( 'Microsoft.Jet.OLEDB.12.0',
'Data Source="c:\Finance\account.xls";User ID=Admin;Password=;Extended properties=Excel 5.0')xactions
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: