Connecting Tech Pros Worldwide Help | Site Map

Using an alises in the WHERE clause

  #1  
Old June 25th, 2009, 04:19 PM
Newbie
 
Join Date: Jun 2009
Posts: 6
Hi,

I'm working in a SQL Server 2008 enviroment trying to get the code below to work.

The error I get is "Invalid column name 'PHONE'."

Thanks in advance for your help.

SELECT DISTINCT TOP 5 REPLACE(HOME_PHONE,'-','') AS PHONE
FROM X
WHERE HOME_PHONE IS NOT NULL AND DO_NOT_CALL IN ('Y','X','1') AND PHONE NOT IN
(SELECT DISTINCT PHONENUMBER FROM X WHERE PHONENUMBER IS NOT NULL)
  #2  
Old June 25th, 2009, 05:31 PM
ck9663's Avatar
Expert
 
Join Date: Jun 2007
Posts: 1,914
Provided Answers: 1

re: Using an alises in the WHERE clause


On your query, do you have the PHONE column in your table x?

SELECT DISTINCT TOP 5 REPLACE(HOME_PHONE,'-','') AS PHONE
FROM X
WHERE HOME_PHONE IS NOT NULL AND DO_NOT_CALL IN ('Y','X','1') AND PHONE NOT IN
(SELECT DISTINCT PHONENUMBER FROM X WHERE PHONENUMBER IS NOT NULL)

Happy Coding!

--- CK
  #3  
Old June 25th, 2009, 06:48 PM
Newbie
 
Join Date: Jun 2009
Posts: 6

re: Using an alises in the WHERE clause


No. HOME_PHONE is the field. PHONE is the alises I'm using in the SELECT statement.

I can explain alittle more. The reason for the REPLACE function is that in the 1st table some of the phone numbers have -'s in them where in the 2nd table (in my sub-query) they do not.
  #4  
Old June 25th, 2009, 09:06 PM
Newbie
 
Join Date: Jun 2009
Posts: 6

re: Using an alises in the WHERE clause


Found a work-around the alises to fix this issue.

SELECT DISTINCT REPLACE(HOME_PHONE,'-','')
FROM X
WHERE HOME_PHONE IS NOT NULL AND DO_NOT_CALL IN ('Y','X','1') AND REPLACE(HOME_PHONE,'-','') NOT IN
(SELECT DISTINCT PHONENUMBER FROM X WHERE PHONENUMBER IS NOT NULL)
  #5  
Old July 17th, 2009, 07:05 PM
Newbie
 
Join Date: Jul 2009
Posts: 9

re: Using an alises in the WHERE clause


For aliases in WHERE clause you can use two level select query.

SELECT * FROM (
SELECT DISTINCT REPLACE(HOME_PHONE,'-','') a
FROM X )
WHERE HOME_PHONE IS NOT NULL AND DO_NOT_CALL IN ('Y','X','1')
AND a NOT IN
(SELECT DISTINCT PHONENUMBER FROM X WHERE PHONENUMBER IS NOT NULL)


Hope this will help you .

-Pankaj Tambe.
Reply