SQL用文本替换字符串中的int

时间:2018-03-28 13:36:03

标签: sql

我正在尝试进行查询,但在我的结果中,我有一个数字为'2'的列,我希望将其替换为'factura'。

我该怎么做?我想替换Cust。[文档类型]列

   SELECT Detail.[Entry No_],
           'Cliente' AS Tipo,
           Cust.[Customer No_] AS Cliente,
           Detail.[Posting Date] AS DATA,
           Detail.[Document No_] AS Documento,
           Detail.[Amount (LCY)] AS Valor,
           Cust.[Document Type] AS LiqPorTipo,
           Cust.[Document No_] AS LiqPorNDocumento,
           '97' AS Conta,
           'MR' AS Loja,
           'SUPER' AS Utilizador,
           'MR01' AS POS
    FROM dbo.MBS_tabela_21Detailed_NAV16 AS Detail
    INNER JOIN dbo.MBS_tabela_21_NAV16 AS Cust ON Detail.[Cust_ Ledger Entry No_] = Cust.[Entry No_]
    INNER JOIN dbo.Integracao_Periodo_NAV16 AS Integr ON YEAR(Detail.[Posting Date]) = Integr.Ano
    AND MONTH(Detail.[Posting Date]) = Integr.Mes
    WHERE (Detail.[Document No_] LIKE '%REC%'
           OR Detail.[Document No_] LIKE '%L%')
      AND (Detail.[Entry Type] = 2)
      AND (Cust.[Global Dimension 1 Code] = 'LMR')

这导致

359229  Cliente 503392154   2018-03-23 00:00:00.000 1803PAGLEITE37  -2064,62000000000000000000  2   MRVFFT1800012   97  MR  SUPER   MR01

我希望

359229  Cliente 503392154   2018-03-23 00:00:00.000 1803PAGLEITE37  -2064,62000000000000000000  fatura  MRVFFT1800012   97  MR  SUPER   MR01

2 个答案:

答案 0 :(得分:1)

您的查询缺失"详细信息。[条目类型]列"。比较预期的查询结果和您的查询结果我假设你想使用case或create dictionary table来加入它。

最佳选择是创建附加表并存储所有键值转换。

所以你可以拥有 2 - factura

并将其加入您的查询。

如果不是,你应该这样做:

 SELECT Detail.[Entry No_],
           'Cliente' AS Tipo,
           Cust.[Customer No_] AS Cliente,
           Detail.[Posting Date] AS DATA,
           Detail.[Document No_] AS Documento,
           Detail.[Amount (LCY)] AS Valor,
           case when Cust.[Document Type] = 2 then 'factura' else '' end  AS LiqPorTipo,
           Cust.[Document No_] AS LiqPorNDocumento,
           '97' AS Conta,
           'MR' AS Loja,
           'SUPER' AS Utilizador,
           'MR01' AS POS
    FROM dbo.MBS_tabela_21Detailed_NAV16 AS Detail
    INNER JOIN dbo.MBS_tabela_21_NAV16 AS Cust ON Detail.[Cust_ Ledger Entry No_] = Cust.[Entry No_]
    INNER JOIN dbo.Integracao_Periodo_NAV16 AS Integr ON YEAR(Detail.[Posting Date]) = Integr.Ano
    AND MONTH(Detail.[Posting Date]) = Integr.Mes
    WHERE (Detail.[Document No_] LIKE '%REC%'
           OR Detail.[Document No_] LIKE '%L%')
      AND (Detail.[Entry Type] = 2)
      AND (Cust.[Global Dimension 1 Code] = 'LMR')

答案 1 :(得分:0)

因为你想在连接结果上替换一个列。贝娄structure可以帮助你

UPDATE
    Table_A
SET
    Table_A.col1 = Table_B.col1,
    Table_A.col2 = Table_B.col2
FROM
    Some_Table AS Table_A
    INNER JOIN Other_Table AS Table_B
        ON Table_A.id = Table_B.id
WHERE
    Table_A.col3 = 'cool'

基于上面的结构你的sql scritp如下。

UPDATE Detail
SET Detail.[Entry Type] = REPLACE(Detail.[Entry Type], '2', 'factura')

FROM dbo.MBS_tabela_21Detailed_NAV16 AS Detail
INNER JOIN dbo.MBS_tabela_21_NAV16 AS Cust
  ON Detail.[Cust_ Ledger Entry No_] = Cust.[Entry No_]
INNER JOIN dbo.Integracao_Periodo_NAV16 AS Integr
  ON YEAR(Detail.[Posting Date]) = Integr.Ano
  AND MONTH(Detail.[Posting Date]) = Integr.Mes

WHERE (Detail.[Document No_] LIKE '%REC%'
OR Detail.[Document No_] LIKE '%L%')
AND (Detail.[Entry Type] = 2)
AND (Cust.[Global Dimension 1 Code] = 'LMR')