Search content within the blog

Tuesday, December 9, 2008

Searching data between two dates by ignoring time values

Here is a stored procedure that returns records between from date and to date by ignoring their time values. If the from date , to date are not supplied it returns all the values of the table.
CREATE Procedure [dbo].getAllRecordsIgnoringTimevalues         
(
@FromDate VARCHAR(25) =NULL,--indicates that the parameter it is optional
@ToDate VARCHAR(25) =NULL --indicates that the parameter it is optional
)
As
Begin


Select *
From
[dbo].tabletosearch

WHERE
AND (CAST(FLOOR(CAST( fromdate AS float)) AS datetime) >= convert(datetime,@FromDate)OR ISNULL(convert(datetime,@FromDate),'')='')
AND (CAST(FLOOR(CAST( todate AS float)) AS datetime) <= convert(datetime,@ToDate) OR ISNULL(convert(datetime,@ToDate),'')='')

END

No comments:

Post a Comment