Wednesday, February 2, 2011

SQL Server: A Simple Method to Get Specific Part of String


SUBSTRING is a nice function to get a part of string, but when a string consists of many parts, getting specific part of it, is a bit painful task. Hopefully, following function will help you to achieve such tasks.




Hopefully, you will modify it to handle invalid sections i.e. in our example case if, only 4 section are in string but you provide if number 5 as section it will return you first part. Modify it, so it should return NULL for such cases. ;)

SQL Server: Indexes for GROUP BY Columns, a Good Choice


Every one of us knows that index on the columns referred to in an ORDER BY clause are helpful for optimizer to organize the output fast because columns values are already in sorted form.
Same way, columns which are part of GROUP BY benefit from indexes on the corresponding columns as GROUP BY clause columns are sorted first because sorted column values allow the adjacent matching values to be grouped quickly.
Indexes are also helpful for aggregate functions like MIN and MAX. Indexes on such columns are helpful for optimizer which need table/clustered index scan to find MIN or MAX values for a given column.