刚在群里看一个网友案例,要将一个多文本的字段分解成多个字段,效果如下:
他使用的方式是 用right left mid 来取文本,生成字段:
Select tbl钣金发货单.ID, tbl钣金发货单.录入日期, tbl钣金发货单.条码, left(条码,Instr(2,[条码],"/")-1) AS 下单日期, MID(条码,Len(left(条码,Instr(2,[条码],"/")-1))+2,Instr(2,MID(条码,Instr(2,[条码],"/")+1,50),"/")-1) AS 客户编号, MID(条码,Len(left(条码,Instr(2,[条码],"/")-1))+Len(MID(条码,Len(left(条码,Instr(2,[条码],"/")-1))+2,Instr(2,MID(条码,Instr(2,[条码],"/")+1,50),"/")-1))+3,instr(2,MID(条码,Len(left(条码,Instr(2,[条码],"/")-1))+Len(MID(条码,Len(left(条码,Instr(2,[条码],"/")-1))+2,Instr(2,MID(条码,Instr(2,[条码],"/")+1,50),"/")-1))+3,50),"/")-1) AS 主订单编号, MID(条码,len(left(条码,Instr(2,[条码],"/")-1))+len(MID(条码,Len(left(条码,Instr(2,[条码],"/")-1))+2,Instr(2,MID(条码,Instr(2,[条码],"/")+1,50),"/")-1))+len(MID(条码,Len(left(条码,Instr(2,[条码],"/")-1))+Len(MID(条码,Len(left(条码,Instr(2,[条码],"/")-1))+2,Instr(2,MID(条码,Instr(2,[条码],"/")+1,50),"/")-1))+3,instr(2,MID(条码,Len(left(条码,Instr(2,[条码],"/")-1))+Len(MID(条码,Len(left(条码,Instr(2,[条码],"/")-1))+2,Instr(2,MID(条码,Instr(2,[条码],"/")+1,50),"/")-1))+3,50),"/")-1))+4,Instr(2,MID(条码,len(left(条码,Instr(2,[条码],"/")-1))+len(MID(条码,Len(left(条码,Instr(2,[条码],"/")-1))+2,Instr(2,MID(条码,Instr(2,[条码],"/")+1,50),"/")-1))+len(MID(条码,Len(left(条码,Instr(2,[条码],"/")-1))+Len(MID(条码,Len(left(条码,Instr(2,[条码],"/")-1))+2,Instr(2,MID(条码,Instr(2,[条码],"/")+1,50),"/")-1))+3,instr(2,MID(条码,Len(left(条码,Instr(2,[条码],"/")-1))+Len(MID(条码,Len(left(条码,Instr(2,[条码],"/")-1))+2,Instr(2,MID(条码,Instr(2,[条码],"/")+1,50),"/")-1))+3,50),"/")-1))+4,50),"/")-1) AS 副订单编号, mid(条码,len(left(条码,Instr(2,[条码],"/")-1))+len(MID(条码,Len(left(条码,Instr(2,[条码],"/")-1))+2,Instr(2,MID(条码,Instr(2,[条码],"/")+1,50),"/")-1))+len(MID(条码,Len(left(条码,Instr(2,[条码],"/")-1))+Len(MID(条码,Len(left(条码,Instr(2,[条码],"/")-1))+2,Instr(2,MID(条码,Instr(2,[条码],"/")+1,50),"/")-1))+3,instr(2,MID(条码,Len(left(条码,Instr(2,[条码],"/")-1))+Len(MID(条码,Len(left(条码,Instr(2,[条码],"/")-1))+2,Instr(2,MID(条码,Instr(2,[条码],"/")+1,50),"/")-1))+3,50),"/")-1))+len(MID(条码,len(left(条码,Instr(2,[条码],"/")-1))+len(MID(条码,Len(left(条码,Instr(2,[条码],"/")-1))+2,Instr(2,MID(条码,Instr(2,[条码],"/")+1,50),"/")-1))+len(MID(条码,Len(left(条码,Instr(2,[条码],"/")-1))+Len(MID(条码,Len(left(条码,Instr(2,[条码],"/")-1))+2,Instr(2,MID(条码,Instr(2,[条码],"/")+1,50),"/")-1))+3,instr(2,MID(条码,Len(left(条码,Instr(2,[条码],"/")-1))+Len(MID(条码,Len(left(条码,Instr(2,[条码],"/")-1))+2,Instr(2,MID(条码,Instr(2,[条码],"/")+1,50),"/")-1))+3,50),"/")-1))+4,Instr(2,MID(条码,len(left(条码,Instr(2,[条码],"/")-1))+len(MID(条码,Len(left(条码,Instr(2,[条码],"/")-1))+2,Instr(2,MID(条码,Instr(2,[条码],"/")+1,50),"/")-1))+len(MID(条码,Len(left(条码,Instr(2,[条码],"/")-1))+Len(MID(条码,Len(left(条码,Instr(2,[条码],"/")-1))+2,Instr(2,MID(条码,Instr(2,[条码],"/")+1,50),"/")-1))+3,instr(2,MID(条码,Len(left(条码,Instr(2,[条码],"/")-1))+Len(MID(条码,Len(left(条码,Instr(2,[条码],"/")-1))+2,Instr(2,MID(条码,Instr(2,[条码],"/")+1,50),"/")-1))+3,50),"/")-1))+4,50),"/")-1))+5,50) AS 金额
FROM tbl钣金发货单;
看起来是不是很吓人? 密密麻麻的。 其实如果学会用自定义函数,可以很简单的生成。
上面这种是很规范用/连成的字符,在vba里 用split很容易就可 以分成数组。
所以 我的方式是1先生成自定义函数,2前端直接调用
先在vbe插入一个模块,生成自定义函数 cut
Function cut(x, y)
cut = Split(x, "/")(y - 1) '-1原因是数字是以0开始编号的
End Function
然后在查询中,直接使用cut(字段,序列)取对应序列。
最终代码为:
Select 测试.条码, CDate(cut([条码],1)) AS 客户日期, cut([条码],2) AS 客户编号, cut([条码],3) AS 主订单编号, cut([条码],4) AS 副订单编号, cut([条码],5) AS 金额
FROM 测试;下
附件下载:
点击下载此附件