Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting SQL Geography Lat/Long to VARCHAR without loss of precision

I have a geography column called Location. I need to SELECT the Location.Lat and Location.Long as a single VARCHAR result. Using the following I am losing precision:

Query:

SELECT CONVERT(VARCHAR(MAX), Location.Lat) + ' ' + CONVERT(VARCHAR(MAX), Location.Long)

Original Location.Lat: 52.2708633333333

Original Location.Long: -9.73093666666667

Result form above: 52.2709 -9.73094

I understand that this is due to Lat and Long being float and not having precision specified so possible workaround would be to force it to decimal first with a specified precision and then convert that to varchar. This seems.....clunky, and having to pick a random precision will force either

(a) loss of trailing values if it is too low or

(b) additional unnecessary trailing values.

Please tell me I'm missing a function or conversion method to return these as accurate varchar representations!

like image 215
KrustyGString Avatar asked Mar 16 '23 11:03

KrustyGString


1 Answers

You need something like this:

SELECT CONVERT(VARCHAR(40),CAST(Location.Lat AS decimal(18,14))) + ' ' 
+ CONVERT(VARCHAR(40),CAST(Location.Long AS decimal(18,14))) 
like image 187
Thanos Markou Avatar answered Mar 18 '23 07:03

Thanos Markou