SQL Server Spatial: Get length of multiple Points - sql-server

I have a table containing many SQL Server Geography Coordinates (geography datatype). The points are correctly ordered in the table. I want to combine these points to a line and calculate the total length of the polyline.
My idea was the following:
UNION the Geography Coordinates. (this creates a MULTI POINT.)
Convert the MULTI POINT to a LINE STRING.
Use .STLength() to get the length
Is there an efficient way to union existing point without constructing a LINESTRING from a string?
I am using SQL Server 2012.

Related

In SQL Server, how can I execute a math expression stored in string for an entire column?

I have a column in an SQL server table that has stored math expressions in string, in the form of "1X1350+3X400". I need to create a new column with the outcome of the calculation, in this case it should be 2,550.
I have been able to get this outcome for single values using sp_executesql, but only for single cells, not for the entire column, which is what I need.
Any suggestions will be greatly appreciated!

SQL SSRS Unable to determine the spatial data type in the specified dataset field: GEO

I need some help, I have a stored procedure(SP) that contains NO parameters in SSMS. The SP generates spatial data by joining 2 tables one table containing two fields needed for labelling purposes and the other from a table that containsthe GEO field.
This SP populates spatial data in SSMS which I can view and interact with. All seems well so far...
I then use SSRS to create a data source and a dataset of the said stored procedure. Following this I create a new .rdl and insert a map choosing SQL Server spatial query as the source then selecting the stored procedure dataset. Following this SSRS attempts to join the data and I then receive the error from the Microsoft SQL Server Report Designer:
Unable to determine the spatial data type in the specified dataset field: GEO
I have searched the the internet for answers already and none suit predominately because they all refer to parameters causing the issue however my query does not contain nor pass any. I have tried the geo field as both a geography and geometry datatype no avail.
Any help or advice would be great.
I have just been playing with a Heat Map in SSRS using SQL Jason's example (http://www.sqljason.com/2012/03/heat-maps-for-ssrs-using-map-control.html).
It looks like your data doesn't have enough points. According to Teo Lachev (http://prologika.com/CS/blogs/blog/archive/2009/08/30/heat-maps-as-reports.aspx):
SQL Server describes polygons in Well-Known Text (WKT) standard
sponsored by Open Geospatial Consortium. For instance, a polygon is
described as five (surprise) points on the coordinate system, as
shown below. The fifth point is the same as the first point.
The example format you gave only had four points. The first and last are the same so there seems to be a missing point or you are not graphing rectangles. It doesn't look like the Spatial Data supports any other shapes. The Map wizard only shows rectangles in the Wizard when creating the map.

SQL Server datatype for DB2 column "Char() for BIT Data"

I googled but failed to find an answer for the following question.
I have a DB2 database column of datatype “Char(100) for BIT Data“. It stores encrypted value of employee ID. I need to store this data in SQL Server.
What should be the datatype for this column in SQL Server?
Is there any formatting needed before inserting into SQL Server?
The column in your DB2 table is an ordinary character string, except that the for BIT Data part tells DB2 to treat that string as arbitrary binary data, rather than text. This matters mainly for sorts and comparisons. DB2 (as normally configured) would sort and compare strings alphabetically. A capital A would come before a lowercase b. But for BIT Data strings are compared by the underlying numeric value of the characters. Capital A would come after lowercase z.
In SQL server, there is a BINARY datatype for this, so you would probably use BINARY(100). No formatting should be necessary, as you probably want the value of raw binary data to stay exactly the same.

SQL Server distance search

I have a Microsoft SQL Server database with a table of Locations. Each location has its address and latitude and longitude coordinates.
In my application, the user can input a zipcode and we return a list of close by locations.
This is my approach.
a) Using a zipcode DB I search the lat,lon for the zipcode (this is the center point).
b) I run a search like this
SELECT Position_ID, distance(pos_lon,pos_lat,zip_lon,zip_lat) dist
FROM Positions
ORDER BY dist
"distance" is a function that calculates the distance between two points.
The problem is that as my location DB increases the time to run these searches is starting to grow.
Is there a better approach?
If you're using SQL Server 2008, you probably want to look into the geography column type, the STDistance function and spatial indexes.
Spatial Indexing Overview
Geography Methods Supported by Spatial Indexes
How to: Create a Spatial Index
I would do a calculation of the box surrounding your zip code at the specified distance to get the lat/lon values for the four corners. Then do a simple comparison of your position values to select those which fall inside the square. This way, you don't have to calculate the distance from your zip code to every point in your db for every search.
Which version of SQL server? If 2008 have a look at the spatial type.
http://www.microsoft.com/sqlserver/2008/en/us/spatial-data.aspx
EDIT: Also limiting that query with a where would probably help as you likely don't want to go to far in any particular direction.

SQL Server 2005 import a csv file , rows in correct order

I use SQL Server wizard to import csv files, however the rows imported appears in different order. How can I say to SQL Server to import rows in correct order.
In databases there is no such thing as order of records, as opposed to numbered spreadsheet rows, you need a column to order by.
You would use something like
SELECT * FROM myTable ORDER BY SomeColumn
It's probably importing in the correct order, but SQL server is retrieving them in a different order. Try adding a unique column on your table and then select the rows using that as an order by.
Getting back to the original question, I'm exporting email from Outlook by first sequencing the messages as desired, then selecting multiples a month at a time from the desired folder, then doing the FILE SAVE AS option instead of the Outlook Export function which appears to keep the correct order that far. They are exported to text with no delimiters used.
I created tables in SQL Server that contain only an identity column and a varchar(4000) column. Use row delimiter CRLF and no field delimiter for the import. So far, this appears to be keeping each line in the messages in the correct order. you can create a unique index on the table OR specify ORDER BY the ident column and view emails nicely in what I THINK so far is the correct sequence.
I'm extracting character and numeric data from certain randomly placed rows within each message, so plan to do this with functions LTRIM and RTRIM, then use a pointer to each character until I find the next space, checking each group for being character or numeric, converting numeric data to decimal, and thus parsing the data from the strings to the appropriate data type variable. Working left to right, when I discover a space, I can LTRIM the string to itself and easily move the remaining data to the left. I'm working with dates, alphanumeric stock symbols, and decimal quantities and prices, so each will be identified with function ISNUMERIC before conversion to avoid errors and identify bad data.
Repeated use of the LTRIM will easily move on the next data item in the string, reducing the individual character scanning of intervening spaces.

Resources