Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update statement- Geography column - sql server

Is it different to update geography column in sql server than a regular field( varchar....). Can you please provide a sample statement to do this. thanks.

like image 420
dotnetrocks Avatar asked Nov 25 '11 09:11

dotnetrocks


People also ask

How do you add a column in an update statement?

If we want to add multiple columns to the existing table using any single statement, we can use the below syntax: ALTER TABLE table_name (Name of the table) ADD [COLUMN] column_definition, (for adding column)

What is Stgeomfromtext?

Returns a geometry instance from an Open Geospatial Consortium (OGC) Well-Known Text (WKT) representation augmented with any Z (elevation) and M (measure) values carried by the instance.


2 Answers

I am not sure if this is the answer you are looking for - but as I would say, the main difference is that when updating a "regular field", you typically provide directly the new value - for example:

UPDATE mytable SET name = 'John' WHERE id = 1

When updating a geography column, you probably cannot provide the value directly (since it is a very long hexadecimal number, which encodes the geoghraphy information) but you will want to compute it from some other values (which can, but do not have to be columns of the same table), e.g.:

UPDATE mytable SET gps=geography::STPointFromText('POINT(' + lng + ' ' + lat + ')', 4326) 

where lng and lat are varchar values specifying the GPS coordinates in a "human-readable" format (like lat = '48.955790', lng = '20.524500') - in this case they are also columns of mytable.

like image 181
Helena Avatar answered Oct 15 '22 18:10

Helena


If you have Latitude and Longitude as decimals, you can update a geography column as shown below:

DECLARE @latitude DECIMAL(15,6)
    ,@longitude DECIMAL(15,6);

SET @latitude = 29.938580;
SET @longitude = -81.337384;

UPDATE Properties
SET Geog = GEOGRAPHY::Point(@latitude, @longitude, 4326)
WHERE PropertyID = 858;
like image 27
Paul Van Gundy Avatar answered Oct 15 '22 18:10

Paul Van Gundy