就是要把location这个字段里的值,分折到每一行。。
如何在ACCESS里处理?
比如,
AutoID PartID Location
----- -------------- --------------
14 HAMP-025-0002 C190 C234 C237
15 HAMP-025-0038 D27 D28 D29
...变成PartID Location
-------------- --------------
HAMP-025-0002 C190
HAMP-025-0002 C234
HAMP-025-0002 C237
HAMP-025-0038 D27
HAMP-025-0038 D28
HAMP-025-0038 D29
.....
如何在ACCESS里处理?
比如,
AutoID PartID Location
----- -------------- --------------
14 HAMP-025-0002 C190 C234 C237
15 HAMP-025-0038 D27 D28 D29
...变成PartID Location
-------------- --------------
HAMP-025-0002 C190
HAMP-025-0002 C234
HAMP-025-0002 C237
HAMP-025-0038 D27
HAMP-025-0038 D28
HAMP-025-0038 D29
.....
--AutoID PartID Locationselect a.AutoID,a.PartID,
Location=substring(a.Location,b.number,charindex(' ',a.Location+' ',b.number)-b.number)
from tb a,master..spt_values b
where b.[type] = 'p' and b.number between 1 and len(a.Location)
and substring(' '+a.Location,b.number,1) = ' '
你搜下分隔字符串
但是access不知道怎么弄
SELECT PartID, left(Location,InStr(Location," ")) AS a1,mid(Location,InStr(Location," ")+1) as a2 FROM tb
) AS t
union all
SELECT PartID, Left(a2,InStr(a2," ")) AS L from(
SELECT PartID, left(Location,InStr(Location," ")) AS a1,mid(Location,InStr(Location," ")+1) as a2 FROM tb
) AS t
union all
SELECT PartID, left(Location,InStr(Location," ")) AS L FROM tb order by PartID
insert into @t(autoid,partid,location)
select 14,'HAMP-025-0002','C190 C234 C237' union all
select 15,'HAMP-025-0038','D27 D28 D29';
--select * from @t;
select a.autoId,a.partId,b.vx
from
(select autoid,partid,cast('<root><v>'+REPLACE(location,' ','</v><v>')+'</v></root>' as xml) as x from @t) a
outer apply(
select vx=N.v.value('.','varchar(100)') from a.x.nodes('/root/v') N(v)
) b/*
autoId partId vx
----------- -------------------------------------------------- -----------
14 HAMP-025-0002 C190
14 HAMP-025-0002 C234
14 HAMP-025-0002 C237
15 HAMP-025-0038 D27
15 HAMP-025-0038 D28
15 HAMP-025-0038 D29*/