Please start any new threads on our new site at http://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

 SQL Server Forums Profile | Active Topics | Members | Search | Forum FAQ
 All Forums  SQL Server 2000 Forums  SQL Server Development (2000)  Distance Application with sorting in SQL Reply to Topic  Printer Friendly
Author  Topic

USA
0 Posts

 Posted - 02/01/2002 :  07:45:33 Lou writes "I am trying to come up with a SQL statement that will figure the distance between one zip code and a table of another zip codes and return them sorted by distance.The tables would be:DISTANCE TABLEZip Code, Latitude, LongitudeZIP CODE TABLEZip Code, Town NameAnother zip code would be provided as the starting point. Is this possible to do just in SQL?"

robvolk
Most Valuable Yak

USA
15732 Posts

 Posted - 02/01/2002 :  07:59:47 If you can use some basic trig functions like COS, SIN, and TAN to find the distances, then yes you can do this using on SQL. I'm not sure if the math is right, but the following example should give you an idea:DECLARE @startzip char(5), @destzip char(5)SELECT @startzip='11701', @destzip='30097'SELECT Start.Latitude, Start.Longitude, Dest.Latitude, Dest.Longitude,TAN((Start.Latitude-Dest.Latitude)/(Start.Longitude-Dest.Longitude)) AS DistanceTANFROM Distance Start, Distance DestWHERE Start.ZipCode=@startzip AND Dest.ZipCode=@destzipYou can JOIN the table of zip codes to this instead of supply a single zip code as the destination. Can you provide some more detail? If you've got a formula for calculating distance we could plug it in to give you actual distances.

aiken
Aged Yak Warrior

USA
525 Posts

 Posted - 02/02/2002 :  16:45:18 I have just this application running. For performance, I precompute the distance between every zip code (!) and then throw out the ones that are over 2500 miles. It's a 38GB database, but that's really the only way you can do it if you want to be able to sort by "closest" (otherwise you have to compute distance for every possible combination in the query itself, which can take forever).I keep the zip code data and stored procedures in a seperate database, as it never changes and I don't want to be backing it up all the time. Cheers-b

Arnold Fribble
Yak-finder General

United Kingdom
1961 Posts

 Posted - 02/03/2002 :  05:28:53 Rob, what on earth are you doing with that tan?!You might want to sanity check this function with some known values: I haven't tested it extensively.`CREATE FUNCTION GCdist (@lat1 float, @lon1 float, @lat2 float, @lon2 float)RETURNS floatASBEGIN -- Parameters in RADIANS, result in km -- Haversine formula from http://www.census.gov/cgi-bin/geo/gisfaq?5.1 DECLARE @dlon float, @dlat float, @a float, @c float SET @dlon = @lon2 - @lon1 SET @dlat = @lat2 - @lat1 SET @a = POWER(SIN(@dlat / 2.0), 2.0) + COS(@lat1) * COS(@lat2) * POWER(SIN(@dlon / 2.0), 2.0) SET @c = 2.0 * ATN2(SQRT(@a), SQRT(1.0 - @a)) RETURN 6367.0 * @cENDGOCREATE FUNCTION dms2rad (@deg int, @min int, @sec float)RETURNS floatASBEGIN RETURN RADIANS(CONVERT(float, @deg) + CONVERT(float, @min)/60.0 + @sec/3600.0)END`Edited by - Arnold Fribble on 02/03/2002 06:10:18

robvolk
Most Valuable Yak

USA
15732 Posts

 Posted - 02/03/2002 :  08:06:08 quote:Rob, what on earth are you doing with that tan?!Misusing it, most likely! It's been almost 20 years since I've had to use serious trigonometry. Give me a break!

Glockenspiel
Yak Posting Veteran

