将数据类型varchar转换为数字CAST时无效

时间:2012-06-04 21:18:22

标签: sql-server-2005

我知道这就像死马一样被打败了。然而,无论我如何切片,投下它或转换它我都有同样的问题。 将数据类型varchar转换为数字时出错。

SELECT  property_id, property_case_number, property_address, property_city, 
property_state, property_zip, property_lon, property_lat 
FROM property 
WHERE (property_active = 1) 
AND 
(property_county = (SELECT property_county FROM property AS property_1 
WHERE (property_id = 9165))) 
AND 
(property_id <> 9165)
AND
property_lon IS NOT Null 
AND 
property_lat IS NOT Null 
AND 
dbo.LatLonRadiusDistance(
CONVERT(DECIMAL(15,12),(select property_lat from property where property_id = 9165)), 
CONVERT(DECIMAL(15,12),(select property_lon from property where property_id = 9165)), 
property_lat,property_lon) <= '5'

一旦我在最后添加dbo.LatLonRadiusDistance,我就会遇到这个问题。

dbo.LatLonRadiusDistance比较lat&amp;以英里为单位的距离。

FUNCTION [dbo].[LatLonRadiusDistance] 
(
@lat1Degrees decimal(15,12),
@lon1Degrees decimal(15,12),
@lat2Degrees decimal(15,12),
@lon2Degrees decimal(15,12)
)
RETURNS decimal(9,4)
AS
BEGIN

DECLARE @earthSphereRadiusKilometers as decimal(10,6)
DECLARE @kilometerConversionToMilesFactor as decimal(7,6)
SELECT @earthSphereRadiusKilometers = 6366.707019
SELECT @kilometerConversionToMilesFactor = .621371

-- convert degrees to radians
DECLARE @lat1Radians decimal(15,12)
DECLARE @lon1Radians decimal(15,12)
DECLARE @lat2Radians decimal(15,12)
DECLARE @lon2Radians decimal(15,12)
SELECT @lat1Radians = (@lat1Degrees / 180) * PI()
SELECT @lon1Radians = (@lon1Degrees / 180) * PI()
SELECT @lat2Radians = (@lat2Degrees / 180) * PI()
SELECT @lon2Radians = (@lon2Degrees / 180) * PI()

-- formula for distance from [lat1,lon1] to [lat2,lon2]
RETURN ROUND(2 * ASIN(SQRT(POWER(SIN((@lat1Radians - @lat2Radians) / 2) ,2)
    + COS(@lat1Radians) * COS(@lat2Radians) * POWER(SIN((@lon1Radians - @lon2Radians) / 2), 2)))
    * (@earthSphereRadiusKilometers * @kilometerConversionToMilesFactor), 4)

END

我确定这与

有关
(select property_lat from property where property_id = 9165)

但无论我如何演员或转换它都不会改变事物。

如果我自己运行该函数,则不会出错。

任何人都有任何见解?

这是一个示例行

8462    023-125514  15886 W MOHAVE ST   GOODYEAR    AZ  85338-0000  -112.400297000000   33.429041000000

property_lat&amp; property_lon是varchar(50)

2 个答案:

答案 0 :(得分:0)

您很可能期望布尔短路来挽救评估WHERE子句的顺序。这是一个已知的谬误:SQL中不保证布尔运算符短路。请参阅On SQL Server boolean operator short-circuit以获得讨论并证明查询优化器可以跳过布尔短路。类似的主题是T-SQL functions do no imply a certain order of execution。它的要点是SQL是声明性语言,而不是命令式语言。

在您的情况下,可能会为ID property_id = 9165property_active=1 不同的属性调用您的演员和转换,并且可能会尝试将不是数字的字符串值转换为一个数字,因此你看到的例外。由于问题描述中缺少大量信息(如所涉及的所有对象的精确定义,包括所有表,索引,列类型等),因此难以进行精确诊断。

最好的途径是升级到SQL Server 2008并使用内置geography内置支持的内置STDistance类型:

  

这是近似于测地距离的近似值。的偏差   STDistance()在普通地球模型上的精确测地距离   不超过.25%。

答案 1 :(得分:0)

在使用查询后,我开始工作了。

SELECT  [property_id], [property_case_number], [property_address], [property_city], 
[property_state], [property_zip], [property_lon], 
[property_lat] 
FROM property 
WHERE ([property_active] = 1) 
AND 
([property_county] = (SELECT b.property_county FROM property b WHERE (b.property_id =     @prop_id))) 
AND 
([property_id] <> @prop_id)
AND
[property_lon] IS NOT Null 
AND 
[property_lat] IS NOT Null 

AND
dbo.LatLonRadiusDistance(
(SELECT c.property_lat FROM property c WHERE (c.property_id = @prop_id)),
(SELECT d.property_lon FROM property d WHERE (d.property_id = @prop_id)),
CAST([property_lat] as FLOAT), 
CAST([property_lon] as FLOAT)) <= 5

添加[]似乎已经避开了我遇到的问题。