Peter Larsson Blog

Patron Saint of Lost Yaks

How to calculate the number of weekdays for any given period

Here is a specialized version for a month, and here is a specialized version for a year. This code below calculates the number of weekdays for any given period. Read more →

Extended Get Nth Weekday of period

Recently I posted a function which returned the Nth weekday of a month, either from the beginning of month or from the end of month. Function is found here I have fiddled around with it and have now extended the function to find the Nth weekday not only for a month, but also for a quarter or a year. Read more →

Composable DML

With the arrival of SQL Server 2005 the new OUTPUT operator was introduced. The OUTPUT operator works much like a "local trigger" on the current statement. The drawback is that there is no way to filter the returned resultset directly. Read more →

Excel ERF clone for two variables

I previously posted a solution for one variable here and here is a solution for using two variables. CREATEFUNCTION dbo.fnErf ( @z1 FLOAT, @z2 FLOAT, @MaxIterations TINYINT = 10 ) RETURNSFLOAT AS BEGIN IF @z1 IS NULL OR @z2 IS NULL RETURN NULL DECLARE @n TINYINT, @s1 FLOAT, @s2 FLOAT, @p1 FLOAT, @p2 FLOAT, @a1 FLOAT, @a2 FLOAT SELECT @n = 1, @p1 = 1, @p2 = 1, @a1 = @z1, @a2 = @z2, @MaxIterations = COALESCE(ABS(@MaxIterations), 10) WHILE @p1 <> 0. Read more →


Adam Machanic has a nice competition going on here until the 16th of March 2009. Please join in! The prize, for the best submission, is a full MSDN subscription, valued at around $10,000. Read more →