<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: Jokeyxero's Code Snippets</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Sat, 26 Jul 2008 15:16:41 GMT</pubDate>
    <description>DZone Snippets: Jokeyxero's Code Snippets</description>
    <item>
      <title>Pull Date-Effective Records that Overlap Each Other</title>
      <link>http://snippets.dzone.com/posts/show/5590</link>
      <description>Basic framework and examples of how to pull records that overlap each other from a temporal (aka date-effective) database.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;-- PK is a placeholder for the Primary Key column(s)&lt;br /&gt;-- SoftPK is a placeholder for columns that need to match for overlap, but are not the PK&lt;br /&gt;-- Sometimes SoftPK is the PK, in which case you remove the &lt;&gt; PK line and match the SoftPK.&lt;br /&gt;-- The idea is to check for overlap on all rows except for yourself, since that always overlaps.&lt;br /&gt;--   This snippet assumes the EndDate column has a NULL to represent infinity.&lt;br /&gt;--   There are alternate syntaxes for this (compare or EndDate is null) but I've found&lt;br /&gt;--   the Coalesce to provide the best query plan usually. The constant itself is debatable and&lt;br /&gt;--   implementation dependent.&lt;br /&gt;select outer.*&lt;br /&gt;from DateEffectiveTable as outer&lt;br /&gt;where exists (&lt;br /&gt;  select 1&lt;br /&gt;  from DateEffectiveTable as inner&lt;br /&gt;  where inner.PK &lt;&gt; outer.PK&lt;br /&gt;    -- and inner.SoftPK = outer.SoftPK&lt;br /&gt;    and inner.StartDate &lt;= coalesce(outer.EndDate, '9999-12-31')&lt;br /&gt;    and coalesce(inner.EndDate, '9999-12-31') &gt; outer.StartDate&lt;br /&gt;)&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;-- Example Usage&lt;br /&gt;-- Assume we have a table containing the address history for a person&lt;br /&gt;-- A person may have multiple addresses at once (own multiple houses).&lt;br /&gt;-- A person may move away then back (same address, different time ranges).&lt;br /&gt;-- ie, multiple simultaneous unique records are possible, but not&lt;br /&gt;-- multiple simultaneous non-unique records (no self-overlap).&lt;br /&gt;-- Yes, it is usually an odd design to have an AddressID, but go with it.&lt;br /&gt;select outer.*&lt;br /&gt;from AddressHistory as outer&lt;br /&gt;where exists (&lt;br /&gt;  select 1&lt;br /&gt;  from AddressHistory as inner&lt;br /&gt;  where outer.AddressID &lt;&gt; inner.AddressID&lt;br /&gt;    and outer.PersonID = inner.PersonID&lt;br /&gt;    and outer.Street = inner.Street&lt;br /&gt;    and outer.Zip = inner.Zip&lt;br /&gt;    and inner.StartDate &lt;= coalesce(outer.EndDate, '9999-12-31')&lt;br /&gt;    and coalesce(inner.EndDate, '9999-12-31') &gt; outer.StartDate&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;-- Same example, but assume PK is a more "appropriate" one of&lt;br /&gt;-- { PersonID, Street, Zip, StartDate }&lt;br /&gt;select outer.*&lt;br /&gt;from AddressHistory as outer&lt;br /&gt;where exists (&lt;br /&gt;  select 1&lt;br /&gt;  from AddressHistory as inner&lt;br /&gt;  where &lt;br /&gt;    not (&lt;br /&gt;     outer.PersonID = inner.PersonID&lt;br /&gt;     and outer.Street = inner.Street&lt;br /&gt;     and outer.Zip = inner.Zip&lt;br /&gt;     and outer.StartDate = inner.StartDate&lt;br /&gt;    )&lt;br /&gt;    and outer.PersonID = inner.PersonID&lt;br /&gt;    and outer.Street = inner.Street&lt;br /&gt;    and outer.Zip = inner.Zip&lt;br /&gt;    and inner.StartDate &lt;= coalesce(outer.EndDate, '9999-12-31')&lt;br /&gt;    and coalesce(inner.EndDate, '9999-12-31') &gt; outer.StartDate&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Wed, 04 Jun 2008 19:54:15 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5590</guid>
      <author>jokeyxero (xero)</author>
    </item>
    <item>
      <title>Pull Date-Effective Records that Intersect A Given Range</title>
      <link>http://snippets.dzone.com/posts/show/5503</link>
      <description>Basics of how to pull records that intersect a date range from a temporal (aka date-effective) database.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;-- @rangeStart is the start of the date range of interest&lt;br /&gt;-- @rangeEnd is the end of the date range of interest&lt;br /&gt;-- If you use null to signify infinity, then use the commented line instead of the one above it.&lt;br /&gt;select *&lt;br /&gt;from DateEffectiveTable&lt;br /&gt;where StartDate &lt; @rangeEnd&lt;br /&gt;  and EndDate &gt;= @rangeStart&lt;br /&gt;  -- and coalesce(endDate, '9999-12-31') &gt;= @rangeStart&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 16 May 2008 14:50:51 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5503</guid>
      <author>jokeyxero (xero)</author>
    </item>
    <item>
      <title>Calculate Last Day of Last Month</title>
      <link>http://snippets.dzone.com/posts/show/5076</link>
      <description>VB/VBA/VB.NET one-liner to calculate the end of last month. Useful for SSRS/RDL Expressions and Excel/Office Formulas. Note that it does not use string parsing, which can cause localization problems.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;DateAdd("d", -1.0 * DatePart("d", Today), Today)&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 01 Feb 2008 22:51:12 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5076</guid>
      <author>jokeyxero (xero)</author>
    </item>
    <item>
      <title>Calculate First Day of Current Month</title>
      <link>http://snippets.dzone.com/posts/show/5075</link>
      <description>VB/VBA/VB.NET one-liner to calculate the start of the current month. Useful for SSRS/RDL Expressions and Excel/Office Formulas. Note that it does not use string parsing, which can cause localization problems.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;DateAdd("D", -1.0 * DatePart("D", Today) + 1, Today)&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 01 Feb 2008 22:48:45 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5075</guid>
      <author>jokeyxero (xero)</author>
    </item>
    <item>
      <title>Calculate First Day of Last Month</title>
      <link>http://snippets.dzone.com/posts/show/5074</link>
      <description>VB/VBA/VB.NET one-liner to calculate the start of the previous month. Useful for SSRS/RDL Expressions and Excel/Office Formulas. Note that it does not use string parsing, which can cause localization problems.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;DateAdd("D", -1.0 * DatePart("D", Today) + 1, DateAdd("m", -1, Today))&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 01 Feb 2008 22:47:11 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5074</guid>
      <author>jokeyxero (xero)</author>
    </item>
    <item>
      <title>Read Multidimensional C char Array of Unknown Length</title>
      <link>http://snippets.dzone.com/posts/show/4034</link>
      <description>Reading a multidimensional C char array (aka C String array) containing char 0 and terminated with literal zero without knowing length.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;char *a[] = { "11", "23", "", "44", "11", "", "16", "36", "", "51", "71", "", "46", "26", "", "14", "68", 0};&lt;br /&gt;int sentinel = 0;&lt;br /&gt;int i = 0;&lt;br /&gt;while ( *(a + i++) != sentinel ) { /* counting away */ }&lt;br /&gt;int aLength = i; // 18, length&lt;br /&gt;int aUbound = i - 1; // 17, index of last element&lt;br /&gt;int aMaxIdx = i - 2; // 16, index of last element where a[i] doesn't cause seg fault&lt;br /&gt;// You now have the lengths so read like normal&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 17 May 2007 00:56:20 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4034</guid>
      <author>jokeyxero (xero)</author>
    </item>
    <item>
      <title>PRNG In SQL Select </title>
      <link>http://snippets.dzone.com/posts/show/4033</link>
      <description>Simple example of a PRNG (pseudo-random number generator) written into a SQL statement&lt;br /&gt;&lt;br /&gt;Example is in T-SQL, but it ports well&lt;br /&gt;&lt;br /&gt;Actual application should use either a better random algorithm, or the output be used with randomized seeds. This is definitely not cryptographically secure. It's very handy if you need a simple random number with your recordset though.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;-- Setup some vars we'll need&lt;br /&gt;DECLARE @prng TABLE (seed BIGINT, rnum nchar(10))&lt;br /&gt;DECLARE @seeds TABLE (seed BIGINT)&lt;br /&gt;DECLARE @seed BIGINT&lt;br /&gt;DECLARE @C1 BIGINT, @C2 BIGINT, @C3 BIGINT&lt;br /&gt;SET @seed = 0&lt;br /&gt;SET @C1 = 1664525&lt;br /&gt;SET @C2 = 4294967296&lt;br /&gt;SET @C3 = 1013904223&lt;br /&gt;&lt;br /&gt;-- Create a seed table so we can have some data to use&lt;br /&gt;WHILE @seed &lt; 10&lt;br /&gt;BEGIN&lt;br /&gt;    INSERT INTO @seeds (seed) VALUES (@seed)&lt;br /&gt;    SET @seed = @seed + 1&lt;br /&gt;END&lt;br /&gt;&lt;br /&gt;-- Create our PRNG (inserts into table for illustrative purposes)&lt;br /&gt;-- prng(seed) ::= ((((C1 * seed) % C2) + C3) % C2) / C2&lt;br /&gt;-- Then convert prng(seed) into a string&lt;br /&gt;-- of 10 chars, 8 of which are decimal places&lt;br /&gt;INSERT INTO @prng&lt;br /&gt;SELECT&lt;br /&gt;    seed,&lt;br /&gt;    REPLACE(&lt;br /&gt;        STR(&lt;br /&gt;            ( CAST((((@C1*seed)%@C2)+@C3)%@C2 AS FLOAT) )&lt;br /&gt;            / ( CAST(@C2 AS FLOAT)),&lt;br /&gt;            10, 8&lt;br /&gt;        ),&lt;br /&gt;        ' ', '0') AS rnum&lt;br /&gt;FROM @seeds&lt;br /&gt;&lt;br /&gt;-- Let's take a look at what we created&lt;br /&gt;SELECT * FROM @prng&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 17 May 2007 00:39:32 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4033</guid>
      <author>jokeyxero (xero)</author>
    </item>
    <item>
      <title>Trim Template for XSLT</title>
      <link>http://snippets.dzone.com/posts/show/4032</link>
      <description>Common Trim function for XSLT (as a template)&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;xsl:template name="left-trim"&gt;&lt;br /&gt;  &lt;xsl:param name="s" /&gt;&lt;br /&gt;  &lt;xsl:choose&gt;&lt;br /&gt;    &lt;xsl:when test="substring($s, 1, 1) = ''"&gt;&lt;br /&gt;      &lt;xsl:value-of select="$s"/&gt;&lt;br /&gt;    &lt;/xsl:when&gt;&lt;br /&gt;    &lt;xsl:when test="normalize-space(substring($s, 1, 1)) = ''"&gt;&lt;br /&gt;      &lt;xsl:call-template name="left-trim"&gt;&lt;br /&gt;        &lt;xsl:with-param name="s" select="substring($s, 2)" /&gt;&lt;br /&gt;      &lt;/xsl:call-template&gt;&lt;br /&gt;    &lt;/xsl:when&gt;&lt;br /&gt;    &lt;xsl:otherwise&gt;&lt;br /&gt;      &lt;xsl:value-of select="$s" /&gt;&lt;br /&gt;    &lt;/xsl:otherwise&gt;&lt;br /&gt;  &lt;/xsl:choose&gt;&lt;br /&gt;&lt;/xsl:template&gt;&lt;br /&gt;&lt;br /&gt;&lt;xsl:template name="right-trim"&gt;&lt;br /&gt;  &lt;xsl:param name="s" /&gt;&lt;br /&gt;  &lt;xsl:choose&gt;&lt;br /&gt;    &lt;xsl:when test="substring($s, 1, 1) = ''"&gt;&lt;br /&gt;      &lt;xsl:value-of select="$s"/&gt;&lt;br /&gt;    &lt;/xsl:when&gt;&lt;br /&gt;    &lt;xsl:when test="normalize-space(substring($s, string-length($s))) = ''"&gt;&lt;br /&gt;      &lt;xsl:call-template name="right-trim"&gt;&lt;br /&gt;        &lt;xsl:with-param name="s" select="substring($s, 1, string-length($s) - 1)" /&gt;&lt;br /&gt;      &lt;/xsl:call-template&gt;&lt;br /&gt;    &lt;/xsl:when&gt;&lt;br /&gt;    &lt;xsl:otherwise&gt;&lt;br /&gt;      &lt;xsl:value-of select="$s" /&gt;&lt;br /&gt;    &lt;/xsl:otherwise&gt;&lt;br /&gt;  &lt;/xsl:choose&gt;&lt;br /&gt;&lt;/xsl:template&gt;&lt;br /&gt;&lt;br /&gt;&lt;xsl:template name="trim"&gt;&lt;br /&gt;  &lt;xsl:param name="s" /&gt;&lt;br /&gt;  &lt;xsl:call-template name="right-trim"&gt;&lt;br /&gt;    &lt;xsl:with-param name="s"&gt;&lt;br /&gt;      &lt;xsl:call-template name="left-trim"&gt;&lt;br /&gt;        &lt;xsl:with-param name="s" select="$s" /&gt;&lt;br /&gt;      &lt;/xsl:call-template&gt;&lt;br /&gt;    &lt;/xsl:with-param&gt;&lt;br /&gt;  &lt;/xsl:call-template&gt;&lt;br /&gt;&lt;/xsl:template&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 17 May 2007 00:35:37 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4032</guid>
      <author>jokeyxero (xero)</author>
    </item>
    <item>
      <title>Normalize Space and Keep Line Breaks in XSTL</title>
      <link>http://snippets.dzone.com/posts/show/4031</link>
      <description>XSLT function pattern to normalize space but keep line breaks, without using a template.&lt;br /&gt;&lt;br /&gt;It replaces both line break characters with non-whitespace placeholders, normalizes, then puts the line breaks back in, nothing fancy. Note that it will handle all four types of line breaks (LF, CR, CRLF, LFCR) even though only LF is XML standard. In our case, the placeholders are characters 160 and 173, the hard space and soft hyphen respectively. They're used, but generally only by programmers and Office documents.&lt;br /&gt;&lt;br /&gt;For reference, pretend "FieldValue/." is the path to a &lt;FieldValue&gt;foo&lt;/FieldValue&gt; element.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;translate(&lt;br /&gt; normalize-space(&lt;br /&gt;  translate(FieldValue/., '&amp;#xA;&amp;#xD;', '&amp;#xA0;&amp;#xAD;')&lt;br /&gt; ), '&amp;#xA0;&amp;#xAD;&#173;', '&amp;#xA;&amp;#xD;'&lt;br /&gt;)&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 17 May 2007 00:30:40 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4031</guid>
      <author>jokeyxero (xero)</author>
    </item>
  </channel>
</rss>
