Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert float latitude and longitude into geography

How to convert float latitude and longitude values into geography type value? I have @lat and @lon variables.

like image 733
Sergey Metlov Avatar asked Aug 01 '11 17:08

Sergey Metlov


2 Answers

With case statement:

CASE
    WHEN ((@Latitude IS NOT NULL) AND (@Longitude IS NOT NULL))
    THEN geography::Point(@Latitude, @Longitude, 4326)
    ELSE NULL           
END

or a variant with if:

 DECLARE @Location geography = NULL
 IF (@Latitude IS NOT NULL AND @Longitude IS NOT NULL)
    SET @Location = geography::Point(@Latitude, @Longitude, 4326);
like image 141
Anderson Avatar answered Oct 23 '22 04:10

Anderson


I've found solution by myself:

geography::STPointFromText(
    'POINT(' + CAST(@lon AS VARCHAR(20)) + ' ' + CAST(@lat AS VARCHAR(20)) + ')', 4326)
like image 33
Sergey Metlov Avatar answered Oct 23 '22 03:10

Sergey Metlov