TheGeeky
TheGeeky

Reputation: 971

Convert string coordinates to geography

I have string coordinates in my table but I want to do some geographical functionalities, So I need first to convert this string value to geography.like this:

geography::STGeomFromText('POINT([location])', 4326).MakeValid().STDistance(@p)

but for sure this code didn't work as it needs here point not string coordinates.

The full code:

geography::STGeomFromText('POINT([location])', 4326).MakeValid().STDistance(@p);

DECLARE @p geography;  
            SET @p = geography::STGeomFromText('POINT({$Lon} {$Lat})', 4326);
            Select TOP 1 id, location from branches where {$location} <= {$this->radius} order by {$location}

Upvotes: 2

Views: 1478

Answers (1)

pwang
pwang

Reputation: 71

It's a little difficult to provide a perfect solution without seeing how the code is interporlating the variables, but SQL could be having issues recognizing your long/lat as strings with the STGeomFromText method.

Could you try something like this:

SELECT geography::STGeomFromText('POINT(' + CAST([$Long] AS VARCHAR(20)) + ' ' + CAST([$Lat] AS VARCHAR(20)) + ')', 4326)

Or more succinctly:

SELECT geography::Point([$Lat], [$Long], 4326)

Upvotes: 1

Related Questions