SELECT with a Replace()

Posted by andyjohnson on Stack Overflow See other posts from Stack Overflow or by andyjohnson
Published on 2010-04-16T10:14:49Z Indexed on 2010/04/16 10:23 UTC
Read the original article Hit count: 246

Filed under:
|
|
|

I have a table of names and addresses, which includes a postcode column. I want to strip the spaces from the postcodes and select any that match a particular pattern. I'm trying this (simplified a bit) in T-SQL on SQL Server 2005:

SELECT Replace(Postcode, ' ', '') AS P
FROM Contacts
WHERE P LIKE 'NW101%'

But I get the following error;

Msg 207, Level 16, State 1, Line 3
Invalid column name 'P'.

If I remove the WHERE clause I get a list of postcodes without spaces, which is what I want to search. How should I approach this? What am I doing wrong?

© Stack Overflow or respective owner

Related posts about sql

Related posts about sql-server