Point In Polygon With TSQL

Oct 18, 2005

I've got a table with lat/longs for specific points:

CREATE TABLE [Point] (
[PointID] [int],
[Lat] [numeric](10, 6),
[Lon] [numeric](10, 6)
)


I have another table with polygon borders as lat/longs:

CREATE TABLE [Polygon] (
[PolygonID] [int],
[PointNum] [int],
[Lat] [numeric](10, 6),
[Lon] [numeric](10, 6)
)


What I need to do, is come up with a script that will determine which Polygon contains each point. I'd like to be able to assign a PolygonID to each record in the Point table, or give it a 0 if there is no polygon that contains that point.

I'm looking for a way to do this within SQL Server (or possibly with the use of an extended stored procedure if necessary).

Thanks for your help and suggestions!

Tyler

View 3 Replies


ADVERTISEMENT

SQL 2012 :: Determining Which Polygon Contains A Point

Nov 5, 2015

I have a table that contains spatial points and the name of the polygon they belong to (geography and varchar columns). I need to write a function to accept a point and determine which polygon contains that point. How can I write the query so that it will search through each polygon (derived from the geography points found in the table) and return the name of the polygon that contains that point?

View 2 Replies View Related

SQL Server 2012 :: Geography Data Type - Test If Point Is Inside A Closed Polygon

Dec 5, 2013

I have a closed polygon that coincidently is in the shape of Iowa :) I have a point that is within the state and a point WELL outside it, but I get weird results that I don't expect when I try to get it to tell me that the point is within the polygon. Here is some basic code, with long coordinates data.

DECLARE @g geography,
@pIn geography,
@pOut geography

