How to use alias column name in where clause in SQL Server

后端 未结 5 1823
时光说笑
时光说笑 2020-11-27 06:49

When I tried to perform the below code in SQL Server 2005 I am getting the error

Invalid column name DistanceFromAddress

Code:<

5条回答
  •  眼角桃花
    2020-11-27 06:52

    You can't use aliased columns in a WHERE clause. You can try using a derived table. Perhaps something like this (sorry, not tested):

    SELECT * FROM
    (SELECT SQRT(POWER(cast(Program_Latitude as float) - cast('41.5126237' as float), 2) +   
     POWER(cast(Program_Longitude as float) - cast('-81.6516411' as float), 2)) * 62.1371192 
     AS DistanceFromAddress from tblProgram) mytable
    WHERE DistanceFromAddress < 2
    

提交回复
热议问题