简体   繁体   中英

SQL Server 2008 R2: Converting data type varchar to geometry

I am updating table with some values with Geometry Path Condition.

Here in the following example: I am passing table name as @Table , @ColumnA (for setting new values), @GeoPath (to check in the condition) for the dynamic scipt as shown below:

@Table = 'Table1'

@ColumnA = 'A'

@GeoPath = 0xE610000001040500000061574D5E31433140000000003EAF52405E3B0D825B92314000000000AACA52407BEECBC0FB263140000000001

SET @query =    'Update ['+@Table+']
                 SET ColumnA = '''+@ColumnA+'''
                 WHERE CONVERT(Geometry,'+CAST(@GeoPath AS varchar(MAX))+').STIntersects(geometry::Point(Latitude,Longitude, 4326))= 1';
PRINT(@query);

EXECUTE(@query);

But getting the error:

Error converting data type varchar to geometry.

When working with GEOGRAPHY or GEOMETRY you need to specify more information instead of just converting from one type to another.

For example this is conversion of polygon binary data back into geography type.

DECLARE @g geography; 
SET @g = geography::STPolyFromWKB(0x01030000000100000005000000F4FDD478E9965EC0DD24068195D3474083C0CAA145965EC0508D976E12D3474083C0CAA145965EC04E62105839D44740F4FDD478E9965EC04E62105839D44740F4FDD478E9965EC0DD24068195D34740, 4326);
SELECT @g

here is same example but this time from text

DECLARE @g_text geography; 
SET @g_text = geography::STPolyFromText('POLYGON ((-122.358 47.653, -122.348 47.649, -122.348 47.658, -122.358 47.658, -122.358 47.653))', 4326);
SELECT @g_text

I'm not sure what type of binary data you have in order to convert it. Just make sure that you are using correct conversion method.

Read more on geography::STPolyFromText

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM