从表中选择最近值

从表中选择最近值

本文介绍了从表中选择最近值 - 将数据类型nvarchar转换为数字时出错。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我想从表中选择最近的下限和上限值,我在下面查询使用

Hi All,

I want to choose nearest lower and upper values from table for that I am using below query

DECLARE @lat1 decimal(18,6)
DECLARE @lon1 decimal(18,6)

SET @lat1 = 17.43969
SET @lon1 = 78.4842

SELECT TOP 5 ID, Cl_Name,lat1,lon1
from MyTable
ORDER BY ABS(cast(lat1 as decimal(18,6)) - @lat1)



如果我运行上面的查询,它会给我以下错误 -

转换数据类型时出错 nvarchar 数字





这个查询有什么问题


If I run above query, it gives me below error-
Error converting data type nvarchar to numeric.


What is wrong in this query

推荐答案


SELECT ID, Cl_Name,lat1,lon1
FROM MyTable
WHERE ISNUMERIC(lat1) <> 1;


DECLARE @lat1 decimal(18,6)
DECLARE @lon1 decimal(18,6)

SET @lat1 = 17.43969
SET @lon1 = 78.4842

SELECT TOP 5 ID, Cl_Name,@lat1,@lon1
from MyTable
ORDER BY ABS(cast(@lat1  as decimal(18,6)) - @lat1)





由: Nirav Prabtani

设置语言代码块

[/编辑]



By :Nirav Prabtani
Set language code block
[/Edit]


这篇关于从表中选择最近值 - 将数据类型nvarchar转换为数字时出错。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 05:50