3

I have imported a csv file containing spatial area information in varchar, then converted varchar(max) values to varbinary(max) by adding '0x' to varchar(max) values prior to conversion. By then, apart from the '0x' in the beginning, the data in varbinary(max) column looks exactly the same as the varchar(max) one in converted to text.

Now I run the following script:

select geometry::STGeomCollFromWKB(wkb, 4326) from dbo.MyTable

where WKB is the varbinary(max) column. Running the above script throws this error: 'The well-known binary (WKB) input is not valid'

The source of data is from Open Street Map so no doubt they are correct area data. So I assume there must be something wrong in what I am doing or I am missing some point to convert WKB to geometry data type.

Could anyone help please?

Rez.Net
  • 1,354
  • 2
  • 19
  • 28

1 Answers1

1

I assume the problem is when converting the varchar data to varbinary you are converting the actual character representation of the binary data, rather than just changing the type to binary.

Eg, if you have the data 0xDEADBEEF in your varchar column, then doing convert(varbinary(max), 'DEADBEEF') will convert the ascii character representations into binary.

What you want to do instead is convert the hex string into binary, which is possible using the style parameter of convert.

SELECT convert(varbinary(max), 'DEADBEEF', 2)

should do what you want to convert your varchar wkb data into real binary.

stevehem
  • 671
  • 3
  • 6
  • I've got a similar question... They've provide me a table which contains Geometries of *VARBINARY(MAX)* datatype, instead of *Geometry*. What can I do to cast them as any spatial data type? I've tried [STGeomFromWKB](https://learn.microsoft.com/en-us/sql/t-sql/spatial-geometry/stgeomfromwkb-geometry-data-type) with no luck... – joninx Jun 06 '17 at 14:40