Sergey Metlov
Sergey Metlov

Reputation: 26291

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.

Upvotes: 9

Views: 9778

Answers (2)

Anderson
Anderson

Reputation: 1011

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);

Upvotes: 19

Sergey Metlov
Sergey Metlov

Reputation: 26291

I've found solution by myself:

geography::STPointFromText(
    'POINT(' + CAST(@lon AS VARCHAR(20)) + ' ' + CAST(@lat AS VARCHAR(20)) + ')', 4326)

Upvotes: 2

Related Questions