Thursday, May 30, 2013

SQL Query to return a table containing all dates falling within a date range along with their week day names

Following is the T-SQL code to return all dates falling within a given date range. This uses CTE for populating all dates, and store them in a temporary table.

DECLARE @StartDate datetime
DECLARE @EndDate datetime
SET @StartDate = '01-Apr-2013'
SET @EndDate = '30-Apr-2013'

;WITH DatesList
SELECT @StartDate [fldDate]
SELECT fldDate + 1 FROM DatesList WHERE fldDate < @EndDate

SELECT fldDate, DATENAME(dw, fldDate) as fldDayFullName INTO #DatesList FROM DatesList option (maxrecursion 0)

SELECT * FROM #DatesList

Friday, May 10, 2013

How to convert an array of one type to another type in using Linq in .NET

Following is the sample code that converts a string array into GUID array with faster performance:

String strGUIDs[];
//Consider the above array is containing all GUIDs in string format.
Guid[] guids = Array.ConvertAll(strGUIDs, x => Guid.Parse(x));

You can convert an array from one type to another using the above approach.