Archive for May, 2008

Type.GetType()

On Monday, May 26, 2008 - 23:33 in

Browsing through ASP.NET Forums, I found an interesting questions raised by a member that when he tried to get type information of a class using Type.GetType it was a charm in C#, but VB did not worked in same manners. He was trying to create new instances of that class at runtime, and populate those instances with data fetched from any database.

I checked its documentation in MSDN, here are its salient points, which should always be thought out before using this.

It accepts only a parameter which must contain an assembly qualified name of required type. This parameter could be only type name (i.e. "Contact") or may include namespace in it (i.e. "Hansvits.Models.Contact").

If parameter has only type name then method will search only in the assembly in which that call was made. If any full/partial assembly name is mentioned then it will load that assembly and search in it.

(Continue Reading …)

Using Boolean algebra in SQL

On Friday, May 16, 2008 - 01:59 in ,

Recently I was working on a procedure which required that I marked some items in different categories, and then based on that marking we have to summarize items in different sections. I have extracted the main idea from this procedure for our example. It is using some Boolean algebra to segregate items into different sections.

(Continue Reading …)

Bucketing consecutive numbers in a range

On Monday, May 12, 2008 - 02:17 in ,

Recently we had a little tricky problem to solve. We were displaying a report in which we had to bucket numbers in a range, such that only consecutive numbers should be in that range, if any break is there, then a new range should start. Our first solution did not worked as required. Most difficult part was identifying numbers in a sequence, and placing them in a bucket. We could not create any simple T-SQL queries which could easily sort these things out. Then we thought of first capturing the bucket of each number so that we can easily work it out, and that was not possible without cursors. Lets have a look how we did that.

(Continue Reading …)

Mod operator puzzle

On Monday, May 12, 2008 - 01:12 in ,

Recently I saw a puzzle on SQL Server Central, I was intrigued to solve it myself, so here is what I was able to accomplish, and in less than 10 minutes.

DECLARE @i int
SET @i = 0

CREATE TABLE #X (val INT)

WHILE @i < 100
BEGIN
    SELECT @i = @i + 1
    INSERT INTO #X VALUES (@i)
END

SELECT val,
       val % 3,
       val % 5,
       CASE
            WHEN val % 3 = 0 AND val % 5 = 0 THEN 'BIZZBUZZ'
            WHEN val % 3 = 0 THEN 'BIZZ'
            WHEN val % 5 = 0 THEN 'BUZZ'
       END xval
FROM   #X

DROP TABLE #X