How to use WHERE function in column created from a query?

Asked

Viewed 81 times

-3

select a.campo1, b.campo1,replace(replace(b.campo1,'-',''),'.','') as resultado from dbo.tabela1 a, dbo.tabela2 b 
where a.campo1 = resultado

I am trying to select above, but it displays error "Message 207, Level 16, Status 1, Line 16: Invalid column name 'result'."

How do I turn "requests" into a valid column?

  • 5

    And what comes to be "requests" since such name is not in the command you posted?

  • Requests was the original name of the alias resulting from replace, I forgot to edit that part. (And as it is a confidential Lecta I cannot keep the names)

2 answers

0


Try the following

select a.campo1, b.campo1,replace(replace(b.campo1,'-',''),'.','') as resultado from dbo.tabela1 a, dbo.tabela2 b 
where a.campo1 = replace(replace(b.campo1,'-',''),'.','')

Cannot use alias as parameter

  • But I can’t use the alias as a "fixed element" in that select ? Depending on the result, I would need to use it as a parameter in future selects.

  • Create a view a do select na view can charm the quizer create view field vxpto as select field1 the result ...... / select * from vxpto Where result=42 ....

0

Try:

WITH
    cte AS (SELECT campo1, replace(replace(campo1,'-',''),'.','') as resultado FROM dbo.tabela1)
SELECT cte.campo1, b.campo1, cte.resultado FROM dbo.tabela2 b 
WHERE b.campo1 = cte.resultado;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.