Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

xero http://www.xerotopia.net/

« Newer Snippets
Older Snippets »
Showing 1-8 of 8 total  RSS 

Pull Date-Effective Records that Intersect A Given Range

Basics of how to pull records that intersect a date range from a temporal (aka date-effective) database.

-- @rangeStart is the start of the date range of interest
-- @rangeEnd is the end of the date range of interest
-- If you use null to signify infinity, then use the commented line instead of the one above it.
select *
from DateEffectiveTable
where StartDate < @rangeEnd
  and EndDate >= @rangeStart
  -- and coalesce(endDate, '9999-12-31') >= @rangeStart

Calculate Last Day of Last Month

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.


DateAdd("d", -1.0 * DatePart("d", Today), Today)

Calculate First Day of Current Month

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.

DateAdd("D", -1.0 * DatePart("D", Today) + 1, Today)

Calculate First Day of Last Month

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.

DateAdd("D", -1.0 * DatePart("D", Today) + 1, DateAdd("m", -1, Today))

Read Multidimensional C char Array of Unknown Length

Reading a multidimensional C char array (aka C String array) containing char 0 and terminated with literal zero without knowing length.

char *a[] = { "11", "23", "", "44", "11", "", "16", "36", "", "51", "71", "", "46", "26", "", "14", "68", 0};
int sentinel = 0;
int i = 0;
while ( *(a + i++) != sentinel ) { /* counting away */ }
int aLength = i; // 18, length
int aUbound = i - 1; // 17, index of last element
int aMaxIdx = i - 2; // 16, index of last element where a[i] doesn't cause seg fault
// You now have the lengths so read like normal

PRNG In SQL Select

Simple example of a PRNG (pseudo-random number generator) written into a SQL statement

Example is in T-SQL, but it ports well

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.

-- Setup some vars we'll need
DECLARE @prng TABLE (seed BIGINT, rnum nchar(10))
DECLARE @seeds TABLE (seed BIGINT)
DECLARE @seed BIGINT
DECLARE @C1 BIGINT, @C2 BIGINT, @C3 BIGINT
SET @seed = 0
SET @C1 = 1664525
SET @C2 = 4294967296
SET @C3 = 1013904223

-- Create a seed table so we can have some data to use
WHILE @seed < 10
BEGIN
    INSERT INTO @seeds (seed) VALUES (@seed)
    SET @seed = @seed + 1
END

-- Create our PRNG (inserts into table for illustrative purposes)
-- prng(seed) ::= ((((C1 * seed) % C2) + C3) % C2) / C2
-- Then convert prng(seed) into a string
-- of 10 chars, 8 of which are decimal places
INSERT INTO @prng
SELECT
    seed,
    REPLACE(
        STR(
            ( CAST((((@C1*seed)%@C2)+@C3)%@C2 AS FLOAT) )
            / ( CAST(@C2 AS FLOAT)),
            10, 8
        ),
        ' ', '0') AS rnum
FROM @seeds

-- Let's take a look at what we created
SELECT * FROM @prng

Trim Template for XSLT

Common Trim function for XSLT (as a template)

<xsl:template name="left-trim">
  <xsl:param name="s" />
  <xsl:choose>
    <xsl:when test="substring($s, 1, 1) = ''">
      <xsl:value-of select="$s"/>
    </xsl:when>
    <xsl:when test="normalize-space(substring($s, 1, 1)) = ''">
      <xsl:call-template name="left-trim">
        <xsl:with-param name="s" select="substring($s, 2)" />
      </xsl:call-template>
    </xsl:when>
    <xsl:otherwise>
      <xsl:value-of select="$s" />
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

<xsl:template name="right-trim">
  <xsl:param name="s" />
  <xsl:choose>
    <xsl:when test="substring($s, 1, 1) = ''">
      <xsl:value-of select="$s"/>
    </xsl:when>
    <xsl:when test="normalize-space(substring($s, string-length($s))) = ''">
      <xsl:call-template name="right-trim">
        <xsl:with-param name="s" select="substring($s, 1, string-length($s) - 1)" />
      </xsl:call-template>
    </xsl:when>
    <xsl:otherwise>
      <xsl:value-of select="$s" />
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

<xsl:template name="trim">
  <xsl:param name="s" />
  <xsl:call-template name="right-trim">
    <xsl:with-param name="s">
      <xsl:call-template name="left-trim">
        <xsl:with-param name="s" select="$s" />
      </xsl:call-template>
    </xsl:with-param>
  </xsl:call-template>
</xsl:template>

Normalize Space and Keep Line Breaks in XSTL

XSLT function pattern to normalize space but keep line breaks, without using a template.

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.

For reference, pretend "FieldValue/." is the path to a <FieldValue>foo</FieldValue> element.

translate(
 normalize-space(
  translate(FieldValue/., '&#xA;&#xD;', '&#xA0;&#xAD;')
 ), '&#xA0;&#xAD;­', '&#xA;&#xD;'
)

« Newer Snippets
Older Snippets »
Showing 1-8 of 8 total  RSS