 <?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/style/rss1.xsl" ?>
<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>
    <docs>http://www.rssboard.org/rss-specification</docs>
    <generator>mojoPortal Blog Module</generator>
    <managingEditor>Philip@prlsoftware.com</managingEditor>
    <ttl>120</ttl>
    <item>
      <title>Working Days</title>
      <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 /><a href='http://www.prlsoftware.com/working-days.aspx'>Philip Leitch</a>&nbsp;&nbsp;<a href='http://www.prlsoftware.com/working-days.aspx'>...</a>]]></description>
      <link>http://www.prlsoftware.com/working-days.aspx</link>
      <author>Philip Leitch</author>
      <comments>http://www.prlsoftware.com/working-days.aspx</comments>
      <guid isPermaLink="true">http://www.prlsoftware.com/working-days.aspx</guid>
      <pubDate>Thu, 15 Apr 2010 03:51:20 GMT</pubDate>
    </item>
    <item>
      <title>Bearing</title>
      <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 /><a href='http://www.prlsoftware.com/bearing.aspx'>Philip Leitch</a>&nbsp;&nbsp;<a href='http://www.prlsoftware.com/bearing.aspx'>...</a>]]></description>
      <link>http://www.prlsoftware.com/bearing.aspx</link>
      <author>Philip Leitch</author>
      <comments>http://www.prlsoftware.com/bearing.aspx</comments>
      <guid isPermaLink="true">http://www.prlsoftware.com/bearing.aspx</guid>
      <pubDate>Tue, 13 Apr 2010 00:11:10 GMT</pubDate>
    </item>
    <item>
      <title>Point_Bearing_Distance_To_Point</title>
      <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 /><a href='http://www.prlsoftware.com/point_bearing_distance_to_point.aspx'>Philip Leitch</a>&nbsp;&nbsp;<a href='http://www.prlsoftware.com/point_bearing_distance_to_point.aspx'>...</a>]]></description>
      <link>http://www.prlsoftware.com/point_bearing_distance_to_point.aspx</link>
      <author>Philip Leitch</author>
      <comments>http://www.prlsoftware.com/point_bearing_distance_to_point.aspx</comments>
      <guid isPermaLink="true">http://www.prlsoftware.com/point_bearing_distance_to_point.aspx</guid>
      <pubDate>Tue, 09 Mar 2010 03:42:41 GMT</pubDate>
    </item>
    <item>
      <title>Distance</title>
      <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 /><a href='http://www.prlsoftware.com/distance.aspx'>Philip Leitch</a>&nbsp;&nbsp;<a href='http://www.prlsoftware.com/distance.aspx'>...</a>]]></description>
      <link>http://www.prlsoftware.com/distance.aspx</link>
      <author>Philip Leitch</author>
      <comments>http://www.prlsoftware.com/distance.aspx</comments>
      <guid isPermaLink="true">http://www.prlsoftware.com/distance.aspx</guid>
      <pubDate>Tue, 09 Mar 2010 03:37:37 GMT</pubDate>
    </item>
    <item>
      <title>Earth_Radius</title>
      <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 /><a href='http://www.prlsoftware.com/earth_radius.aspx'>Philip Leitch</a>&nbsp;&nbsp;<a href='http://www.prlsoftware.com/earth_radius.aspx'>...</a>]]></description>
      <link>http://www.prlsoftware.com/earth_radius.aspx</link>
      <author>Philip Leitch</author>
      <comments>http://www.prlsoftware.com/earth_radius.aspx</comments>
      <guid isPermaLink="true">http://www.prlsoftware.com/earth_radius.aspx</guid>
      <pubDate>Tue, 09 Mar 2010 03:33:00 GMT</pubDate>
    </item>
    <item>
      <title>Compass_Direction</title>
      <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 /><a href='http://www.prlsoftware.com/compass_direction.aspx'>Philip Leitch</a>&nbsp;&nbsp;<a href='http://www.prlsoftware.com/compass_direction.aspx'>...</a>]]></description>
      <link>http://www.prlsoftware.com/compass_direction.aspx</link>
      <author>Philip Leitch</author>
      <comments>http://www.prlsoftware.com/compass_direction.aspx</comments>
      <guid isPermaLink="true">http://www.prlsoftware.com/compass_direction.aspx</guid>
      <pubDate>Tue, 09 Mar 2010 03:30:24 GMT</pubDate>
    </item>
    <item>
      <title>Angle_To_Bearing</title>
      <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 /><a href='http://www.prlsoftware.com/angle_to_bearing.aspx'>Philip Leitch</a>&nbsp;&nbsp;<a href='http://www.prlsoftware.com/angle_to_bearing.aspx'>...</a>]]></description>
      <link>http://www.prlsoftware.com/angle_to_bearing.aspx</link>
      <author>Philip Leitch</author>
      <comments>http://www.prlsoftware.com/angle_to_bearing.aspx</comments>
      <guid isPermaLink="true">http://www.prlsoftware.com/angle_to_bearing.aspx</guid>
      <pubDate>Tue, 09 Mar 2010 03:18:51 GMT</pubDate>
    </item>
    <item>
      <title>East_Distance</title>
      <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 /><a href='http://www.prlsoftware.com/east_distance.aspx'>Philip Leitch</a>&nbsp;&nbsp;<a href='http://www.prlsoftware.com/east_distance.aspx'>...</a>]]></description>
      <link>http://www.prlsoftware.com/east_distance.aspx</link>
      <author>Philip Leitch</author>
      <comments>http://www.prlsoftware.com/east_distance.aspx</comments>
      <guid isPermaLink="true">http://www.prlsoftware.com/east_distance.aspx</guid>
      <pubDate>Tue, 09 Mar 2010 03:18:10 GMT</pubDate>
    </item>
    <item>
      <title>North_Distance</title>
      <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 /><a href='http://www.prlsoftware.com/north_distance.aspx'>Philip Leitch</a>&nbsp;&nbsp;<a href='http://www.prlsoftware.com/north_distance.aspx'>...</a>]]></description>
      <link>http://www.prlsoftware.com/north_distance.aspx</link>
      <author>Philip Leitch</author>
      <comments>http://www.prlsoftware.com/north_distance.aspx</comments>
      <guid isPermaLink="true">http://www.prlsoftware.com/north_distance.aspx</guid>
      <pubDate>Tue, 09 Mar 2010 03:16:31 GMT</pubDate>
    </item>
    <item>
      <title>Working with Angles</title>
      <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 /><a href='http://www.prlsoftware.com/working-with-angles.aspx'>Philip Leitch</a>&nbsp;&nbsp;<a href='http://www.prlsoftware.com/working-with-angles.aspx'>...</a>]]></description>
      <link>http://www.prlsoftware.com/working-with-angles.aspx</link>
      <author>Philip Leitch</author>
      <comments>http://www.prlsoftware.com/working-with-angles.aspx</comments>
      <guid isPermaLink="true">http://www.prlsoftware.com/working-with-angles.aspx</guid>
      <pubDate>Tue, 09 Mar 2010 03:07:23 GMT</pubDate>
    </item>
    <item>
      <title>Create Insert</title>
      <description><![CDATA[<div>Create&#160; proc&#160; Create_Insert(@Table_Name as varchar(500))<br />
as<br />
/*<br />
www.prlsoftware.com<br />
Author: Philip Leitch<br />
Date: 2009<br />
Purpose: This procedure creates an easy "Insert" script - useful for transfering data.<br />
Copyright: Philip Leitch 2009<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 />
*/<br />
begin<br />
Declare @SQL as varchar(8000)<br />
select @sql = case when @sql is null then '' else @SQL + ', '''''' +' end + char(10) + <br />
'isnull(' + case when syscolumns.collation is not null then<br />
' replace([' + Name + '] , '''''''','''''''''''')&#160; '<br />
else<br />
'&#160; cast([' + Name + '] as varchar) '<br />
end +&#160; ', ''null'') + '''''' '<br />
from syscolumns where id = object_id(@Table_Name)<br />
set @sql = 'Select ''insert into [' + @Table_Name + '] Values('''''' + ' + @sql&#160; + ')''' +<br />
&#160;char(10) + ' from [' + @Table_Name + ']'<br />
print @sql<br />
exec (@sql)<br />
<br />
end</div><br /><a href='http://www.prlsoftware.com/create-insert.aspx'>Philip Leitch</a>&nbsp;&nbsp;<a href='http://www.prlsoftware.com/create-insert.aspx'>...</a>]]></description>
      <link>http://www.prlsoftware.com/create-insert.aspx</link>
      <author>Philip Leitch</author>
      <comments>http://www.prlsoftware.com/create-insert.aspx</comments>
      <guid isPermaLink="true">http://www.prlsoftware.com/create-insert.aspx</guid>
      <pubDate>Tue, 10 Nov 2009 07:40:42 GMT</pubDate>
    </item>
    <item>
      <title>Remove Backup History</title>
      <description><![CDATA[<div>Taken from <a href="http://weblogs.sqlteam.com/tarad/archive/2004/07/02/1704.aspx">http://weblogs.sqlteam.com/tarad/archive/2004/07/02/1704.aspx</a></div>
<div>&#160;</div>
<div>&#160;</div>
<div>NOTE - there&#160; are two procedures here, the first for SQL Server 2000, the second for 2005/2008</div>
<h3>SQL Server 2000</h3>
<p>CREATE PROC isp_DeleteBackupHistory<br />
(@DaysToRetain int)<br />
AS</p>
<p>SET NOCOUNT ON</p>
<p>DECLARE @Err int<br />
DECLARE @rc int</p>
<p>BEGIN TRAN</p>
<p>&#160;DELETE FROM msdb..restorefile<br />
&#160;FROM msdb..restorefile rf<br />
&#160;INNER JOIN msdb..restorehistory rh <br />
&#160;ON rf.restore_history_id = rh.restore_history_id<br />
&#160;INNER JOIN msdb..backupset bs <br />
&#160;ON rh.backup_set_id = bs.backup_set_id<br />
&#160;WHERE bs.backup_finish_date &lt; (GETDATE() - @DaysToRetain)</p>
<p>&#160;SET @Err = @@ERROR</p>
<p>&#160;IF @Err &lt;&gt; 0<br />
&#160;&#160;GOTO Error_Exit<br />
&#160;<br />
&#160;DELETE FROM msdb..restorefilegroup<br />
&#160;FROM msdb..restorefilegroup rfg<br />
&#160;INNER JOIN msdb..restorehistory rh <br />
&#160;ON rfg.restore_history_id = rh.restore_history_id<br />
&#160;INNER JOIN msdb..backupset bs <br />
&#160;ON rh.backup_set_id = bs.backup_set_id<br />
&#160;WHERE bs.backup_finish_date &lt; (GETDATE() - @DaysToRetain)</p>
<p>&#160;SET @Err = @@ERROR</p>
<p>&#160;IF @Err &lt;&gt; 0<br />
&#160;&#160;GOTO Error_Exit<br />
&#160;<br />
&#160;DELETE FROM msdb..restorehistory<br />
&#160;FROM msdb..restorehistory rh<br />
&#160;INNER JOIN msdb..backupset bs <br />
&#160;ON rh.backup_set_id = bs.backup_set_id<br />
&#160;WHERE bs.backup_finish_date &lt; (GETDATE() - @DaysToRetain)</p>
<p>&#160;SET @Err = @@ERROR</p>
<p>&#160;IF @Err &lt;&gt; 0<br />
&#160;&#160;GOTO Error_Exit<br />
&#160;<br />
&#160;SELECT media_set_id, backup_finish_date<br />
&#160;INTO #Temp <br />
&#160;FROM msdb..backupset<br />
&#160;WHERE backup_finish_date &lt; (GETDATE() - @DaysToRetain)</p>
<p>&#160;SET @Err = @@ERROR</p>
<p>&#160;IF @Err &lt;&gt; 0<br />
&#160;&#160;GOTO Error_Exit<br />
&#160;<br />
&#160;DELETE FROM msdb..backupfile<br />
&#160;FROM msdb..backupfile bf<br />
&#160;INNER JOIN msdb..backupset bs <br />
&#160;ON bf.backup_set_id = bs.backup_set_id<br />
&#160;INNER JOIN #Temp t<br />
&#160;ON bs.media_set_id = t.media_set_id<br />
&#160;WHERE bs.backup_finish_date &lt; (GETDATE() - @DaysToRetain)</p>
<p>&#160;SET @Err = @@ERROR</p>
<p>&#160;IF @Err &lt;&gt; 0<br />
&#160;&#160;GOTO Error_Exit<br />
&#160;<br />
&#160;DELETE FROM msdb..backupset<br />
&#160;FROM msdb..backupset bs<br />
&#160;INNER JOIN #Temp t<br />
&#160;ON bs.media_set_id = t.media_set_id</p>
<p>&#160;SET @Err = @@ERROR</p>
<p>&#160;IF @Err &lt;&gt; 0<br />
&#160;&#160;GOTO Error_Exit<br />
&#160;<br />
&#160;DELETE FROM msdb..backupmediafamily<br />
&#160;FROM msdb..backupmediafamily bmf<br />
&#160;INNER JOIN msdb..backupmediaset bms <br />
&#160;ON bmf.media_set_id = bms.media_set_id<br />
&#160;INNER JOIN #Temp t <br />
&#160;ON bms.media_set_id = t.media_set_id</p>
<p>&#160;SET @Err = @@ERROR</p>
<p>&#160;IF @Err &lt;&gt; 0<br />
&#160;&#160;GOTO Error_Exit<br />
&#160;<br />
&#160;DELETE FROM msdb..backupmediaset<br />
&#160;FROM msdb..backupmediaset bms<br />
&#160;INNER JOIN #Temp t <br />
&#160;ON bms.media_set_id = t.media_set_id</p>
<p>&#160;SET @Err = @@ERROR</p>
<p>&#160;IF @Err &lt;&gt; 0<br />
&#160;&#160;GOTO Error_Exit</p>
<p>COMMIT TRAN</p>
<p>SET @rc = 0</p>
<p>GOTO isp_DeleteBackupHistory_Exit</p>
<p>Error_Exit:</p>
<p>ROLLBACK TRAN</p>
<p>SET @rc = -1</p>
<p>isp_DeleteBackupHistory_Exit:</p>
<p>DROP TABLE #Temp</p>
<p>SET NOCOUNT OFF</p>
<p>RETURN @rc</p>
<h3>SQL Server 2005 or 2008</h3>
<div><span id="apnlCommentsWrapper$RBS_Holder"><span ajaxcall="async" id="apnlCommentsWrapper">CREATE PROCEDURE [dbo].[Delete_Backup_History] <br />
(@days_to_retain int = 186) <br />
AS <br />
BEGIN<br />
SET NOCOUNT ON <br />
<br />
DECLARE @Err int <br />
DECLARE @rc int <br />
DECLARE @target_date datetime<br />
<br />
SET @target_date = GETDATE() - @days_to_retain<br />
<br />
BEGIN TRAN <br />
<br />
DELETE FROM msdb..restorefile <br />
FROM msdb..restorefile rf <br />
INNER JOIN msdb..restorehistory rh ON rf.restore_history_id = rh.restore_history_id <br />
INNER JOIN msdb..backupset bs ON rh.backup_set_id = bs.backup_set_id <br />
WHERE bs.backup_finish_date &lt; @target_date <br />
<br />
SET @Err = @@ERROR <br />
<br />
IF @Err &lt;&gt; 0 <br />
GOTO Error_Exit <br />
<br />
DELETE FROM msdb..restorefilegroup <br />
FROM msdb..restorefilegroup rfg <br />
INNER JOIN msdb..restorehistory rh ON rfg.restore_history_id = rh.restore_history_id <br />
INNER JOIN msdb..backupset bs ON rh.backup_set_id = bs.backup_set_id <br />
WHERE bs.backup_finish_date &lt; @target_date <br />
<br />
SET @Err = @@ERROR <br />
<br />
IF @Err &lt;&gt; 0 <br />
GOTO Error_Exit <br />
<br />
DELETE FROM msdb..restorehistory <br />
FROM msdb..restorehistory rh INNER JOIN msdb..backupset bs <br />
ON rh.backup_set_id = bs.backup_set_id <br />
WHERE bs.backup_finish_date &lt; @target_date <br />
<br />
SET @Err = @@ERROR <br />
<br />
IF @Err &lt;&gt; 0 <br />
GOTO Error_Exit <br />
<br />
SELECT media_set_id, backup_finish_date <br />
INTO #Temp <br />
FROM msdb..backupset <br />
WHERE backup_finish_date &lt; @target_date <br />
<br />
SET @Err = @@ERROR <br />
<br />
IF @Err &lt;&gt; 0 <br />
GOTO Error_Exit <br />
<br />
DELETE FROM msdb..backupfilegroup <br />
FROM msdb..backupfilegroup bfg <br />
INNER JOIN msdb..backupset bs ON bfg.backup_set_id = bs.backup_set_id <br />
INNER JOIN #Temp t ON bs.media_set_id = t.media_set_id <br />
WHERE bs.backup_finish_date &lt; @target_date <br />
<br />
SET @Err = @@ERROR <br />
<br />
IF @Err &lt;&gt; 0 <br />
GOTO Error_Exit <br />
<br />
DELETE FROM msdb..backupfile <br />
FROM msdb..backupfile bf <br />
INNER JOIN msdb..backupset bs ON bf.backup_set_id = bs.backup_set_id <br />
INNER JOIN #Temp t ON bs.media_set_id = t.media_set_id <br />
WHERE bs.backup_finish_date &lt; @target_date <br />
<br />
SET @Err = @@ERROR <br />
<br />
IF @Err &lt;&gt; 0 <br />
GOTO Error_Exit <br />
<br />
DELETE FROM msdb..backupset <br />
FROM msdb..backupset bs <br />
INNER JOIN #Temp t ON bs.media_set_id = t.media_set_id <br />
WHERE bs.backup_finish_date &lt; @target_date <br />
<br />
SET @Err = @@ERROR <br />
<br />
IF @Err &lt;&gt; 0 <br />
GOTO Error_Exit <br />
<br />
DELETE FROM msdb..backupmediafamily <br />
FROM msdb..backupmediafamily bmf <br />
INNER JOIN msdb..backupmediaset bms ON bmf.media_set_id = bms.media_set_id <br />
INNER JOIN #Temp t ON bms.media_set_id = t.media_set_id <br />
WHERE NOT EXISTS (SELECT media_set_id FROM msdb..backupset bs <br />
WHERE bs.media_set_id = t.media_set_id <br />
AND bs.backup_finish_date &gt;= @target_date)<br />
<br />
SET @Err = @@ERROR <br />
<br />
IF @Err &lt;&gt; 0 <br />
GOTO Error_Exit <br />
<br />
DELETE FROM msdb..backupmediaset <br />
FROM msdb..backupmediaset bms <br />
INNER JOIN #Temp t ON bms.media_set_id = t.media_set_id <br />
WHERE NOT EXISTS (SELECT media_set_id FROM msdb..backupset bs <br />
WHERE bs.media_set_id = t.media_set_id <br />
AND bs.backup_finish_date &gt;= @target_date)<br />
<br />
<br />
SET @Err = @@ERROR <br />
<br />
IF @Err &lt;&gt; 0 <br />
GOTO Error_Exit <br />
<br />
COMMIT TRAN <br />
<br />
SET @rc = 0 <br />
<br />
GOTO isp_DeleteBackupHistory_Exit <br />
<br />
Error_Exit: <br />
<br />
ROLLBACK TRAN <br />
<br />
SET @rc = -1 <br />
<br />
isp_DeleteBackupHistory_Exit: <br />
<br />
DROP TABLE #Temp <br />
<br />
SET NOCOUNT OFF <br />
<br />
RETURN @rc <br />
END<br />
<br />
<br />
</span></span></div><br /><a href='http://www.prlsoftware.com/remove-backup-history.aspx'>Philip Leitch</a>&nbsp;&nbsp;<a href='http://www.prlsoftware.com/remove-backup-history.aspx'>...</a>]]></description>
      <link>http://www.prlsoftware.com/remove-backup-history.aspx</link>
      <author>Philip Leitch</author>
      <comments>http://www.prlsoftware.com/remove-backup-history.aspx</comments>
      <guid isPermaLink="true">http://www.prlsoftware.com/remove-backup-history.aspx</guid>
      <pubDate>Mon, 09 Nov 2009 23:11:50 GMT</pubDate>
    </item>
    <item>
      <title>OverlapDates</title>
      <description><![CDATA[<div>CREATE FUNCTION OverlapDates <br />
&#160;&#160;&#160; (@Startdate as datetime= null,<br />
&#160;&#160;&#160; @EndDate as datetime = null,<br />
&#160;&#160;&#160; @Startdate2 as datetime= null,<br />
&#160;&#160;&#160; @EndDate2 as datetime = null)<br />
RETURNS bit<br />
AS<br />
/*<br />
www.prlsoftware.com<br />
Author: Philip Leitch<br />
Date: 2009<br />
Purpose: This function determines whether pairs of dates overlap or not.<br />
Copyright: Philip Leitch 2009<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 />
The ranges overlap if the first start or end is between the start and end of the second time range.<br />
The ranges overlap i the second start or end is between the start and end of the first time range.<br />
The "order" of start/finish is assumed to be in advancing time, nulls are considered as "N/A".&#160; <br />
<br />
For instance, does my life overlap with another person's life.&#160; if we are both still alive then we both <br />
have no end date defined because it hasn't happened yet.&#160; Therefore we automatically overlap.&#160; <br />
*/<br />
BEGIN<br />
&#160;&#160;&#160; Declare @ReturnValue as bit<br />
<br />
&#160;&#160;&#160; if (&#160;&#160;&#160; @Startdate Between&#160; isnull(@Startdate2,@Startdate-1) and isnull(@EndDate2, @startdate+1)<br />
&#160;&#160;&#160; &#160;&#160;&#160; or<br />
&#160;&#160;&#160; &#160;&#160;&#160; @EndDate Between&#160; isnull(@Startdate2,@EndDate-1) and isnull(@EndDate2, @EndDate+1)<br />
&#160;&#160;&#160; &#160;&#160;&#160; or<br />
&#160;&#160;&#160; &#160;&#160;&#160; @Startdate2 Between&#160; isnull(@Startdate,@Startdate2-1) and isnull(@EndDate, @Startdate2+1)<br />
&#160;&#160;&#160; &#160;&#160;&#160; or<br />
&#160;&#160;&#160; &#160;&#160;&#160; @EndDate2 Between&#160; isnull(@Startdate,@EndDate-1) and isnull(@EndDate, @EndDate+1)<br />
&#160;&#160;&#160; )<br />
&#160;&#160;&#160; &#160;&#160;&#160; set @ReturnValue = 1<br />
&#160;&#160;&#160; else<br />
&#160;&#160;&#160; &#160;&#160;&#160; set @ReturnValue = 0<br />
&#160;&#160;&#160; return @ReturnValue<br />
End<br />
GO<br />
<br />
-- =============================================<br />
-- Example to execute function<br />
-- =============================================<br />
SELECT dbo.OverlapDates('1/1/1999', '1/1/2000', '1/1/2002','1/1/2008'), 'Standard ranges not overlapping, first earlier than second'<br />
union all SELECT dbo.OverlapDates('1/1/02','1/1/2008', '1/1/1999', '1/1/2000'), 'Standard ranges not overlapping, first later than second'<br />
union all SELECT dbo.OverlapDates('1/1/1999', '1/1/2008', '1/1/2002','1/1/2003'), 'Standard ranges overlapping'<br />
union all SELECT dbo.OverlapDates('1/1/2003', null, '1/1/05',null) As Result, 'Second range higher than first, neither have a known end' as Test<br />
union all SELECT dbo.OverlapDates('1/1/2003', null, '1/1/01',null), 'Second range lower than first, nether have a known end'<br />
union all SELECT dbo.OverlapDates('1/1/2003', '1/1/2004', '1/1/01',null), 'Second range started before first but without an end'<br />
union all SELECT dbo.OverlapDates('1/1/2003', null, null,'1/5/2005'), 'Second range ended after first started.&#160; First range has no end, second range no beginning'<br />
union all SELECT dbo.OverlapDates(null, '1/1/2000', '1/1/02',null), 'Second range starts but has no known end, second range has no known start but ends before the start of the second range'</div><br /><a href='http://www.prlsoftware.com/1overlapdates.aspx'>Philip Leitch</a>&nbsp;&nbsp;<a href='http://www.prlsoftware.com/1overlapdates.aspx'>...</a>]]></description>
      <link>http://www.prlsoftware.com/1overlapdates.aspx</link>
      <author>Philip Leitch</author>
      <comments>http://www.prlsoftware.com/1overlapdates.aspx</comments>
      <guid isPermaLink="true">http://www.prlsoftware.com/1overlapdates.aspx</guid>
      <pubDate>Sun, 11 Oct 2009 22:00:00 GMT</pubDate>
    </item>
    <item>
      <title>Data_Size</title>
      <description><![CDATA[<div>create&#160; View Data_Size as</div>
<div>/*<br />
www.prlsoftware.com<br />
Author: Philip Leitch<br />
Date: 2009<br />
Purpose: Provides a way of viewing the size of tables, and what part of the table contribute to that size.<br />
Copyright: Philip Leitch 2009<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:</div>
<div>A number of columns have been added to provide more insight into the tables.</div>
<div>&#160;</div>
<div>A K_Per_Row column is shown to give an idea as to how "dense" the inforamtion is.&#160; A low value indicates that each new row adds very little to space.&#160; This is useful when trying to identify where size savings can be made.</div>
<div>&#160;</div>
<div>The number of Indexes is normally very tightly aligned with the K_Per_Row.&#160; The more indexes on a table the more space each row adds (an additional record will mean more information in each and every index).</div>
<div>&#160;</div>
<div>An additional column "Has_Clustered Index" is included as a general warning.&#160; The Clustered Index of a table is how the data is stored on the physical disk.&#160; A large table with no clustered index and no primary key is called a "Heap" and quickly becomes very fragmented since there is literally no logic as to how data is organised for this table on the disk.&#160; A primary key is a proxy for a clustered index in that it acts as a clustered index when no clustered index exists.</div>
<div><br />
*/<br />
select<br />
&#160; sysobjects.name as Table_Name,<br />
sum(cast(reserved as bigint)*8) as Total_Size ,<br />
sum ( case when (indid =0 or indid=1) then cast(reserved as bigint)*8 else 0 end) as Data_and_Indexes,<br />
sum ( case when (indid not in (255,0,1)) then cast(reserved as bigint)*8 else 0 end) as Index_Size,<br />
sum ( case when (indid =0 or indid=1) then cast(reserved as bigint)*8 else 0 end) - <br />
sum ( case when (indid not in (255,0,1)) then cast(reserved as bigint)*8 else 0 end)&#160; as Data_Size,<br />
sum ( case when (indid=255) then cast(reserved as bigint)*8 else 0 end) as Text_Size,<br />
sum(case when indid in (0,1) then rowcnt else 0 end) as Rough_row_Count,<br />
cast(sum(cast(reserved as bigint)*8)/ cast(case when sum(case when indid in (0,1) then rowcnt else 0 end) = 0 then 1 else sum(case when indid in (0,1) then rowcnt else 0 end) end as float) as numeric(12,2)) as [K_Per_Row],<br />
<br />
sum(case when indid &gt; 1 then 1 else 0 end) as Indexes,<br />
case when isnull(sum(case when indid = 0 then 1 else 0 end),1) &gt; 0 then 'No Clustered Index' else '' end&#160; as Has_Clustered_Index<br />
from dbo.sysindexes <br />
join dbo.sysobjects on<br />
sysindexes.id = sysobjects.id<br />
where reserved &lt;&gt; 0 and sysobjects.xtype = 'U '<br />
group by <br />
&#160; sysobjects.name<br />
go<br />
select * from Data_Size order by total_size desc</div>
<p>&#160;</p><br /><a href='http://www.prlsoftware.com/data_size.aspx'>Philip Leitch</a>&nbsp;&nbsp;<a href='http://www.prlsoftware.com/data_size.aspx'>...</a>]]></description>
      <link>http://www.prlsoftware.com/data_size.aspx</link>
      <author>Philip Leitch</author>
      <comments>http://www.prlsoftware.com/data_size.aspx</comments>
      <guid isPermaLink="true">http://www.prlsoftware.com/data_size.aspx</guid>
      <pubDate>Thu, 08 Oct 2009 01:40:03 GMT</pubDate>
    </item>
    <item>
      <title>Progress Bar</title>
      <description><![CDATA[<div>Create procedure Progress(<br />
&#160;&#160;&#160; @Min as float,<br />
&#160;&#160;&#160; @Max as float,<br />
&#160;&#160;&#160; @Value as float,<br />
&#160;&#160;&#160; @Length as int = 100)<br />
as<br />
/*<br />
www.prlsoftware.com<br />
Author: Philip Leitch<br />
Date: 2009<br />
Purpose: Creates a text based progress bar. <br />
Copyright: Philip Leitch 2009<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 />
Best used with text return.<br />
*/<br />
begin<br />
&#160;&#160;&#160; if @min &gt; @max or @min &gt; @value<br />
&#160;&#160;&#160; &#160;&#160;&#160; begin<br />
&#160;&#160;&#160; &#160;&#160;&#160; print 'Invalid Range.'<br />
&#160;&#160;&#160; &#160;&#160;&#160; return <br />
&#160;&#160;&#160; &#160;&#160;&#160; end<br />
&#160;&#160;&#160; declare @Stars as int<br />
<br />
&#160;&#160;&#160; set @Stars = round((((@value - @min) )/ (@max - @min) * @Length) ,0)<br />
&#160;&#160;&#160; print '[' + replicate('*', @Stars) + replicate ('-', @Length-@Stars) + '] ' + cast(cast(round((((@value - @min) )/ (@max - @min) * 100) ,2) as numeric(8,2)) as varchar) + '%'<br />
&#160;&#160;&#160; return<br />
end</div><br /><a href='http://www.prlsoftware.com/progress-bar.aspx'>Philip Leitch</a>&nbsp;&nbsp;<a href='http://www.prlsoftware.com/progress-bar.aspx'>...</a>]]></description>
      <link>http://www.prlsoftware.com/progress-bar.aspx</link>
      <author>Philip Leitch</author>
      <comments>http://www.prlsoftware.com/progress-bar.aspx</comments>
      <guid isPermaLink="true">http://www.prlsoftware.com/progress-bar.aspx</guid>
      <pubDate>Mon, 31 Aug 2009 03:41:33 GMT</pubDate>
    </item>
    <item>
      <title>Prime Numbers</title>
      <description><![CDATA[<div>
<div>Recently there was a competition on creating prime nubmers in SQL Server.&#160; Although there were faster versions, I believe that this is the fastest version that will scale.</div>
<div>&#160;</div>
<div><font size="2" face="Arial"><span lang="EN-GB" style="font-size: 10pt; font-family: Arial;">create table Primes (p integer NOT NULL Primary Key)<o:p></o:p></span></font></div>
<div><font size="2" face="Arial"><span lang="EN-GB" style="font-size: 10pt; font-family: Arial;">go<o:p></o:p></span></font></div>
<div><font size="2" face="Arial"><span lang="EN-GB" style="font-size: 10pt; font-family: Arial;">set nocount on<o:p></o:p></span></font></div>
<div><font size="2" face="Arial"><span lang="EN-GB" style="font-size: 10pt; font-family: Arial;">-- Seed the pot with the first two Primes<o:p></o:p></span></font></div>
<div><font size="2" face="Arial"><span lang="EN-GB" style="font-size: 10pt; font-family: Arial;">declare @testval integer, @First as int, @Second as int, @SQRT as int<o:p></o:p></span></font></div>
<div><font size="2" face="Arial"><span lang="EN-GB" style="font-size: 10pt; font-family: Arial;">-- Start testing at n=6, stepping 6 per test.<o:p></o:p></span></font></div>
<div><font size="2" face="Arial"><span lang="EN-GB" style="font-size: 10pt; font-family: Arial;">set @testval = 6<o:p></o:p></span></font></div>
<div><font size="2" face="Arial"><span lang="EN-GB" style="font-size: 10pt; font-family: Arial;">BEGIN TRANSACTION<o:p></o:p></span></font></div>
<div><font size="2" face="Arial"><span lang="EN-GB" style="font-size: 10pt; font-family: Arial;">while @testval &lt; 5000000<o:p></o:p></span></font></div>
<div><font size="2" face="Arial"><span lang="EN-GB" style="font-size: 10pt; font-family: Arial;">begin<o:p></o:p></span></font></div>
<div><font size="2" face="Arial"><span lang="EN-GB" style="font-size: 10pt; font-family: Arial;">set @First = @testval -1<o:p></o:p></span></font></div>
<div><font size="2" face="Arial"><span lang="EN-GB" style="font-size: 10pt; font-family: Arial;">set&#160; @Second = @testval +1<o:p></o:p></span></font></div>
<div><font size="2" face="Arial"><span lang="EN-GB" style="font-size: 10pt; font-family: Arial;">set&#160; @SQRT = SQRT(@testval +1)<o:p></o:p></span></font></div>
<div><font size="2" face="Arial"><span lang="EN-GB" style="font-size: 10pt; font-family: Arial;">if&#160; not exists (select @First from Primes where (@First) % p = 0 and p &lt;= @SQRT)<o:p></o:p></span></font></div>
<div><font size="2" face="Arial"><span lang="EN-GB" style="font-size: 10pt; font-family: Arial;">insert Primes select @First<o:p></o:p></span></font></div>
<div><font size="2" face="Arial"><span lang="EN-GB" style="font-size: 10pt; font-family: Arial;">if&#160; not exists (select @Second from Primes where (@Second) % p = 0 and p &lt;= @SQRT)<o:p></o:p></span></font></div>
<div><font size="2" face="Arial"><span lang="EN-GB" style="font-size: 10pt; font-family: Arial;">insert Primes select @Second<o:p></o:p></span></font></div>
<div><font size="2" face="Arial"><span lang="EN-GB" style="font-size: 10pt; font-family: Arial;">set @testval = @testval + 6<o:p></o:p></span></font></div>
<div><font size="2" face="Arial"><span lang="EN-GB" style="font-size: 10pt; font-family: Arial;">end<o:p></o:p></span></font></div>
<div><font size="2" face="Arial"><span lang="EN-GB" style="font-size: 10pt; font-family: Arial;">INSERT Primes select 2<o:p></o:p></span></font></div>
<div><font size="2" face="Arial"><span lang="EN-GB" style="font-size: 10pt; font-family: Arial;">INSERT Primes select 3<o:p></o:p></span></font></div>
<div><font size="2" face="Arial"><span lang="EN-GB" style="font-size: 10pt; font-family: Arial;">COMMIT</span></font></div>
</div>
<div>&#160;</div>
<div>&#160;</div><br /><a href='http://www.prlsoftware.com/prime-numbers.aspx'>Philip Leitch</a>&nbsp;&nbsp;<a href='http://www.prlsoftware.com/prime-numbers.aspx'>...</a>]]></description>
      <link>http://www.prlsoftware.com/prime-numbers.aspx</link>
      <author>Philip Leitch</author>
      <comments>http://www.prlsoftware.com/prime-numbers.aspx</comments>
      <guid isPermaLink="true">http://www.prlsoftware.com/prime-numbers.aspx</guid>
      <pubDate>Fri, 07 Aug 2009 05:41:27 GMT</pubDate>
    </item>
    <item>
      <title>SP_Kill_Users</title>
      <description><![CDATA[<div>&#160;</div>
<div>use master</div>
<div>go</div>
<div>create&#160; Proc SP_Kill_Users <br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; @Database_Name as varchar(1000) = null<br />
as <br />
/*</div>
<div>www.prlsoftware.com<br />
Author: Philip Leitch<br />
Date: 2002<br />
Purpose: Kills all processes currently using a specified database, or all databases if no database is supplied.<br />
Copyright: Philip Leitch 2002<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.</div>
<div>Notes:<br />
Be very careful with this procedure Killing processes without first checking what they are is dangerous.</div>
<div>This procedure is very useful when a restore is required urgently.<br />
&#160;</div>
<div>&#160;</div>
<div>Please note this procedure is intended to be placed into the master database, which combined with the SP_ prefix provides global calling scope.</div>
<div>*/</div>
<div>Declare [Kill_All] cursor FORWARD_ONLY <br />
for <br />
select databases.name, processes.spid from master.dbo.sysprocesses as processes<br />
join master.dbo.sysdatabases as databases on<br />
processes.dbid = databases.dbid<br />
where hostname &lt;&gt; @@servername and hostname &lt;&gt;''<br />
and (databases.name = @database_name or @database_name is null)<br />
and processes.spid &lt;&gt; @@spid<br />
group by databases.name, processes.spid <br />
order by databases.name<br />
&#160;<br />
open [Kill_All] <br />
&#160;<br />
declare @KillSQL as varchar(1000)<br />
declare @DatabaseName as varchar(100)<br />
declare @SPID as varchar(10)<br />
&#160;<br />
fetch [Kill_All] into @DatabaseName,&#160; @SPID <br />
while @@fetch_status = 0<br />
begin<br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; set @KillSQL = 'Kill ' + cast(@spid as varchar(100))<br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; exec (@KillSQL)<br />
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; fetch [Kill_All] into @DatabaseName,&#160; @SPID <br />
end</div>
<div>deallocate [Kill_All] <br />
go</div><br /><a href='http://www.prlsoftware.com/sp_kill_users.aspx'>Philip Leitch</a>&nbsp;&nbsp;<a href='http://www.prlsoftware.com/sp_kill_users.aspx'>...</a>]]></description>
      <link>http://www.prlsoftware.com/sp_kill_users.aspx</link>
      <author>Philip Leitch</author>
      <comments>http://www.prlsoftware.com/sp_kill_users.aspx</comments>
      <guid isPermaLink="true">http://www.prlsoftware.com/sp_kill_users.aspx</guid>
      <pubDate>Sat, 13 Jun 2009 10:22:49 GMT</pubDate>
    </item>
    <item>
      <title>Time To Text</title>
      <description><![CDATA[<div>create FUNCTION Time_To_Text<br />
(@Time as datetime)<br />
returns Varchar(255)<br />
begin<br />
/*</div>
<div>www.prlsoftware.com<br />
Author: Philip Leitch<br />
Date: 2005<br />
Purpose: Displays just the time component of a datetime/smalldatetime value.<br />
Copyright: Philip Leitch 2005<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.</div>
<div>Notes:<br />
Currently returns the time in 24 hour time.<br />
*/<br />
DECLARE @RETURN AS VARCHAR(255)<br />
SET @RETURN = right('0' + cast(datepart(HOUR, @Time) as varchar),2) + ':' +<br />
right('0' + cast(datepart(minute, @Time) as varchar),2) + ':' + <br />
right('0' + cast(datepart(SECOND, @Time) as varchar),2) + '.' + <br />
right('00' + cast(datepart(MILLISECOND, @Time) as varchar),3)</div>
<div>return @RETURN</div>
<div>end<br />
go<br />
-- =============================================<br />
-- Example to execute function<br />
-- =============================================<br />
--One of these values will be in 24 hour format.<br />
select dbo.Time_To_Text(getdate()), dbo.Time_To_Text(getdate()+.5)</div><br /><a href='http://www.prlsoftware.com/time-to-text.aspx'>Philip Leitch</a>&nbsp;&nbsp;<a href='http://www.prlsoftware.com/time-to-text.aspx'>...</a>]]></description>
      <link>http://www.prlsoftware.com/time-to-text.aspx</link>
      <author>Philip Leitch</author>
      <comments>http://www.prlsoftware.com/time-to-text.aspx</comments>
      <guid isPermaLink="true">http://www.prlsoftware.com/time-to-text.aspx</guid>
      <pubDate>Thu, 11 Jun 2009 23:30:03 GMT</pubDate>
    </item>
    <item>
      <title>Tax Component</title>
      <description><![CDATA[<div>&#160;</div>
<div>IF EXISTS (SELECT 'x' FROM&#160;&#160; sysobjects WHERE&#160; name = 'TAX_Component')<br />
&#160;DROP FUNCTION TAX_Component<br />
GO<br />
Create FUNCTION dbo.TAX_Component<br />
&#160;(@Charge as Money,<br />
&#160; @TAX_Rate as numeric(12,4))<br />
RETURNS&#160; Money<br />
AS<br />
BEGIN<br />
/*<br />
Author: Philip Leitch<br />
Date: 2001<br />
Purpose: Determines the tax component of an already taxed value.<br />
Copyright: Philip Leitch 2001<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.</div>
<div>Notes:<br />
This function was created for Goods and Services (GST) tax, a type of Value Added Tax (VAT).&#160; The Tax portion<br />
of a value is frequently required When only the total net (inclusive of tax) value is provided.&#160; This function<br />
will provide the tax component, given any charge and tax rate.</div>
<div>*/<br />
&#160; RETURN case when @TAX_Rate = 0 then 0 else cast(@Charge * 1/((<a href="mailto:1/@TAX_Rate)+1">1/@TAX_Rate)+1</a>) as numeric(12,2)) end</div>
<div>END<br />
GO</div>
<div>-- =============================================<br />
-- Example to execute function<br />
-- =============================================<br />
declare @Amount as money<br />
declare @TAX_Rate as float<br />
set @amount = $100<br />
set @TAX_Rate = 0.10 --Equates to TAX<br />
select @Amount as Amount, @TAX_Rate as rate, dbo.TAX_Component(@Amount, @TAX_Rate) as TAX_Component, @Amount - dbo.TAX_Component(@Amount, @TAX_Rate) as Total_Exclusive_Charge<br />
select 9.0900+&#160;90.9100</div><br /><a href='http://www.prlsoftware.com/tax-component.aspx'>Philip Leitch</a>&nbsp;&nbsp;<a href='http://www.prlsoftware.com/tax-component.aspx'>...</a>]]></description>
      <link>http://www.prlsoftware.com/tax-component.aspx</link>
      <author>Philip Leitch</author>
      <comments>http://www.prlsoftware.com/tax-component.aspx</comments>
      <guid isPermaLink="true">http://www.prlsoftware.com/tax-component.aspx</guid>
      <pubDate>Sun, 07 Jun 2009 11:59:48 GMT</pubDate>
    </item>
    <item>
      <title>OverlapDates</title>
      <description><![CDATA[<div>IF EXISTS (SELECT 'x' FROM&#160;&#160; sysobjects WHERE&#160; name = 'OverlapDates')<br />
&#160;DROP FUNCTION OverlapDates<br />
GO<br />
CREATE FUNCTION OverlapDates <br />
&#160;(@Startdate as datetime,<br />
&#160;@EndDate as datetime = null,<br />
&#160;@Startdate2 as datetime,<br />
&#160;@EndDate2 as datetime = null)<br />
RETURNS bit<br />
AS<br />
BEGIN<br />
&#160;Declare @ReturnValue as bit<br />
/*<br />
Author: Philip Leitch<br />
Date: 2005<br />
Purpose: Determines if one pair of dates overlaps with another pair of dates.<br />
Copyright: Philip Leitch 2005<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.</div>
<div>&#160;</div>
<div>Liability: The developer assumes all liability when using this code.</div>
<div>Notes:<br />
This function assumes that the start is not null in either pair but the end date can be null of either pair.<br />
The start and end date are common for any duration based activity, such as pricing or activities.</div>
<div>The ranges overlap if the first start is before the second start,<br />
and the first end is after the second start.<br />
OR <br />
The ranges overlap if the first start is betwene the second start and the second end.<br />
*/</div>
<div><br />
&#160;if (@Startdate &lt;= @Startdate2 and (@Startdate2 &lt;= @EndDate or @EndDate is null))<br />
&#160;&#160;or<br />
&#160;&#160;(@Startdate &gt; @Startdate2 and (@Startdate &lt;= @EndDate2 or @EndDate2 is null))<br />
&#160;&#160;set @ReturnValue = 1<br />
&#160;else<br />
&#160;&#160;set @ReturnValue = 0<br />
&#160;return @ReturnValue<br />
End<br />
GO</div>
<div>-- =============================================<br />
-- Example to execute function<br />
-- =============================================<br />
SELECT dbo.OverlapDates('1/1/2003', getdate(), '1/1/05','')</div>
<div>&#160;</div><br /><a href='http://www.prlsoftware.com/overlapdates.aspx'>Philip Leitch</a>&nbsp;&nbsp;<a href='http://www.prlsoftware.com/overlapdates.aspx'>...</a>]]></description>
      <link>http://www.prlsoftware.com/overlapdates.aspx</link>
      <author>Philip Leitch</author>
      <comments>http://www.prlsoftware.com/overlapdates.aspx</comments>
      <guid isPermaLink="true">http://www.prlsoftware.com/overlapdates.aspx</guid>
      <pubDate>Sun, 07 Jun 2009 09:46:18 GMT</pubDate>
    </item>
  </channel>
</rss>
