sql零在比较2记录集时领先

时间:2011-12-22 03:06:44

标签: sql

我有一个问题是比较2组数据是否有前导零 在Inventory_NBP中,有000000000000909120 但是,在PDM_Analysis中,只有909120没有前导零 由于未满足“IN”条件,下面的当前查询无法检索任何000000000000909120或909120。

如何修改以下查询以满足我的要求?

sel * FROM Inventory_NBP.v_dmnd_rsrv_dpnd_rqr_mrp
WHERE plnt_id ='WA01' 
and mtrl_id in('G29329-001', '000000000000909120', '13-0006-001') 
and 
 (mtrl_id, plnt_id)
      IN
      (    SELECT itm_cd, sap_plnt_cd
           FROM PDM_Analysis.v_itm_plnt_extn
      )

2 个答案:

答案 0 :(得分:0)

进行铸造。这可以解决您的问题。请检查。

SELECT * FROM Inventory_NBP.v_dmnd_rsrv_dpnd_rqr_mrp
WHERE plnt_id ='WA01' 
and mtrl_id in('G29329-001',CAST(CAST('000000000000909120' AS INT) AS VARCHAR(10)), '13-0006-001') 
and 
 (mtrl_id, plnt_id)
      IN
      (    SELECT itm_cd, sap_plnt_cd
           FROM PDM_Analysis.v_itm_plnt_extn
      )

答案 1 :(得分:0)

以下是如何通过填充(SQL Server 2008语法)匹配值而不管前导零的示例:

WITH T1
     AS 
     (
      SELECT * 
        FROM (
              VALUES ('000000000000909120'),
                     ('00000000099'),
                     ('000000055'), 
                     ('22'),
                     ('152')
             ) AS T (data_col)
     ),
     T2
     AS 
     (
      SELECT * 
        FROM (
              VALUES ('909120'),
                     ('99'),
                     ('00055'), 
                     ('0000000022'), 
                     ('152')
             ) AS T (data_col)
     )     
SELECT *
  FROM T1 INNER JOIN T2
          ON T1.data_col 
                = REPLICATE('0', LEN(T1.data_col) - LEN(T2.data_col)) 
                     + T2.data_col
UNION
SELECT *
  FROM T1 INNER JOIN T2
          ON T2.data_col 
                = REPLICATE('0', LEN(T2.data_col) - LEN(T1.data_col)) 
                     + T1.data_col;