I’m using an alias to refer to a computed column. Here is a snippet from the actual code I’m trying to make work, to compute similarity and return matches where the similarity score is 3 or higher.
select [FirstName], difference([FirstName], 'mitch') as similarity
from [Dev].[dbo].[Name]
where similarity > 2
order by similarity desc
Exception Message:
Invalid column name ‘similarity’.
As similarity is not a real column, how would I make this work?
Column aliases and computations are performed in the projection (
SELECT) phase of the query, which occurs after the selection (WHEREandJOIN) phase. Because of this, they can’t be referenced in theWHEREclause or in aJOINcondition because they do not yet exist. You can either use your query with theSELECTclause as a subquery or you can duplicate the computation in theWHEREclause:or