How can I select a circle of data using X and Y grid coordinates in a SQL server database table?

Viewed 527

This is a fun one, apologies for the lack of information but I don't have a clue about how to approach this.

I have a SQL Server database which is storing map coordinates against post area codes. For example postal area code BD6 is located at 414300m along the X axis and 429800m along Y axis. These values are in one table, in three separate columns, e.g:

PostAreaCode    AlongXAxis    AlongYAxis
BD6               414300       429800

I need to select all post area codes within a 161km radius of a particular post area code. So for example, the coordinates for BD6 are in the middle of the circle, and the edge of the circle is 161km away.

I can create a square around BD6 by using the BETWEEN keyword and doing something like...

SELECT PostAreaCode
FROM PostAreaCodeTable
WHERE AlongXAxis IS BETWEEN (414300-161000) AND (414300+161000)
AND AlongYAxis IS BETWEEN (429800-161000) AND (429800+161000)

...but I have no idea how to make it a circle. I consider myself to be pretty good at SQL but this is completely beyond me. Any help is much appreciated! Please explain mathsy answers as much as possible because maths is not my strong point. Thanks!

2 Answers
Related