Peter Larsson Blog

Patron Saint of Lost Yaks

How to calculate the number of weekdays for any given period

Here http://weblogs.sqlteam.com/peterl/archive/2009/07/12/How-to-calculate-the-number-of-weekdays-in-a-month.aspx is a specialized version for a month, and here http://weblogs.sqlteam.com/peterl/archive/2009/07/12/How-to-calculate-number-of-weekdays-in-a-year.aspx is a specialized version for a year. This code below calculates the number of weekdays for any given period. Read more →

How to calculate the number of weekdays in a month

Here http://weblogs.sqlteam.com/peterl/archive/2009/07/12/How-to-calculate-the-number-of-weekdays-for-any-given.aspx you can find a specialized version that calculates number of weekdays for any given period. CREATEFUNCTION dbo.fnMonthWeekDays ( @Year SMALLINT, @Month TINYINT ) RETURNSTINYINT AS BEGIN RETURN ( SELECT 20 + SUM(1 - DATEDIFF(DAY, '17530101', dt) % 7 / 5) FROM ( SELECT DATEADD(MONTH, 12 * @Year - 22800 + @Month, -1) AS dt WHERE ISDATE(10000 * @Year + 100 * @Month + 31) = 1 UNION ALL SELECT DATEADD(MONTH, 12 * @Year - 22800 + @Month, -2) WHERE ISDATE(10000 * @Year + 100 * @Month + 30) = 1 UNION ALL SELECT CASE ISDATE(10000 * @Year + 100 * @Month + 29) WHEN 1 THEN DATEADD(MONTH, 12 * @Year - 22800 + @Month, -3) ELSE '18991231' END ) AS d WHERE @Year BETWEEN 1753 AND 9999 AND@Month BETWEEN 1 AND 12 ) END Legacy Comments Sowmya 2009-10-22 re: How to calculate the number of weekdays in a month Can u please explain the logic ? Read more →

How to calculate number of weekdays in a year

This function calculates the number of weeksdays in a year, and has error-checking for invalid years. It is also language independant. Here http://weblogs.sqlteam.com/peterl/archive/2009/07/12/How-to-calculate-the-number-of-weekdays-for-any-given.aspx you can find a specialized version that calculates 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 http://weblogs.sqlteam.com/peterl/archive/2009/06/17/How-to-get-the-Nth-weekday-of-a-month.aspx 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 →

How to check Danish personal number

CREATEFUNCTION dbo.fnCheckDanSSN ( @SSN CHAR(10) ) RETURNSBIT AS BEGIN IF @SSN NOT LIKE '[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]' RETURN 0 DECLARE @x CHAR(6) SET @x = SUBSTRING(@SSN, 5, 2) + SUBSTRING(@SSN, 3, 2) + SUBSTRING(@SSN, 1, 2) IF SUBSTRING(@SSN, 7, 1) IN('0', '1', '2', '3') AND ISDATE('19' + @x) = 0 RETURN 0 IF SUBSTRING(@SSN, 7, 1) IN('4', '9') SUBSTRING(@SSN, 5, 2) BETWEEN '00' AND '36' AND ISDATE('20' + @x) = 0 RETURN 0 IF SUBSTRING(@SSN, 7, 1) IN('4', '9') SUBSTRING(@SSN, 5, 2) BETWEEN '37' AND '99' AND ISDATE('19' + @x) = 0 RETURN 0 IF SUBSTRING(@SSN, 7, 1) IN('5', '6', '7', '8') SUBSTRING(@SSN, 5, 2) BETWEEN '00' AND '57' AND ISDATE('20' + @x) = 0 RETURN 0 IF SUBSTRING(@SSN, 7, 1) IN('5', '6', '7', '8') SUBSTRING(@SSN, 5, 2) BETWEEN '58' AND '99' AND ISDATE('18' + @x) = 0 RETURN 0 DECLARE @Digits INT SET @Digits = 4 * SUBSTRING(@SSN, 1, 1) + 3 * SUBSTRING(@SSN, 2, 2) + 2 * SUBSTRING(@SSN, 3, 2) + 7 * SUBSTRING(@SSN, 4, 2) + 6 * SUBSTRING(@SSN, 5, 2) + 5 * SUBSTRING(@SSN, 6, 2) + 4 * SUBSTRING(@SSN, 7, 2) + 3 * SUBSTRING(@SSN, 8, 2) + 2 * SUBSTRING(@SSN, 9, 2) + 1 * SUBSTRING(@SSN, 10, 2) RETURN1 - SIGN(@Digits % 11) END Legacy Comments Saggi Neumann 2009-05-30 re: How to check Danish personal number This is how we validate ID numbers in Israel. Read more →

