Always watch your PATH

For a couple of days I’ve had major trouble with my work notebook. After installing new Visual Studios (2012 and 2013), all of a sudden various system software stopped working.

For example several applications could no longer be started using the pinned icons in the task bar. Also, when trying to invoke some system dialogs in the Control Panel I got error messages like %windir%\system32\xyz.exe was not found. Or the strange fact that all the entries in the Start Menu lost their symbols…

After some days of much cursing and swearing, several attempts of restoring previous Restore Points, etc., I got a hint that solved my problem:

The content of the PATH environment variable may not be longer than 2048 characters! If it is, neither the PATH variable, nor any other %..% system variable will work.

I shortened the path, rebooted, problem solved. Welcome to Windows 7, where the PATH variable is still limited in length…

Rounding to DATETIME components

Sometimes you need to round DateTimes in T-SQL to a certain DateTime component. For example, you need to truncate the minutes and seconds and only keep the date and the hour. To do so, you can do this:

SELECT DATEADD(hh, DATEDIFF(hh, '20000101', getdate()), '20000101')

This turns 20120101 13:59:59 into 20120101 13:00:00. Please note that the comparison date 20000101 is totally arbitrary. You can use any day before the date you’re truncating.

Transpose lines into comma separated strings

Sometimes you need to return from a stored procedure the results of a SELECT as a comma-separated string.

Example: The SELECT returns:

But you want the stored procedure to return:
Hello, World, It's, Me

Instead of looping with a cursor you can solve this pretty elegantly like the following:

The AND Field IS NOT NULL at the end of the WHERE-clause is important, because otherwise the result would always be null if one of the values was null (because something + NULL = NULL).

Paginate results from T-SQL query

In MySQL you can use LIMIT to page results. There’s nothing like that in T-SQL, but with a little effort you get the same result:

This returns 15 entries starting at entry 45 in table tCustomer. Please note that the page number is zero-based here, that’s why we use ROW_NUMBER() OVER (ORDER BY LastName, FirstName)) – 1.

Notes about System.Threading.Timer

You can fail to pass a state object to a System.Threading.Timer – you won’t see any values if you pass the instance to the timer’s constructor before initializing the values, as obviously a copy of the object is created.

The following does not work:

The timer will use 0 for object.Number.

The following, however, works as expected:

Same column widths in WPF ListBox

You can use ItemTemplate to style the appearance of entries of a ListBox for example. Sometimes you want to use a Grid, and you want the column widths to be the same for all items of the ListBox. You can simply use the SharedSizeGroup attribute:

Don’t forget to set the Grid.IsSharedSizeGroup attribute to true for the ListBox.