One cannot reference the computed column in a WHERE
clause because the WHERE
clause is evaluated before the SELECT
in the logical processing order of the query.
Use a common table expression (WITH
) so that the computed column may be used later in a WHERE
claause:
WITH cte_example AS (
SELECT PhoneNumber, REPLACE(REPLACE(REPLACE(PhoneNumber, ')', ''), '(', ''), '-', '') AS Justnum
FROM Person.PersonPhone
)
SELECT PhoneNumber, Justnum
FROM cte_example
WHERE Justnum <> '10';
If you have multiple statements in the batch, be sure the preceding statement is terminated with a semi-column to avoid syntax errors.