SET @g = geography::STPolyFromText('POLYGON((-91.119987 40.705402, -91.129158 40.682148, -91.162498 40.656311, -91.214912 40.643818, -91.262062 40.639545, -91.375610 40.603439, -91.411118 40.572971, -91.412872 40.547993, -91.382103 40.528496, -91.374794 40.503654, -91.385399 40.447250, -91.372757 40.402988, -91.385757 40.392361, -91.418816 40.386875, -91.448593 40.371902, -91.476883 40.390968, -91.490158 40.390762, -91.500221

[code]...

(1 row(s) affected)As I read that there is a distance of about 7864 meters, this is close to what I would expect, so that's ok. The point outside I would expect a distance as well so that is confusing.. Then we have the intersects, it says that the point inside does NOT intersect but the one outside DOES, this is backed up by the intersection values.

View 1 Replies View Related

Help With TSQL Stored Procedure - Error-Exec Point-Procedure Code

Nov 6, 2007

I am building a stored procedure that changes based on the data that is available to the query. See below.
The query fails on line 24, I have the line highlighted like this.
Can anyone point out any problems with the sql?

------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------
This is the error...


Msg 8114, Level 16, State 5, Procedure sp_SearchCandidatesAdvanced, Line 24

Error converting data type varchar to numeric.

------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------
This is the exec point...


EXEC [dbo].[sp_SearchCandidatesAdvanced]

@LicenseType = 4,

@PositionType = 4,

@BeginAvailableDate = '10/10/2006',

@EndAvailableDate = '10/31/2007',

@EmployerLatitude = 29.346675,

@EmployerLongitude = -89.42251,

@Radius = 50

GO

------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------
This is the STORED PROCEDURE...


set ANSI_NULLS ON

set QUOTED_IDENTIFIER ON

go



ALTER PROCEDURE [dbo].[sp_SearchCandidatesAdvanced]

@LicenseType int = 0,

@PositionType int = 0,

@BeginAvailableDate DATETIME = NULL,

@EndAvailableDate DATETIME = NULL,

@EmployerLatitude DECIMAL(10, 6),

@EmployerLongitude DECIMAL(10, 6),

@Radius INT


AS


SET NOCOUNT ON


DECLARE @v_SQL NVARCHAR(2000)

DECLARE @v_RadiusMath NVARCHAR(1000)

DECLARE @earthRadius DECIMAL(10, 6)


SET @earthRadius = 3963.191


-- SET @EmployerLatitude = 29.346675

-- SET @EmployerLongitude = -89.42251

-- SET @radius = 50


SET @v_RadiusMath = 'ACOS((SIN(PI() * ' + @EmployerLatitude + ' / 180 ) * SIN(PI() * p.CurrentLatitude / 180)) + (COS(PI() * ' + @EmployerLatitude + ' / 180) * COS(PI() * p.CurrentLatitude / 180) * COS(PI()* p.CurrentLongitude / 180 - PI() * ' + @EmployerLongitude + ' / 180))) * ' + @earthRadius




SELECT @v_SQL = 'SELECT p.*, p.CurrentLatitude, p.CurrentLongitude, ' +

'Round(' + @v_RadiusMath + ', 0) AS Distance ' +

'FROM ProfileTable_1 p INNER JOIN CandidateSchedule c on p.UserId = c.UserId ' +

'WHERE ' + @v_RadiusMath + ' <= ' + @Radius


IF @LicenseType <> 0

BEGIN

SELECT @v_SQL = @v_SQL + ' AND LicenseTypeId = ' + @LicenseType

END


IF @PositionType <> 0

BEGIN

SELECT @v_SQL = @v_SQL + ' AND Position = ' + @PositionType

END


IF LEN(@BeginAvailableDate) > 0

BEGIN

SELECT @v_SQL = @v_SQL + ' AND Date BETWEEN ' + @BeginAvailableDate + ' AND ' + @EndAvailableDate

END


--SELECT @v_SQL = @v_SQL + 'ORDER BY CandidateSubscriptionEmployerId DESC, CandidateFavoritesEmployerId DESC, Distance'


PRINT(@v_SQL)

EXEC(@v_SQL)


-----------------------------------------------------------------------------------------------------------------

View 4 Replies View Related

Who Nos When The Polygon Made Up In

Mar 3, 2008

i need help on this one

View 8 Replies View Related

Drawing Polygon On Chart

Mar 17, 2008

I am working on a report which requires me to draw two polygons (rectangles) on a scatter chart to visually indicate "good" and "bad" ranges of plotted data. Is there a way to do this out-of-the-box?

Otherwise, if I must create a custom report item, how do I draw on the chart surface? I have looked at the following articles on Custom Report Items, but when I open the sample solutions I can't put the custom report items on the chart surface.

http://blogs.msdn.com/chrishays/default.aspx
http://msdn2.microsoft.com/en-us/magazine/cc188686.aspx

Do I have to create a custom report item which inherits from a chart control? If so, does anyone have any reference samples?

View 4 Replies View Related

Is Point In Time Recovery To A Point Before The Last Full Database Backup Possible?

Mar 26, 2008

Hello all,

First off, I appreciate the time that those of you reading and responding to this request are offering. My quesiton is a theoretical and hopefully simple one, and yet I have been unable to find an answer to it on other searches or sources.

Here's the situation. I am working with SQL Server 2005 on a Windows Server 2003 machine. I have a series of databases, all of which are in Full recovery mode, using a backup device for the full database backups and a separate device for the log backups. The full backups are run every four days during non-business hours. The log backups are run every half hour.

Last week, one of my coworkers found that some rarely-used data was unavailable, and wanted to restore a database to a point in time where the data was available. He told me that point in time was some time back in November.

To accomplish this, I restored the database (in a separate database, as to not overwrite my production database) using the Point in Time Recovery option. I selected November from the "To a point in time" window (I should note that this window is always grey, never white like most active windows, it seems), and the full database backup and the subsequent logs all became available in the "Select the backup sets to restore" window.

I then tried a bevy of different options from the "Options" screen. However, every restore succeeds (ie: it doesn't error out), but seems to be bringing the database back to a current point in time. It's never actually going back to the point in time I specify.

My questions are as follows:

a) Is it possible to do a point in time recovery to a point in time BEFORE the last full database backup?

b) If so, what options would you recommend I use? (ie: "Overwrite the existing database", restore with recovery, etc etc).

I again appreciate any and all advice I receive, and I look forward to hearing from anyone and everyone on this topic. Thank you.

Ryan

View 4 Replies View Related

SQL Query: Select All The Points Within A Closed Polygon

Feb 18, 2005

HI:

