1

I'm trying to do a query but in my result I have a column with the number '2' which I want to be replaced by 'factura'.

How can I do this?? I want to replace Cust.[Document Type] column

   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')

this results in

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

and i want to have

359229  Cliente 503392154   2018-03-23 00:00:00.000 1803PAGLEITE37  -2064,62000000000000000000  fatura  MRVFFT1800012   97  MR  SUPER   MR01
7
  • That is invalid standard SQL. Which DBMS product are you using? Commented Mar 28, 2018 at 13:38
  • What sql platform are you using? What is the error message, if any, returned? Commented Mar 28, 2018 at 13:38
  • Error in FROM clause: near 'SELECT'. Unable to parse query text. Commented Mar 28, 2018 at 13:39
  • And which SQL server do you use? Commented Mar 28, 2018 at 13:41
  • sql server 2016 Commented Mar 28, 2018 at 13:42

2 Answers 2

1

Your query is missing "Detail.[Entry Type] column". Comparing expected query result and your query result I assume you would like to use either case or create dictionary table to join it.

Best option would be to create additional table and store there all key-value translations.

So you could have 2 - factura

And join it in your query.

If not you should do like this:

 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')
Sign up to request clarification or add additional context in comments.

Comments

0

As you want to replace one column base on join result. Bellow structure may help you

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'

Base on above structure your sql scritp is bellow.

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')

5 Comments

Assuming the data type isn't INT.
After join you have value and you know the destination column type, So you can cast it.
This isnt working... Column or expression 'Document Type' cannot be updated. This SQL statement type cannot be used in a view or function. Only a SELECT statement can be used.
I alter the Update table for Cust and the Set field to Cust.[Document Type]
What is your document type? What type of data you want to store in this column. Please check hope types are not mis-match

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.