How to calculate Finnish personal number

CREATEFUNCTION dbo.fnCalculateFinSSN ( @SSN CHAR(10) ) RETURNSCHAR(11) AS BEGIN IF LOWER(@SSN) NOT LIKE '[0-9][0-9][0-9][0-9][0-9][0-9][-+a][0-9][0-9][0-9]' RETURN NULL IF SUBSTRING(@SSN, 7, 1) IN('-', '+') AND ISDATE('19' + SUBSTRING(@SSN, 1, 6)) = 0 RETURN NULL IF LOWER(SUBSTRING(@SSN, 7, 1)) = 'a' AND ISDATE('20' + SUBSTRING(@SSN, 1, 6)) = 0 RETURN 0 DECLARE @Digits INT SET @Digits = CAST(SUBSTRING(@SSN, 1, 6) + SUBSTRING(@SSN, 8, 3) AS INT) % 31 RETURN @@SSN + SUBSTRING('0123456789abcdefghijklmnopqrstuvwxy', @Digits + 1, 1) END Read more →

How to check Finnish personal number

CREATEFUNCTION dbo.fnCheckFinSSN ( @SSN CHAR(11) ) RETURNSBIT AS BEGIN IF LOWER(@SSN) NOT LIKE '[0-9][0-9][0-9][0-9][0-9][0-9][-+a][0-9][0-9][0-9][0-9a-y]' RETURN 0 IF SUBSTRING(@SSN, 7, 1) IN('-', '+') AND ISDATE('19' + SUBSTRING(@SSN, 1, 6)) = 0 RETURN 0 IF LOWER(SUBSTRING(@SSN, 7, 1)) = 'a' AND ISDATE('20' + SUBSTRING(@SSN, 1, 6)) = 0 RETURN 0 DECLARE @Digits INT SET @Digits = CAST(SUBSTRING(@SSN, 1, 6) + SUBSTRING(@SSN, 8, 3) AS INT) % 31 IF SUBSTRING('0123456789abcdefghijklmnopqrstuvwxy', @Digits + 1, 1) <> LOWER(RIGHT(@SSN, 1)) SET@Digits = -1 RETURN @Digits + 1 END Read more →

Extended ISO week function

CREATEFUNCTION dbo.fnISOWEEK ( @theDate DATETIME ) RETURNSTINYINT AS BEGIN RETURN ( SELECT CASE WHEN @theDate >= '99990104' THEN (DATEPART(DAYOFYEAR, @theDate) - 4) / 7 WHEN @theDate >= '99990101' THEN 52 WHEN NextYear <= @theDate THEN 0 WHEN CurrentYear <= @theDate THEN DATEDIFF(DAY, CurrentYear, @theDate) / 7 ELSE DATEDIFF(DAY, PreviousYear, @theDate) / 7 END + 1 FROM ( SELECT DATEADD(DAY,(DATEDIFF(DAY, '17530101', DATEADD(YEAR, -1, Jan4)) / 7) * 7, '17530101') AS PreviousYear, DATEADD(DAY,(DATEDIFF(DAY, '17530101', Jan4) / 7) * 7, '17530101') AS CurrentYear, DATEADD(DAY,(DATEDIFF(DAY, '17530101', DATEADD(YEAR, 1, Jan4)) / 7) * 7, '17530101') AS NextYear FROM ( SELECT DATEADD(YEAR, DATEDIFF(YEAR, 0, @theDate), 3) AS Jan4 )AS x ) AS d ) END Read more →

Greatest Common Divisor function

This approach is not dependent on the 32-level recursion as most other algorithms are for this problem. CREATEFUNCTION dbo.fnGCD ( @a INT, @b INT ) RETURNSINT AS BEGIN DECLARE @c INT IF @a IS NULL OR @b IS NULL OR (@a = 0 AND @b = 0) RETURN NULL IF @a = 0 OR @b = 0 RETURN ABS(@a) + ABS(@b) IF ABS(@a) < ABS(@b) SELECT @c = ABS(@a), @a = ABS(@b), @b = @c ELSE SELECT @a = ABS(@a), @b = ABS(@b) SET @c = @a % @b WHILE @c > 0 SELECT @a = @b, @b = @c, @c = @a % @b RETURN @b END Legacy Comments RBarryYoung 2009-04-15 re: Greatest Common Divisor function That's the iterative version of the chinese remainder algorithim, isn't it Peso? Read more →