Is there any algorithim, which can help me write a script or SQL that will select all the points within a Closed Polygon.

This polygon and point data have latitude and longitude information

Help will be appreciated.

Thanks
Namita

View 12 Replies View Related

SQL Server 2014 :: Select All Points Within Polygon

Mar 25, 2014

I have a table which hold records that contain all the lat/long points. I want to select all records that are within a polygon. Server 2008...

View 6 Replies View Related

SQL Server Over Point-to-Point T1

Nov 10, 2006

I'm running SQL Server 2005 on a Server 2003 machine serving both our home network as well as a remote site through a point-to-point T1. While file transfer speeds are up to par, the remote site's interaction with SQL Server (Point of sale system) is very slow. After testing I am certain that it has nothing to do with the actual physical machine in place neither is it an issue with the program itself since speeds are as they are supposed to be over the home network lan. It seems that there might be a packet size issue or something of the sort. Has anyone dealt with this before or have any thoughts?

Thanks,
Peter

View 7 Replies View Related

Equivalent Tsql For Sql Server 2000 Is Needed [from Sql Server 2005 Only Tsql]

Nov 19, 2007

Can anyone please give me the equivalent tsql for sql server 2000 for the following two queries which works fine in sql server 2005

1
-- Full Table Structure

select t.object_id, t.name as 'tablename', c.name as 'columnname', y.name as 'typename', case y.namewhen 'varchar' then convert(varchar, c.max_length)when 'decimal' then convert(varchar, c.precision) + ', ' + convert(varchar, c.scale)else ''end attrib,y.*from sys.tables t, sys.columns c, sys.types ywhere t.object_id = c.object_idand t.name not in ('sysdiagrams')and c.system_type_id = y.system_type_idand c.system_type_id = y.user_type_idorder by t.name, c.column_id


2
-- PK and Index
select t.name as 'tablename', i.name as 'indexname', c.name as 'columnname' , i.is_unique, i.is_primary_key, ic.is_descending_keyfrom sys.indexes i, sys.tables t, sys.index_columns ic, sys.columns cwhere t.object_id = i.object_idand t.object_id = ic.object_idand t.object_id = c.object_idand i.index_id = ic.index_idand c.column_id = ic.column_idand t.name not in ('sysdiagrams')order by t.name, i.index_id, ic.index_column_id

This sql is extracting some sort of the information about the structure of the sql server database[2005]
I need a sql whihc will return the same result for sql server 2000

View 1 Replies View Related

Point Me To Some Resources

May 21, 2008

I normally develop for MySQL and PHP, but we have a new application that runs on MS SQL and I'm having difficulty using the same structure as MySQL to run queries. Can someone here point me to a good resource to learn the basics?

Thanks!

View 1 Replies View Related

Power Point

Oct 26, 2005

Hi there.

Is it possible to convert the Power Point file to SQL Server Version 7 or 2000? My PowerPoint files have no images or pictures, only lyrics for songs that are used in the church during sunday services.

I was appointed to make a system in VB6 but my first job is to convert the PowerPoint files to SQL Server. There are 2000 songs in the powerpoint right now.

Please help.

Thanks.

View 2 Replies View Related

Can Someone Point Me In The Right Direction?

Feb 11, 2007

Hello, thanks for reading this.
I am trying to learn a bit about debveloping my own website, and I am in need of a basic direction on how to acomplish this task:

I have an local program running on my PC that creates an MS Access database export of my inventory. This export can be directly uploaded to my web server in a zip format.
My web site is developed in Coldfusion MX7, and uses a MS SQL 2005 database. What I would like to do is create somewhat of an "Auto update" for my website, exporting my access database and updating the MS SQL database automatically.

I really can use some good direction on this problem.
Thank you for any help in advance.

View 4 Replies View Related

Point And Click DTS

Mar 19, 2007

i have a DTS Package a saved when i set up the package. it's in local packages in Enterprise Manager now. is there any way i can simply set up a shortcut on my desktop to execute that DTS, rather then having to nav though Enterprise Manager?

yeah, i know i'm lazy :o)

View 2 Replies View Related

Point Me In The Right Direction

Nov 24, 2007