50 Posts

 Posted - 10/15/2002 :  18:19:20 Here's a function that is derived from the original one but further simplified. I verified that it will return the same results, although this one has been further modified to return the results in STATUTE MILES rather than kilometers (if you want it in km again then just replace the constant 69.041 with 6367; if you want it in nautical miles just use 60).`CREATE FUNCTION GCdist2 (@lat1 FLOAT, @lon1 FLOAT, @lat2 FLOAT, @lon2 FLOAT)RETURNS FLOATASBEGIN -- Parameters in RADIANS, result in STATUTE MILES DECLARE @c FLOAT SET @c = ACOS(SIN(@lat1)*SIN(@lat2)+COS(@lat1)*COS(@lat2)*COS(@lon1-@lon2)) RETURN 69.041 * @cENDGO`

Arnold Fribble
Yak-finder General

United Kingdom
1961 Posts

 Posted - 10/16/2002 :  07:05:51 Hmm. When I posted that haversine formula, it was on the assumption that the argument in the referenced GIS faq was correct -- that the cosine formula gives inaccurate results for small distances.What I'd failed to take into account is the difference it makes in going from single precision to double precision. The inaccuracy is still there, it's just that you need points a few inches apart to notice! quote:[...] return the results in STATUTE MILES rather than kilometers (if you want it in km again then just replace the constant 69.041 with 6367; if you want it in nautical miles just use 60). By definition there are exactly 1.609344 km in a statute mile, and 1.852 km in a nautical mile. So the constants should be 3956 (rather than 69.041) and 3438 (rather than 60).Edited by - Arnold Fribble on 10/16/2002 07:15:08

liana
Starting Member

1 Posts

 Posted - 09/06/2006 :  00:12:37 hican anyone help me how to sort many pointer for example1.1.11.1.21.1.3 and so onhave any idea?

SwePeso
Patron Saint of Lost Yaks

Sweden
30421 Posts

 Posted - 09/06/2006 :  02:50:38 This should be a separate topic!If there are no more than three values in the string, use this```declare @test table (version varchar(100)) insert @test select '1.1.2' union all select '1.1.3' union all select '1.1.15' union all select '1.1.1' select version from @test order by CAST(parsename(version, 3) AS INT), CAST(parsename(version, 2) AS INT), CAST(parsename(version, 1) AS INT)```Peter LarssonHelsingborg, Sweden

Arnold Fribble
Yak-finder General

United Kingdom
1961 Posts

 Posted - 09/06/2006 :  02:56:42 Blimey, it's a long time since I last saw this thread!
Topic
 Reply to Topic  Printer Friendly Jump To: Select Forum General SQL Server Forums       New to SQL Server Programming       New to SQL Server Administration       Script Library       Data Corruption Issues       Database Design and Application Architecture SQL Server 2012 Forums       Transact-SQL (2012)       SQL Server Administration (2012)       SSIS and Import/Export (2012)       Analysis Server and Reporting Services (2012)       Replication (2012)       Availability Groups and DR (2012)       Other SQL Server 2012 Topics SQL Server 2008 Forums       Transact-SQL (2008)       SQL Server Administration (2008)       SSIS and Import/Export (2008)       High Availability (2008)       Replication (2008)       Analysis Server and Reporting Services (2008)       Other SQL Server 2008 Topics SQL Server 2005 Forums       Transact-SQL (2005)       SQL Server Administration (2005)       .NET Inside SQL Server (2005)       SSIS and Import/Export (2005)       Service Broker (2005)       Replication (2005)       High Availability (2005)       Analysis Server and Reporting Services (2005)       Express Edition and Compact Edition (2005)       Other SQL Server Topics (2005) SQL Server 2000 Forums       SQL Server Development (2000)       SQL Server Administration (2000)       Import/Export (DTS) and Replication (2000)       Transact-SQL (2000)       Analysis Services (2000)       MSDE (2000) Development Tools       ASP.NET       Reporting Services Development       Other Development Tools Site Related Forums       Site Related Discussions       Article Discussion       Poll Discussion       The Yak Corral Other Forums       SQL Server 6.5 \ SQL Server 7.0       Other Topics       MS Access       ClearTrace Support Forum Old Forums       CLOSED - General SQL Server       CLOSED - SQL Server 2005/Yukon  -------------------- Home Active Topics Frequently Asked Questions Member Information Search Page
 SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC
 This page was generated in 0.04 seconds.