Skip to content

Commit 8969454

Browse files
committed
[FEATURE] Add name convention links and udf_CalculateDistanceBetween2Coordinates
1 parent baf5439 commit 8969454

File tree

2 files changed

+64
-2
lines changed

2 files changed

+64
-2
lines changed

SQL Server Name Convention and T-SQL Programming Style.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22

33
Official Reference and useful links
44
- [Transact-SQL Formatting Standards](https://www.simple-talk.com/sql/t-sql-programming/transact-sql-formatting-standards-%28coding-styles%29/) (by Robert Sheldon)
5+
- [Subjectivity : Naming Standards](http://blogs.sqlsentry.com/aaronbertrand/subjectivity-naming-standards/) (by Aaron Bertrand)
56
- [General Database Conventions](http://kejser.org/database-naming-conventions/general-database-conventions/) (by Thomas Kejser)
6-
- [Writing Readable SQL](http://www.codeproject.com/Articles/126380/Writing-Readable-SQL)
7-
- [SQL Style Guide](http://www.sqlstyle.guide/) (Simon Holywell)
7+
- [Writing Readable SQL](http://www.codeproject.com/Articles/126380/Writing-Readable-SQL) (by Red Gate_)
8+
- [SQL Style Guide](http://www.sqlstyle.guide/) (by Simon Holywell)
89
- [SQL Code Layout and Beautification](https://www.simple-talk.com/sql/t-sql-programming/sql-code-layout-and-beautification/) (by William Brewer)
910
- [TSQL Coding Style](http://www.databasejournal.com/features/mssql/tsql-coding-style.html) (by Gregory Larsen)
1011
- [Database object Limitations](http://technet.microsoft.com/en-us/library/ms172451%28v=sql.110%29.aspx)
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
IF OBJECT_ID('dbo.udf_CalculateDistanceBetween2Coordinates', 'FN') IS NULL
2+
EXECUTE ('CREATE FUNCTION dbo.udf_CalculateDistanceBetween2Coordinates() RETURNS INT AS BEGIN RETURN 1 END;');
3+
GO
4+
5+
CREATE FUNCTION dbo.udf_CalculateDistanceBetween2Coordinates
6+
(
7+
@Lat1 Float(18),
8+
@Long1 Float(18),
9+
@Lat2 Float(18),
10+
@Long2 Float(18),
11+
@ReturnType VarChar(10)
12+
)
13+
14+
Returns Float(18)
15+
16+
AS
17+
/*
18+
Note
19+
Author: Vignesh Arulmani
20+
Original link: http://www.sqlservercentral.com/scripts/Calculate+Distance/118083/
21+
Examples
22+
select dbo.udf_CalculateDistanceBetween2Coordinates('37.7749300 ','-122.4194200','34.0522222','-118.2427778','Miles');
23+
select dbo.udf_CalculateDistanceBetween2Coordinates('37.7749300 ','-122.4194200','34.0522222','-118.2427778','Kilometers');
24+
select dbo.udf_CalculateDistanceBetween2Coordinates('37.7749300 ','-122.4194200','34.0522222','-118.2427778','Meters');
25+
select dbo.udf_CalculateDistanceBetween2Coordinates('37.7749300 ','-122.4194200','34.0522222','-118.2427778','Feet');
26+
*/
27+
Begin
28+
29+
Declare @R Float(8);
30+
Declare @dLat Float(18);
31+
Declare @dLon Float(18);
32+
Declare @a Float(18);
33+
Declare @c Float(18);
34+
Declare @d Float(18);
35+
36+
Set @R =
37+
Case @ReturnType
38+
When 'Miles' Then 3956.55
39+
When 'Kilometers' Then 6367.45
40+
When 'Feet' Then 20890584
41+
When 'Meters' Then 6367450
42+
Else 20890584
43+
End
44+
45+
Set @dLat = Radians(@lat2 - @lat1);
46+
47+
Set @dLon = Radians(@long2 - @long1);
48+
49+
Set @a = Sin(@dLat / 2)
50+
* Sin(@dLat / 2)
51+
+ Cos(Radians(@lat1))
52+
* Cos(Radians(@lat2))
53+
* Sin(@dLon / 2)
54+
* Sin(@dLon / 2);
55+
Set @c = 2 * Atn2(sqrt(@a),sqrt(1-@a));
56+
57+
Set @d = @R * @c;
58+
Return @d;
59+
60+
End;
61+
Go

0 commit comments

Comments
 (0)