Peter Larsson Blog

Patron Saint of Lost Yaks

Pass the Torch

Since I became a two-time winner of the "Phil Factor SQL Speed Phreak" competition, I was asked to host the next competition. You can find the competition here http://ask.sqlservercentral.com/questions/826/the-fifo-stock-inventory-sql-problem The "Phil Factor SQL Speed Phreak" competitions are about everyday problems and to get people come together and give input on how to solve the current problem in best possible way. Read more →

10 days old

Here is the lates addition to my family. In this picture the boy is 10 days old. The project name is now decided as "Johan Samuel Peter", username Samuel.   Legacy Comments Michelle Ufford 2009-10-19 re: 10 days old Congratulations, Peter! Read more →

New baby DBA

Today our son was born. A healthy baby of 4.4 kg (9.8 lbs) and 53 cm (20.9 inches). Both mother and son are well, and spend the night at the safety on the hospital. Read more →

NOT IN not equal to <>

Hi! I am between session of 24 hour of PASS right now, so I just post this piece of code I helped out with on a forum. The original poster was amazed that Col1 <> 'Some value' didn't return same records as Col1 NOT IN ('Some value') See this example code to understand the implications of fiddling with SET ANSI_NULLS option setting. Read more →

Updated site

Yesterday I updated my site. It was not that appealing. This is how it looked before http://209.85.129.132/search?q=cache:CMr9J28OCwkJ:www.developerworkshop.net/+developerworkshop.net&cd=1&hl=en&ct=clnk&gl=uk and this is how it loooks now http://www.developerworkshop.net I think it's a great improvement, even if there still are some things to fix. Read more →

Recursive Fibonacci number calculation

The answer to your question is "Yes, I am having a slow day today." ;WITHFibonacci(n, f, f1) AS ( SELECT CAST(1 AS BIGINT), CAST(0 AS BIGINT), CAST(1 AS BIGINT) UNION ALL SELECT n + 1, f + f1, f FROM Fibonacci WHERE n < 93 ) SELECTn, f AS Number FROMFibonacci Legacy Comments Liam Caffrey 2009-08-27 re: Recursive Fibonacci number calculation Hi, Read more →

SQLCLR aggregate function

Phew! Now it's finally done. I haven't coded outside SQL Server since 2007 and that was with VB version 6.0. Well, I felt the need to start code again (at least for SQLCLR), since there are many tasks that will be easier to do with a SQLCLR routine. Read more →

CHAR(0) is not that innocent you may think

Some days ago I posted a solution for a simple problem on a forum about to delete multiple spaces in one statement (not using loop). My suggestion was declare@s varchar(100) set@s = 'xxxx yyyyy zzzzzz' SELECTREPLACE(REPLACE(REPLACE(@s, ' ', ' ' + CHAR(2)), CHAR(2) + ' ', ''), CHAR(2), '') I used CHAR(2) because that is not commonly used in normal texts. Read more →

Third running streak

declare@t table (Id int, dt datetime, value int) setdateformat 'dmy' insertinto @t select1, '10/12/2008', 10 union all select1, '11/12/2008', 10 union all select1, '12/12/2008', 10 union all select1, '13/12/2008', 9 union all select1, '14/12/2008', 10 union all select1, '15/12/2008', 10 union all select1, '16/12/2008', 10 union all select1, '17/12/2008', 10 union all select2, '05/03/2008', 8 union all select2, '06/03/2008', 6 union all select2, '07/03/2008', 8 union all select2, '08/03/2008', 8 union all select2, '09/03/2008', 8 union all select2, '20/03/2008', 8 SELECTId, MIN(dt) AS Startdt, MAX(dt) AS Enddt, MIN(value) AS Value FROM( SELECT Id, dt, value, ROW_NUMBER() OVER (PARTITION BY Id, value ORDER BY dt) AS recID, ROW_NUMBER() OVER (ORDER BY value, dt) AS grpID, ROW_NUMBER() OVER (ORDER BY dt) AS colID FROM @t ) AS d GROUPBY Id, grpID - recID, grpID - colID ORDERBY id, MIN(dt) Read more →

Change collation

Yesterday I hade the unfortenate task to change the database collation for a particular database. Not only the default database collation should be changed, but also all columns! After some investigating about how to do this, I noticed that check constraints and foreign key constraints must be removed before changing a collation. Read more →

Complement of ISNUMERIC function

Today I was involved in an interesting topic about how to check if a text string really is integer or not. This is what I finally suggested. CREATEFUNCTION dbo.fnIsINT ( @Data NVARCHAR(11) ) RETURNSINT AS BEGIN RETURN CASE WHEN SUBSTRING(@Data, 2, LEN(@Data)) LIKE '%[^0-9]%' COLLATE LATIN1_GENERAL_BIN THEN NULL WHEN SUBSTRING(@Data, 1, 1) NOT LIKE '[-+0-9]' COLLATE LATIN1_GENERAL_BIN THEN NULL WHEN @Data IN('-', '+') THEN NULL WHEN CAST(@Data AS BIGINT) NOT BETWEEN -2147483648 AND 2147483647 THEN NULL ELSE CAST(@Data AS INT) END END Read more →

Manipulate XML data, continued

In my previous blog post about how to manipulate data in XML columns, http://weblogs.sqlteam.com/peterl/archive/2009/07/03/Manipulate-XML-data-with-non-xml-columns-and-not-using-variable.aspx, I didn't have time to include how to delete elements. Now I have and here you can see how to delete elements from a XML column. Read more →