我有一个成员表,当显示结果时,我想先显示最近的用户,但有些用户还没有设置位置,这会引发错误。
SELECT ROUND(geography::Point(Lat, Long, 4326).STDistance(geography::Point("&SearchLat&", "&SearchLong&", 4326))/1000,0) as Distance FROM users
如果遇到空字段,我会收到以下错误。
''geography::Point'' failed because parameter 1 is not allowed to be null.
我希望所有用户都能显示,但最接近的是那些用户。
有没有一种简单的方法可以解决这个问题?
答案 0 :(得分:3)
您可以将所有位于北极的未知位置的用户放在:
SELECT ROUND(geography::Point(COALESCE(Lat,89.9), COALESCE(Long,0), 4326).STDistance(
geography::Point("&SearchLat&", "&SearchLong&", 4326))/1000,0) as Distance
FROM users
或在非洲海岸附近:
SELECT ROUND(geography::Point(COALESCE(Lat,0), COALESCE(Long,0), 4326).STDistance(
geography::Point("&SearchLat&", "&SearchLong&", 4326))/1000,0) as Distance
FROM users
或者选择其他一些默认的纬度和经度,这样他们几乎肯定会远离其他用户。
答案 1 :(得分:1)
SELECT ROUND(geography::Point(Lat, Long, 4326).STDistance(geography::Point("&SearchLat&",
"&SearchLong&", 4326))/1000,0) as Distance FROM users
WHERE Lat IS NOT NULL AND Long IS NOT NULL