hi.
i am a c++ developer who requires to learn sql.
i have no other programs than the sql server that comes with visual studio pro 2005.
what

do i need to get/do to create a sql databse and to manipulate it in c++.

thank you.

View 5 Replies View Related

Floating Point Type??

Jan 21, 2008

I need to write a floating point value to a column in a SQL server database. I have the following line ofcode, that formats the float value before passing it off to the stored procedure.
Please look at the line of code below. Does the 8 after SqlDbType.Foat mean that this will be a floatingpoint with 8 signigicant digits past the decimal point?
mySqlCommand.Parameters.Add("@Price", SqlDbType.Float, 8).Value = float.Parse(InitialPrice);

View 3 Replies View Related

How To Use Substring With Starting Point?

Apr 11, 2005

Hi all,
I want to select a column which is store email address. How to use the substring function from starting point '@' to end of string?
Here is my syntax :
select substring(emailaddress, [what syntax] ,len(emailaddress)) from table
Thanks!

View 2 Replies View Related

Floating Point Calculations...

Nov 9, 2001

Hello all,

I can't see any reason for this error, not having a high level understanding of maths I thought I'd post it and hope someone could share some light on it.

I yesterday got called by a client who said that a payment for £15 + VAT was being passed to their payment gateway as 17.62 when it should be 17.63. The VAT calculation is performed in a SQL Server 2000 stored procedure. In the end I tracked it down and it wasn't a propblem with my calculation.

The price was coming out as 17.63 fine. The stored procedure then had to return this price in pence (17.63 * 100 = 17.63). When I put in a print statement with this calculation it was correct but when I output the variable that the result was assigned to it was coming out as 1762.

The variable that the result was being put into was of real datatype.

I then wrote a udf to test this. Here is the function:

CREATE FUNCTION dbo.POUNDS_TO_PENCE
(
@POUNDVALUE real
)
RETURNS INTEGER
AS
BEGIN

RETURN @POUNDVALUE * 100

END

As you can see nothing very special.

If you run this runction and pass in 17.63 it will return 1762!!!

The bit I don't get is if I change the @POUNDSVALUE intput variable to type float it returns the correct amount.

I've also found that the same problem occurs when passing in £30 + VAT (35.25) + 1pence. So, 35.26 comes out as 3525 instead of 3526. This is the case if you keep doubling the number (and adding a few pence here and there).

Does anyone know why this is or is it a bug in the processor?

The SQL books online say the following about the float and real data types:

--------------------------------------------------------
float and real (T-SQL)
Approximate number data types for use with floating point numeric data. Floating point data is approximate; not all values in the data type range can be precisely represented.

Syntax
float[(n)]
Is a floating point number data from - 1.79E + 308 through 1.79E + 308. n is the number of bits used to store the mantissa of the float number in scientific notation and thus dictates the precision and storage size. n must be a value from 1 through 53.


n is Precision Storage size
1-24 7 digits 4 bytes
25-53 15 digits 8 bytes


The Microsoft® SQL Server™ float[(n)] data type conforms to the SQL-92 standard for all values of n from 1 to 53. The synonym for double precision is float(53).

real
Floating point number data from –3.40E + 38 through 3.40E + 38. Storage size is 4 bytes. In SQL Server, the synonym for real is float(24).


--------------------------------------------------------

Apart from the fact that it says 'Approximate number data types' I can't see any difference between the data type apart from the ranges.

Anyone any ideas?
Thanks
Tom Holder

View 2 Replies View Related

Restore Point In Time

Dec 8, 1999

I can't "point in time restore" a test DB if I had only a Full DB Backup (with overwrite option).



Example
12:00 Fullbackup new (overwrite)
12:01 update any rows
12:02 update any rows
12:03 delete any rows
12:05 Transaction Log backup (overwrite)
RESTORE: We can't set "point in Time" to 12:01 od 12:02 ???

why

View 2 Replies View Related

Point In Time Restore

Mar 13, 2000

Hi

1. could any one explain point in time recovery.

2. Problem we had was the database froze 'inload' while doing transaction restore. Is there any way to recover this


regards
rajeev

View 1 Replies View Related

Point In Time Balances

Aug 31, 2005

