Search code examples
sql-servert-sqlgeospatialgeography

SQL Geography string parsing


Why does this work

select geography::STGeomFromText('POINT(-77.010996 38.890358)',4326)

but this doesn't

declare @Latitude decimal(9,6) = 38.890358  
declare @Longitude decimal(9,6) = -77.010996

select geography::STGeomFromText('''POINT(' + 
cast(@Longitude as nvarchar(15)) + ' ' + 
cast(@Latitude as nvarchar(15)) +')''',4326)

What am I missing, they seem to be effectively the same thing.


Solution

  • Get rid of the extra quotes at the begining and end

    declare @Latitude decimal(9,6) = 38.890358  
    declare @Longitude decimal(9,6) = -77.010996
    
    select geography::STGeomFromText('POINT(' + 
    cast(@Longitude as nvarchar(15)) + ' ' + 
    cast(@Latitude as nvarchar(15)) +')',4326)