Krystalware
Products | Purchase | Free Stuff | Forums | Blog | Testimonials | Company | Contact
February 2007 Entries

I use a laptop for a lot of my development, and for some reason, from time to time,  NumLock keeps turning on when I log on. I discovered this behavior is controlled by a registry key, and I'm assuming some program tweaks it on install. Here's the info:

Key: InitialKeyboardIndicators
All Users: HKEY_USERS\.DEFAULT\Control Panel\Keyboard
Current User: HKEY_CURRENT_USER\Control Panel\Keyboard

The possible settings are as follows:

Off: 0
On: 2

The All Users key sets the NumLock state for the login screen.

I've been developing some disposable objects, and was doing research about the proper patterns to follow. This article is a very detailed dissertation on all aspects of IDispose, Finalizers, etc in .NET. Very useful.

http://www.bluebytesoftware.com/blog/PermaLink.aspx?guid=88e62cdf-5919-4ac7-bc33-20c06ae539ae

Since I recently posted a Split UDF, I figured I'd do some research on the best way to do a Join operation. Unfortunately, UDF's can't take table variables as input parameters, so I had to just write a snippet of straight sql. Here's the solution:

 

DECLARE @valueList varchar(1000)

 

SELECT
   
@valueList = COALESCE(@valueList + ',', '') + CAST(Field AS varchar)
FROM
   Table

The only way I'd seen before to do this operation involved a cursor, so it was great to come across this idea. Basically, the use of COALESCE function makes this work. For the first row, it returns an empty string, as @valueList is initially null. This makes the result 'val1'. Then, for subsequent rows, it returns the value with a comma appended, so the result is a comma seperated list. In one statement!

I recently had a friend ask for help comparing two comma seperated lists in SQL server using a UDF. I started based around the Split UDF I wrote, and continued from there. The simplest and most performant thing I could think of was to use an INNER JOIN to compare the table values of the two lists. Then just compare the counts you get back to make sure they're all the same. Here's the UDF:

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

CREATE FUNCTION IsCommaListMatch
(
   @list1 varchar(255),
   @list2 varchar(255)
)
RETURNS bit
AS
BEGIN
   DECLARE @isMatch bit

   DECLARE @list1Values TABLE (val int)
   DECLARE @list2Values TABLE (val int)

   INSERT INTO @list1Values SELECT * FROM dbo.Split(@list1)
   INSERT INTO @list2Values SELECT * FROM dbo.Split(@list2)

   DECLARE @count1 int
   DECLARE @count2 int
   DECLARE @count1Join int
   DECLARE @count2Join int

   SELECT @count1=COUNT(val) FROM @list1Values
   SELECT @count2=COUNT(val) FROM @list2Values

   SELECT
      @count1Join=COUNT(list1Values.val),
      @count2Join=COUNT(list2Values.val)
   FROM
      
@list1Values list1Values
         INNER JOIN @list2Values list2Values
            ON
               list1Values.val=list2Values.val

   IF @count1=@count2 AND @count1=@count1Join AND @count2=@count2Join
      SET @isMatch=1
   ELSE
      SET @isMatch=0

   RETURN @isMatch
END

GO

As always, feel free to contact me if you have ideas on improving this.

I've seen a lot of people on the net looking for a SQL split user defined function. Looking around, there didn't seem to be anything solid and performant. I found this article from 4guysfromrolla, but looking at all those function calls makes me sick. So I wrote my own hopefully performant UDF. I removed the split character parameter, as almost all uses I saw were based on commas. But that should be very simple to add in.

Let me know if you think of any other ideas to make it faster or better.

SET

ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

CREATE

 FUNCTION Split
(
   @list varchar(255)
)
RETURNS @listTable TABLE (val int)
AS
BEGIN
   DECLARE @len int
   DECLARE @startPos int
   DECLARE @endPos int

 

   SET @len = LEN(@list) 
   SET @startPos = 1
   SET @endPos = CHARINDEX(',', @list)

 

   WHILE (@endPos > 0)
   BEGIN
      
INSERT INTO @listTable
         SELECT SUBSTRING(@list, @startPos, @endPos - @startPos)

 

      SET @startPos = @endPos + 1
      SET @endPos = CHARINDEX(',', @list, @startPos)
   END

 

   IF @startPos <= @len
      INSERT INTO @listTable
         SELECT SUBSTRING(@list, @startPos, (@len + 1) - @startPos)

 

END

   RETURN

GO

posted @ Monday, February 05, 2007 2:13 PM | Feedback (0) | Filed Under [ SQL ]

I always try to develop my sites to be standards compliant, and this means using css for all formatting and positioning when possible. I keep having to look up how to center an image, so I decided to write a post so I always know where this information is.

To center an image, set the left and right margins to auto. This will make the margins fill the available space. You must also set the display to block so the image is treated as a content block. Here's a full css definition:

img {
      
display: block;
      margin-left: auto;
      margin-right: auto;
}