Hi, I am pretty new to Analysis Services and I have come across a small problem to do with point in time balances, I guess that this is similar shiver72's post titled 'Date Range Problem'. I am not really after a solution as such (although that would be great :-), more interested in a pointer to some resource(s) which I can read up on myself.

Here is the scenario:

A student enrols into a course that has a start and finish date. This enrolment has a certain value based upon its length, the course type etc which means that each
enrolment's 'value' can vary at any time between the start and finish dates.

For example a student may enrol in a full year basket weaving course with a value of 0.0500, they then enrol in a part year course titled 'Dimensional Modelling 101' with a value of 1.000

...and their enrolments would look like this...

01-JAN-2005 31-DEC-2005 0.0500
02-JAN-2005 15-JUN-2005 1.0000

The client wishes to know at any point in time the 'value' of their enrolments. These values can vary due to a student starting or finishing their enrolment, withdrawing, having their
enrolment suspended etc, etc

Now, in the Data Warehouse Toolkit it mentions point in time balances in the 'Financial Services' section but it uses SQL to prove the point which is no use to me because I am using MS Analysis Services to dynamically create the result.

As it says in the book its no use creating a row in the fact table to represent the value for each day that the enrolment is in effect because with 400,000 enrolments that works out to over 140 million rows. The alternative is to create one row for each occurence of the variation in the value of the enrolment and then the facts would be completely additive and useful.

I would be looking at using something similar to the following:

fact table
----------
fkdate
value
fksudent
fkcourse
<possibly some degenerate measure like the UID of the course enrolment held in the source system>

dimensions
----------
Student - and of course a whole heap of related dimensions like gender, age, ethnicity etc
Course

For example given the above information the fact table would look like this:

(Student UID is 2005123, course UIDs are 1000 and 1001)

20050101 0.0500 2005123 1000
20050102 1.0000 2005123 1001
20050615 -1.0000 2005123 1001
20051231 -0.0500 2005123 1000

That way I can run a query at, say 15-JUN-2005 and sum the values and it will come out with the correct balance - and yes I know that this idea is straight out of the book :-)

OK, if you've read to this point then I take it that you are an extremely patient person and therefore will forgive my next question.

My question is this, my clients use simple tools like Excel pivot tables to gouge the information they want out of the warehouse, sure, I can write an MDX query to get a point in time balance (just as soon as I learn a bit more about it) but is there _any_ way/means/algorithm/trick/way to hold your head to one side, that I can use so that they can still just 'drag and drop' using the pivot table service as opposed to having to me having to create an MDX-based report for them ?

cheers

View 2 Replies View Related

Point InTime Recovery

May 21, 2002

Hi Guys,
I wanted to know how to do a point in time recovery on sql server 2000.
I wanted to go back in time when I restore the file.
Can u also suggest any good book about it. Does BOL explains about it, I couldn't find it in BOL.
Thanks in advance.
Joe

View 2 Replies View Related

Decimal Point Shifting On SUM

Oct 27, 1998

The following SQL statement returns the correct totals except that
the total value is shifted on decimal place to the right. i.e. a real
total of 955.68 is displayed as 9,556.80. The total_ar field is a money type.
Any help would be appreciated.

Mark

/* AR report Total Greater than 365 days sorted by Dept */

select

a.dept as `Department `,
` A/R 365+ `=sum(case when datediff(day,c.bill_date, getdate()) > 365 then b.total_ar else 0 end)

from

hbm_persnl a, blt_billm b, blt_bill c,hbl_dept d,hbm_matter e

where
e.matter_uno = b.matter_uno and
a.empl_uno = e.bill_empl_uno and
b.bill_tran_uno = c.tran_uno and
b.ar_status = `O` and
e.status_code=`OPEN`

group by a.dept

View 1 Replies View Related

Check Point Option

Apr 18, 2001

Hello folks,

The other day I was checking the dboption and I couldnt see the "Truncate log on Check Point" option in the dboption window. Wondering if i'm looking at the right place.

Appreciated, If any one point me where can I look for this.


Thanks,

Sindi

View 2 Replies View Related

Decimal Point Problem

Jul 29, 2004

I want to force two places to the right of the decimal even if its a whole number.

vbScript

