<?xml version="1.0" encoding="utf-8"?> <?xml-stylesheet type="text/css" href="http://www.prlsoftware.com/Data/style/rss1.css" ?> <?xml-stylesheet type="text/xsl" href="http://www.prlsoftware.com/Data/xsl/rss1.xsl" ?>
<!--RSS generated by mojoPortal Blog Module V 1.0 on Thursday, July 29, 2010-->
<rss version="2.0">
  <channel>
    <title>Scripts</title>
    <link>http://www.prlsoftware.com/t-sql.aspx</link>
    <description>T-SQL Scripts</description>
    <copyright>Copyright 2009 Philip Leitch</copyright>
    <ttl>120</ttl>
    <managingEditor>Philip@nospamprlsoftware.com</managingEditor>
    <generator>mojoPortal Blog Module V 1.0</generator>
    <item>
      <title>Working Days</title>
      <link>http://www.prlsoftware.com/working-days.aspx</link>
      <pubDate>Thu, 15 Apr 2010 03:51:20 GMT</pubDate>
      <guid>http://www.prlsoftware.com/working-days.aspx</guid>
      <author>Philip Leitch</author>
      <comments>http://www.prlsoftware.com/working-days.aspx</comments>
      <description><![CDATA[<p>Create function Working_Days(<br />
&#160;@Date1&#160; Smalldatetime,<br />
&#160;@Date2&#160; Smalldatetime)<br />
/*www.prlsoftware.com<br />
Author: Philip Leitch<br />
Date: 2010<br />
Purpose: This function determines the number of working days between two dates.<br />
Copyright: Philip Leitch 2010<br />
Licensing: This code may be used or modified but if the code is included in a software package attribution to me must be made.<br />
Liability: The developer assumes all liability when using this code.<br />
Notes: The logic is simply the number of days different between the two dates minus 2 times the number of weeks between <br />
the two dates.&#160; Holidays and other issues may need to be factored in.<br />
*/<br />
returns&#160; int<br />
as<br />
begin<br />
&#160;&#160;&#160; return abs(datediff(Day, @Date1, @Date2) - 2 * abs(datediff(week, @date1, @date2)))<br />
end<br />
go<br />
select dbo.working_days ('12/4/2010', '16/4/2010') --During Week<br />
select dbo.working_days ('16/4/2010', '19/4/2010') --Over weekend<br />
&#160;</p><br /><br /><a href='http://www.prlsoftware.com'>Philip Leitch</a>&nbsp;&nbsp;<a href='http://www.prlsoftware.com/working-days.aspx'>...</a>]]></description>
    </item>
    <item>
      <title>Bearing</title>
      <link>http://www.prlsoftware.com/bearing.aspx</link>
      <pubDate>Tue, 13 Apr 2010 00:11:10 GMT</pubDate>
      <guid>http://www.prlsoftware.com/bearing.aspx</guid>
      <author>Philip Leitch</author>
      <comments>http://www.prlsoftware.com/bearing.aspx</comments>
      <description><![CDATA[<p>Create function [dbo].[Bearing]<br />
( @Lat1 float, @Lng1 float, @Lat2 float, @Lng2&#160; float)<br />
/*<br />
www.prlsoftware.com<br />
Author: Philip Leitch<br />
Date: 2010<br />
Purpose: This function converts two sets of latitude and longatude points in to a bearing.<br />
Copyright: Philip Leitch 2010<br />
Licensing: This code may be used or modified but if the code is included in a software package attribution to me must be made.<br />
Liability: The developer assumes all liability when using this code.<br />
Notes: Bearing changes during travel, for instance, if you travel heading due north, after enough time you will end up heading due <br />
south even though you haven’t changed direction.&#160; Therefore this is the outset direction (direction from point A to get to point B).<br />
*/<br />
returns numeric(10,6) as<br />
begin<br />
Declare @Bearing as&#160; numeric(10,6) <br />
&#160;&#160;&#160; <br />
&#160;&#160;&#160; set @Lat1 = @Lat1 * (pi()/180.0)&#160;&#160;&#160; <br />
&#160;&#160;&#160; set @Lng1 = @Lng1 * (pi()/180.0)&#160;&#160;&#160; <br />
&#160;&#160;&#160; set @Lat2 = @Lat2 * (pi()/180.0)&#160;&#160;&#160; <br />
&#160;&#160;&#160; set @Lng2 = @Lng2 * (pi()/180.0)&#160;&#160;&#160; <br />
<br />
<br />
&#160;&#160;&#160; set @Bearing = atn2(sin(@Lng2-@Lng1)*cos(@Lat2),cos(@Lat1)*sin(@Lat2)-sin(@Lat1)*cos(@Lat2)*cos(@Lng2-@Lng1))/ (pi()/180.0)&#160;&#160;&#160; <br />
&#160;&#160;&#160; &#160;<br />
&#160;&#160;&#160; if&#160; @Bearing &lt; 0 <br />
&#160;&#160;&#160; &#160;&#160;&#160; set @Bearing =&#160; 360 + @Bearing<br />
&#160;&#160;&#160; return @Bearing<br />
&#160;&#160;&#160; <br />
end</p><br /><br /><a href='http://www.prlsoftware.com'>Philip Leitch</a>&nbsp;&nbsp;<a href='http://www.prlsoftware.com/bearing.aspx'>...</a>]]></description>
    </item>
    <item>
      <title>Point_Bearing_Distance_To_Point</title>
      <link>http://www.prlsoftware.com/point_bearing_distance_to_point.aspx</link>
      <pubDate>Tue, 09 Mar 2010 03:42:41 GMT</pubDate>
      <guid>http://www.prlsoftware.com/point_bearing_distance_to_point.aspx</guid>
      <author>Philip Leitch</author>
      <comments>http://www.prlsoftware.com/point_bearing_distance_to_point.aspx</comments>
      <description><![CDATA[<div>CREATE&#160;&#160;&#160; &#160;function [dbo].[Point_Bearing_Distance_To_Point]<br />
( @Latitude float, @Longitude float, @Bearing float, @Distance as float)<br />
/*<br />
www.prlsoftware.com<br />
Author: Philip Leitch<br />
Date: 2010<br />
Purpose: This function converts a latitude and longatude, bearing and distance to another latitude and longitude (concatenated together).<br />
Copyright: Philip Leitch 2010<br />
Licensing: This code may be used or modified but if the code is included in a software package attribution to me must be made.<br />
Liability: The developer assumes all liability when using this code.<br />
Notes:<br />
*/<br />
returns varchar(50)<br />
as<br />
begin<br />
Declare @Lat2 float, @Long2&#160; float<br />
set @Latitude = cast(@latitude as numeric(9,6))<br />
Declare @Radius float<br />
&#160;&#160;&#160; set @Radius = dbo.Earth_Radius(@Latitude)<br />
<br />
&#160;&#160;&#160; set @Bearing = @bearing * (pi()/180.0)&#160;&#160;&#160; <br />
&#160;&#160;&#160; set @Latitude = @Latitude * (pi()/180.0)&#160;&#160;&#160; <br />
&#160;&#160;&#160; set @Longitude = @Longitude * (pi()/180.0)&#160;&#160;&#160; <br />
<br />
set @lat2 = asin( sin(@Latitude)*cos(@Distance/@Radius) + <br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; cos(@Latitude)*sin(@Distance/@Radius)*cos(@Bearing) );<br />
set @long2 = @Longitude + atn2(sin(@Bearing)*sin(@Distance/@Radius)*cos(@Latitude), <br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; cos(@Distance/@Radius)-sin(@Latitude)*sin(@lat2));<br />
<br />
&#160;&#160;&#160; return&#160; cast(cast(@lat2 * (180/pi()) as numeric(10,6)) as varchar(50)) + ', ' + cast(cast( @long2 * (180/pi()) as numeric(10,6)) as varchar(50))<br />
end</div><br /><br /><a href='http://www.prlsoftware.com'>Philip Leitch</a>&nbsp;&nbsp;<a href='http://www.prlsoftware.com/point_bearing_distance_to_point.aspx'>...</a>]]></description>
    </item>
    <item>
      <title>Distance</title>
      <link>http://www.prlsoftware.com/distance.aspx</link>
      <pubDate>Tue, 09 Mar 2010 03:37:37 GMT</pubDate>
      <guid>http://www.prlsoftware.com/distance.aspx</guid>
      <author>Philip Leitch</author>
      <comments>http://www.prlsoftware.com/distance.aspx</comments>
      <description><![CDATA[<div>CREATE function [dbo].[Distance]</div>
<div>(@Lat1 float, @Long1 float, @Lat2 float, @Long2 float)<br />
/*<br />
www.prlsoftware.com<br />
Author: Philip Leitch<br />
Date: 2010<br />
Purpose: This function measures the distance between two geospatial points.<br />
Copyright: Philip Leitch 2010<br />
Licensing: This code may be used or modified but if the code is included in a software package attribution to me must be made.<br />
Liability: The developer assumes all liability when using this code.<br />
Notes:<br />
Based on the "ellipsoidal Vincenty formula".&#160; This might not be quite as accurate as others, but is close on accuracy and very fast.<br />
Please visit this web site for scripts that are easily converted into any language:<br />
http://www.movable-type.co.uk/scripts/latlong.html</div>
<div>&#160;</div>
<div>The result is in kilometres Multiply by 1000 for metres<br />
<br />
*/<br />
returns float<br />
as<br />
begin<br />
<br />
&#160;&#160;&#160; if abs(@lat1 - @lat2) &lt; 0.00001 and abs(@long1 - @long2) &lt; 0.00001<br />
&#160;&#160;&#160; &#160;&#160;&#160; return 0<br />
&#160;&#160;&#160; <br />
&#160;&#160;&#160; Declare @Radius float<br />
&#160;&#160;&#160; set @Radius = dbo.Earth_Radius((@lat1 + @lat2)/cast(2 as float))<br />
<br />
&#160;&#160;&#160; --Convert to radians<br />
&#160;&#160;&#160; set @Lat1 = @lat1 * (pi()/180)&#160;&#160;&#160; <br />
&#160;&#160;&#160; set @Long1 = @long1 * (pi()/180)&#160;&#160;&#160; <br />
&#160;&#160;&#160; set @Lat2 = @lat2 * (pi()/180)&#160;&#160;&#160; <br />
&#160;&#160;&#160; set @Long2 = @long2 * (pi()/180)&#160;&#160;&#160; <br />
&#160;&#160;&#160; <br />
&#160;&#160;&#160; Declare @return float<br />
&#160;&#160;&#160; <br />
&#160;&#160;&#160; set @return = acos(<br />
&#160;&#160;&#160; Cos(@Lat1) * cos(@Long1) *<br />
&#160;&#160;&#160; Cos(@Lat2) * cos(@Long2) +<br />
&#160;&#160;&#160; <br />
&#160;&#160;&#160; cos(@lat1) * sin(@Long1) *<br />
&#160;&#160;&#160; cos(@lat2) * sin(@Long2) +<br />
&#160;&#160;&#160; <br />
&#160;&#160;&#160; sin(@lat1) * sin(@lat2)<br />
&#160;&#160;&#160; <br />
&#160;&#160;&#160; ) * @Radius<br />
<br />
&#160;&#160;&#160; return @return<br />
end<br />
&#160;</div><br /><br /><a href='http://www.prlsoftware.com'>Philip Leitch</a>&nbsp;&nbsp;<a href='http://www.prlsoftware.com/distance.aspx'>...</a>]]></description>
    </item>
    <item>
      <title>Earth_Radius</title>
      <link>http://www.prlsoftware.com/earth_radius.aspx</link>
      <pubDate>Tue, 09 Mar 2010 03:33:00 GMT</pubDate>
      <guid>http://www.prlsoftware.com/earth_radius.aspx</guid>
      <author>Philip Leitch</author>
      <comments>http://www.prlsoftware.com/earth_radius.aspx</comments>
      <description><![CDATA[<div>CREATE&#160; Function [dbo].[Earth_Radius](@Latitude numeric(9,6)) <br />
/*<br />
www.prlsoftware.com<br />
Author: Philip Leitch<br />
Date: 2010<br />
Purpose: This function determines the Earth's Radias at a specific latitude.<br />
Copyright: Philip Leitch 2010<br />
Licensing: This code may be used or modified but if the code is included in a software package attribution to me must be made.<br />
Liability: The developer assumes all liability when using this code.<br />
Notes:<br />
*/<br />
returns float<br />
as<br />
begin<br />
&#160;&#160;&#160; <br />
&#160;&#160;&#160; declare @Return as float<br />
<br />
&#160;&#160;&#160; Declare @a as float<br />
&#160;&#160;&#160; declare @b as float<br />
&#160;&#160;&#160; set @a = 6378.1370 --Equatorial radius<br />
&#160;&#160;&#160; set @b = 6356.7523 --Polar radius<br />
&#160;&#160;&#160; Declare @LatitudeRadians float<br />
&#160;&#160;&#160; --Convet to radians<br />
&#160;&#160;&#160; set @LatitudeRadians = @latitude * (pi()/180)&#160;&#160;&#160; <br />
&#160;&#160;&#160; set @Return = sqrt(<br />
&#160;&#160;&#160; &#160;&#160;&#160; &#160;&#160;&#160; (power(power(@a, 2)*Cos(@Latitude),2) + power(power(@b, 2)*sin(@Latitude),2))<br />
&#160;&#160;&#160; &#160;&#160;&#160; &#160;&#160;&#160; &#160;&#160;&#160; &#160;&#160;&#160; &#160;&#160;&#160; &#160;&#160;&#160; &#160;&#160;&#160; /<br />
&#160;&#160;&#160; &#160;&#160;&#160; &#160;&#160;&#160; (power(@a*Cos(@Latitude),2) + power(@b*sin(@Latitude),2))<br />
&#160;&#160;&#160; &#160;&#160;&#160; &#160;&#160;&#160; &#160;&#160;&#160; )<br />
&#160;&#160;&#160; return @Return<br />
end<br />
&#160;</div><br /><br /><a href='http://www.prlsoftware.com'>Philip Leitch</a>&nbsp;&nbsp;<a href='http://www.prlsoftware.com/earth_radius.aspx'>...</a>]]></description>
    </item>
    <item>
      <title>Compass_Direction</title>
      <link>http://www.prlsoftware.com/compass_direction.aspx</link>
      <pubDate>Tue, 09 Mar 2010 03:30:24 GMT</pubDate>
      <guid>http://www.prlsoftware.com/compass_direction.aspx</guid>
      <author>Philip Leitch</author>
      <comments>http://www.prlsoftware.com/compass_direction.aspx</comments>
      <description><![CDATA[<div>CREATE Function [dbo].[Compass_Direction] (@Bearing as float)<br />
/*<br />
www.prlsoftware.com<br />
Author: Philip Leitch<br />
Date: 2010<br />
Purpose: This function converts a Bearing into human readable compass text.<br />
Copyright: Philip Leitch 2010<br />
Licensing: This code may be used or modified but if the code is included in a software package attribution to me must be made.<br />
Liability: The developer assumes all liability when using this code.<br />
Notes:<br />
*/<br />
returns Varchar(5) as<br />
Begin<br />
declare @North_Distance float<br />
set @north_distance = dbo.North_Distance(@Bearing)<br />
declare @East_Distance float<br />
set @East_Distance = dbo.East_Distance(@Bearing)<br />
return case when @East_Distance between 11.25 and 33.75 then 'E' when @East_Distance between&#160; 146.25&#160; and 168.75 then 'W' else '' end&#160; +<br />
&#160;case when @North_Distance &lt;= 56.26&#160; then 'N' when @North_Distance &gt;= 123.74&#160; then 'S' else '' end +<br />
&#160;case when @North_Distance between 11.25 and 33.75 then 'N' when @North_Distance between&#160; 146.25&#160; and 168.75 then 'S' else '' end&#160; +<br />
&#160;case when @East_Distance between 45+11.25 and 45+33.75 then 'E' when @East_Distance between&#160; 146.25-45&#160; and 168.75-45 then 'W' else '' end&#160; +<br />
&#160;case when @North_Distance between 45+11.25 and 45+33.75 then 'N' when @North_Distance between&#160; 146.25-45&#160; and 168.75-45 then 'S' else '' end&#160; +<br />
<br />
&#160;case when @East_Distance &lt;= 56.26&#160; then 'E' when @East_Distance &gt;= 123.74&#160; then 'W' else '' end <br />
end</div><br /><br /><a href='http://www.prlsoftware.com'>Philip Leitch</a>&nbsp;&nbsp;<a href='http://www.prlsoftware.com/compass_direction.aspx'>...</a>]]></description>
    </item>
    <item>
      <title>Angle_To_Bearing</title>
      <link>http://www.prlsoftware.com/angle_to_bearing.aspx</link>
      <pubDate>Tue, 09 Mar 2010 03:18:51 GMT</pubDate>
      <guid>http://www.prlsoftware.com/angle_to_bearing.aspx</guid>
      <author>Philip Leitch</author>
      <comments>http://www.prlsoftware.com/angle_to_bearing.aspx</comments>
      <description><![CDATA[<div>CREATE function [dbo].[Angle_To_Bearing]( @North as float, @East as float)<br />
/*<br />
www.prlsoftware.com<br />
Author: Philip Leitch<br />
Date: 2010<br />
Purpose: This function converts a North and East distance to a bearing.<br />
Copyright: Philip Leitch 2010<br />
Licensing: This code may be used or modified but if the code is included in a software package attribution to me must be made.<br />
Liability: The developer assumes all liability when using this code.<br />
Notes:<br />
*/<br />
returns float <br />
as<br />
begin<br />
&#160;&#160;&#160; return case when @east &lt;= 90 then @North else 360 - @North end<br />
End</div><br /><br /><a href='http://www.prlsoftware.com'>Philip Leitch</a>&nbsp;&nbsp;<a href='http://www.prlsoftware.com/angle_to_bearing.aspx'>...</a>]]></description>
    </item>
    <item>
      <title>East_Distance</title>
      <link>http://www.prlsoftware.com/east_distance.aspx</link>
      <pubDate>Tue, 09 Mar 2010 03:18:10 GMT</pubDate>
      <guid>http://www.prlsoftware.com/east_distance.aspx</guid>
      <author>Philip Leitch</author>
      <comments>http://www.prlsoftware.com/east_distance.aspx</comments>
      <description><![CDATA[<div>CREATE Function [dbo].[East_Distance] (@Bearing as float)<br />
/*<br />
www.prlsoftware.com<br />
Author: Philip Leitch<br />
Date: 2010<br />
Purpose: This function measures the angular distance from due East to the vector bearing.<br />
Copyright: Philip Leitch 2010<br />
Licensing: This code may be used or modified but if the code is included in a software package attribution to me must be made.<br />
Liability: The developer assumes all liability when using this code.<br />
Notes:<br />
*/<br />
returns Float as<br />
begin<br />
&#160;&#160;&#160; return (case when (@Bearing &gt; 270) then (450 - @Bearing) else (abs((@Bearing - 90))) end)<br />
end</div><br /><br /><a href='http://www.prlsoftware.com'>Philip Leitch</a>&nbsp;&nbsp;<a href='http://www.prlsoftware.com/east_distance.aspx'>...</a>]]></description>
    </item>
    <item>
      <title>North_Distance</title>
      <link>http://www.prlsoftware.com/north_distance.aspx</link>
      <pubDate>Tue, 09 Mar 2010 03:16:31 GMT</pubDate>
      <guid>http://www.prlsoftware.com/north_distance.aspx</guid>
      <author>Philip Leitch</author>
      <comments>http://www.prlsoftware.com/north_distance.aspx</comments>
      <description><![CDATA[<div>CREATE Function [dbo].[North_Distance] (@Bearing as float)<br />
/*<br />
www.prlsoftware.com<br />
Author: Philip Leitch<br />
Date: 2010<br />
Purpose: This function measures the angular distance from due North to the vector bearing.<br />
Copyright: Philip Leitch 2010<br />
Licensing: This code may be used or modified but if the code is included in a software package attribution to me must be made.<br />
Liability: The developer assumes all liability when using this code.<br />
Notes:<br />
*/<br />
returns Float as<br />
begin<br />
&#160;&#160;&#160; return (case when (@Bearing &gt; 180) then (360 - @Bearing) else @Bearing end)<br />
end</div><br /><br /><a href='http://www.prlsoftware.com'>Philip Leitch</a>&nbsp;&nbsp;<a href='http://www.prlsoftware.com/north_distance.aspx'>...</a>]]></description>
    </item>
    <item>
      <title>Working with Angles</title>
      <link>http://www.prlsoftware.com/working-with-angles.aspx</link>
      <pubDate>Tue, 09 Mar 2010 03:07:23 GMT</pubDate>
      <guid>http://www.prlsoftware.com/working-with-angles.aspx</guid>
      <author>Philip Leitch</author>
      <comments>http://www.prlsoftware.com/working-with-angles.aspx</comments>
      <description><![CDATA[<p>When working on directional geospatial data it is often useful to use a bearing (direction of travel).&#160; Although using the bearing is very helpful, it does pose a problem: what to do with North.&#160; I recently worked on a large project where I needed to determine average bearing of a vehicle at a specific location (the location would be repeated weekly).&#160;</p>
<p>If the vehicle repeatedly travels due North then the bearing reading that is taken will variously be just over 0 degrees or just under 360 degrees (either side of due North).  The average of this would actually be due South, which is the polar opposite of the direction actually being travelled.  This issue doesn’t only apply to premises that are exactly due North, but by any premises where the bearing of the truck occasionally crosses due North.  For example, 350 degrees direction might occasionally has a value of 0-5 degrees.</p>
<p>I’ve come up with a way to resolve this by using “Angular Distance”.  That is, instead of using bearing which comes back on itself, I’ve used the number of degrees away from North.  This way NW would be 45 degrees, as would also be NE, meaning that a truck travelling due North would have an average Angular Distance from north of zero.  Similar to a normal compass, South would also be 180 degrees.&#160; By doing this, we lose any ability to identify East/West travel, so a second angular distance is required, and I’ve chosen the distance from due East, where 0 is due east and 180 is due West.</p>
<p>Therefore, due north has a Northern Angular Distance   Using these two angular distances the original bearing can be easily determined.  For instance, an Angular Distance of 45 from North and 45 from East means the truck must be travelling NE.  This type of information becomes essential to determining which side of the road a truck is collecting bins.  Calculating the total change in bearing can be unintuitive.  A change from NE (45 Degrees) to NW (315 Degrees) is a total angular distance of zero from North (as this is the angular distance, not angle).  However, over this same change in bearing the angular distance from east is 90 Degrees (45 to 135).  Therefore the actual net angular distance of to bearings is calculated by using the Bearing, not the angular distances.</p>
<p>Compass_Direction – Turns a standard bearing (such as 223) into a bearing (SW).  The divisions are N, NNE, NE, ENE, E, ESE, SE, SSE, S, SSW, SW, WSW, W, WNW, NW, NNW</p>
<p>North_Distance – Provides the Northern Angular Distance from a bearing.</p>
<p>East_Distiance – Provides the Eastern Angular Distance from a bearing.</p>
<p>Angular_Distance - Given the North_Distance and East_Distance it returns the smallest angle between the two vectors.</p>
<p>I will post each of these functions seperately to aid finding them.</p>
<p>&#160;</p>
<p>&#160;</p>
<p>&#160;</p>
<p>&#160;</p><br /><br /><a href='http://www.prlsoftware.com'>Philip Leitch</a>&nbsp;&nbsp;<a href='http://www.prlsoftware.com/working-with-angles.aspx'>...</a>]]></description>
    </item>
  </channel>
</rss>