SQL Server statement queries the specified location (latitude and longitude) in the range of information

Source: Internet
Author: User
Tags cos sin

Specify a latitude and longitude, given a range value, to find out the nearest 5 sets of data within this range around the latitude and longitude.
Longitude:116.312785
Latitude:39.929875
Range: 1 degrees (111 km)
Long is the data table longitude field
Lat for data table latitude field

Center for data table
The statement is tested under SQL Server, and other databases may need to be modified

Selecttop 5 *, ACOS (sin ((39.929875 * PI ())/P-P) * SIN ((lat * PI ())/) cos ((39.929875 * PI ())/+) * cos ((LAT * PI ( )) * COS ((116.312785 * PI ())/-(LONG * PI ())/) * 6380 as Aafromcenterwherelat > 39.929875-1and L At <39.929875 1AND long >116.312785-1and Long < 116.312785 1ORDER by AA------------------------------------- --------------------------------------------------If the data type is not float type, it needs to be converted to float type: cast (LAT as float)

SQL Server statement queries the specified location (latitude and longitude) in the range of information

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.