calculatedScore = FormatNumber((rsQFinal("sumEarned") / rsQFinal("sumPossible")) * 100, 2)


I'm not savy enought to know the SQL equivalent of the above but I'd like SQL Server to perform the above rather than my vbScript code.

heres what I have so far but I don't know the formatNumber equivalent:

CREATE PROCEDURE quarterFinalGradeSA @nClass INT, @nQuarter INT, @nStudent INT AS

SELECT (SUM(tblScores.score) / SUM(tblAssignments.assignmentTotalPoints)) AS returnValue
...

View 4 Replies View Related

Point Of Time Recovery EM

Sep 2, 2004

We have been testing point of time recovery using EM and found that this does not work.
We enter date and time and do net get the logs restored. Even if we use the default date it does not work. In Query Analyser we have have managed to recover to a point in time. Anybody got any idea why EM does not work.
We are using 2000 sp3

View 2 Replies View Related

Point In Time Restore

Nov 26, 2004

My database is in full recovery mode. When I have created some full backups of the database, I would like try to point in time restore. Unfortunately, this option is greyed out on the restore screen. What have I done wrong?

View 1 Replies View Related

Point In Time Recovery

Oct 13, 2006

Dear All,
How do we accomplish point in time recovery with SQL Server database.

For example:
My backup schedule is
Monday - Complete database
Tuesday - transaction log backup
Wednesday-transaction log backup
Thursday - transaction log backup
Friday - Complete database
Saturday - transaction log backup
Sunday - transaction log backup

For complete database backup, I use the below syntax:

BACKUP DATABASE myDB
TO DISK= @File1
WITH DESCRIPTION = @Desc

For transaction log backup, I use the below syntax:
BACKUP LOG myDB
TO DISK= @File1
WITH DESCRIPTION = @Desc


With this scenario, can I accomplish a point in time recovery? For example, if my database crashes on thursday night. How do i do complete recovery till that time?

Pls guide...

Regards,
qA

View 1 Replies View Related

Point In Time Query &>:[

Jul 9, 2007

Hey guys and gals,

I'm having a real problem with this query at the moment...
Basically I have to produce a query which will tell me the total number of people employed by the company at any given date and the total salary for all these people.

We have a people table and a career table.
People(unique_identifier, known_as_and_surname, start_date, termination_date ...)
Career(unique_identifier, parent_identifier, career_date, basic_pay ...)
Relationship people.unique_identifier = career.parent_identifier

Employees can be identified like so

SELECT *
FROM people
WHERE start_date <= DateSelected
AND (termination_date > DateSelected
OR termination_date IS NULL)

Passing the selected date to the query is no trouble at all I am just having problems with the point in time side of this.

All and any help is greatly appreciated :)
~George

P.S. SQL Server 2000 ;)

View 14 Replies View Related

Floating Point Truncation

Mar 24, 2004

How can I truncate a floating point number to required number of decimal points
Eg:
100.642364074 to 100.64 and 67.643929847 to 67.645

Thanks.

View 2 Replies View Related

Many To Many With One Access Point Table

Jun 3, 2008

Hello,

First time poster.

I have a database design question. I just read the article on table inheritance but i still do not know if I'm doing this right.

Say you have an organizations table and a persons table like the ones below with a many to many relationship like these three tables:

Organization:
AutoId TaxId Name

Person:
AutoId FirstName LastName

OrganizationsPersons:
AutoId OrgAutoId PersonAutoId


Now the question is that an organization and a person both need to access the address table.

Is it better to have a PersonsAddress table and an OrganizationsAddress table, or is it better to have some sort of AddressAccess table that the persons and the organizations tables both relate to and have some sort of type field in there that specifies Org or Person?

Thanks ahead of time for any help.

View 7 Replies View Related

Can't Restore Point In Time

Jan 31, 2014

I know NOTHING about SQL. I have a SQL 2005 database. I'm trying to restore a point in time. I get the error:

RESTORE FAILED...AMT12-2-13.TRN... CANNOT FIND THE FILE SPECIFIED.

The file exists, and in the proper location. something is wrong with it. Is there any way to rebuild the entire TRN so that a point in time recovery will work?

View 2 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved