Thursday, June 14, 2007

Part 2 - Implementing PARSENAME to make use of the date range

Nuances about PARSENAME
* Evaluates from Right to Left
* Works with only a period as a delimiter


Code:
-- Note: @Week should be a stored proc input param
Declare @Week varchar(30)
Set @Week = '04/29/2007 to 05/05/2007'

Declare @NewDateRange varchar(30)
Declare @WeekBegin varchar(30)
Declare @WeekEnd varchar(30)


Set @NewDateRange = REPLACE(@Week, ' to ', '.')
Set @WeekBegin = PARSENAME(@NewDateRange, 2)
Set @WeekEnd = PARSENAME(@NewDateRange, 1)


SELECT @WeekBegin as BeginDate
SELECT @WeekEnd as EndDate


/* Results:
BeginDate = 04/29/2007
EndDate = 05/05/2007


Sources:
http://www.sqlteam.com/article/using-the-parsename-function-to-split-delimited-data
http://msdn2.microsoft.com/en-us/library/ms186862(SQL.90).aspx

No comments: