在split语句SQL

时间:2015-09-18 19:18:40

标签: sql sql-server-2008 delimiter

将逗号分隔的字符串拆分为列后,我想将结果与另一个表中的另外两列相结合。

     SELECT A.ID,  
 Split.a.value('.', 'VARCHAR(100)') AS SUPPLIER_SPECIALTY 
 FROM  
 (
     SELECT ID,
         CAST ('<M>' + REPLACE(SUPPLIER_SPECIALTY, ',', '</M><M>') + '</M>' AS XML) AS SUPPLIER_SPECIALTY  
     FROM  JOA_COMPANY_DEMO A 
     WHERE SUPPLIER_SPECIALTY != '' 

 ) AS A CROSS APPLY SUPPLIER_SPECIALTY.nodes ('/M') AS Split(a)

这是我加入表格的代码

 Select B.Code, B.Description FROM vBo B
 INNER JOIN JOA_COMPANY_DEMO A
 ON A.SUPPLIER_SPECIALTY=B.Code 
 WHERE B.TableName = 'SUPPLIER_SPECIALTY'

这就是我所拥有的

ID      SUPPLIER_SPECIALTY
0520xxx 0001
0520xxx 0002
0520xxx 0003

这就是我试图与供应商匹配代码

   ID      SUPPLIER_SPECIALTY  Code Description
    0520xxx 00016              00016    Clocks
    0520xxx 00016              00016    Clocks
    0520xxx 00003              00003    soose      

1 个答案:

答案 0 :(得分:1)

你使用派生表:

 Select B.Code, B.Description 
 FROM vBo B
 INNER JOIN (
         SELECT A.ID,  
         Split.a.value('.', 'VARCHAR(100)') AS SUPPLIER_SPECIALTY 
         FROM  
         (
             SELECT ID,
                 CAST ('<M>' + REPLACE(SUPPLIER_SPECIALTY, ',', '</M><M>') + '</M>' AS XML) AS SUPPLIER_SPECIALTY  
             FROM  JOA_COMPANY_DEMO A 
             WHERE SUPPLIER_SPECIALTY != '' 

         ) AS A CROSS APPLY SUPPLIER_SPECIALTY.nodes ('/M') AS Split(a)
 ) A
 ON A.SUPPLIER_SPECIALTY=B.Code 
 WHERE B.TableName = 'SUPPLIER_SPECIALTY'