MS-SQL Server Selection for Geometry cordinates


In MS-SQL database, we are storing all longitude_degrees, latitude_degrees values for passing vessels. In map draw polygon and trying to select the vessels included in that polygon coordinates.

query :

select longitude_degrees, latitude_degrees  from [ Table ] where Geometry::STGeomFromText('POLYGON((103.47718811035156 1.2504558563232422,103.47982025146484 1.2505289316177368,103.49195098876953 1.249359369277954,103.49137115478516 1.2449731826782227,
103.49049377441406 1.2444614171981812,103.48983001708984 1.2444614171981812,103.48551940917969 1.2444614171981812,103.47762298583984 1.2444614171981812,103.47002410888672 1.2444614171981812,103.47718811035156 1.2504558563232422,103.47718811035156 1.2504558563232422,
103.47718811035156 1.2504558563232422))',4326)
.STContains(Geometry::STPointFromText('POINT(' + CAST([longitude_degrees] AS VARCHAR(20)) + ' ' + CAST([latitude_degrees] AS VARCHAR(20)) + ')',0)) = 1

I have used STIntersects and STEquels function also but no records selected.

My expectation is need to get the  vessels inside that polygon as i selected in map.

Please help me to resolve the  problem.

Venkat satyaSoftware developerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Máté FarkasDatabase Developer and AdministratorCommented:
The problem is that your poligon is not valid because it contains some redundant point.
If you remove those points than it will work:
	SELECT	Geometry::STGeomFromText('POLYGON((
				103.47718811035156 1.2504558563232422,
				103.47982025146484 1.2505289316177368,
				103.49195098876953 1.249359369277954,
				103.49137115478516 1.2449731826782227,
				103.49049377441406 1.2444614171981812,
				103.48983001708984 1.2444614171981812,
				103.48551940917969 1.2444614171981812,
				103.47762298583984 1.2444614171981812,
				103.47002410888672 1.2444614171981812,
				103.47718811035156 1.2504558563232422))', 4326) AS Area,
			Geometry::STGeomFromText('POINT(103.4800 1.2480)', 4326) AS Point
) L
WHERE Area.STContains(Point) = 1

Open in new window

And you have to use the same SRID (sptial reference id) for both objects.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Venkat satyaSoftware developerAuthor Commented:
Hi Farkas,

Thank you very much.

I missed it.

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.