Pages

Tuesday, September 14, 2010

SQL - Date range query


Sometimes we need to get info from the db with the range provided in the stored procedure based on the date and the the date that we send to stored procedure is of the format "dd-mm-yyyy"
Here is the solution to the problem

exec GetCandidateResume @FromDate=N'07-09-2010',@ToDate=N'30-09-2010'

CREATE PROCEDURE [GetCandidateResume] 
(     
    @FromDate    VARCHAR(20),
    @ToDate        VARCHAR(20)
)     
AS 
SELECT
    CandidateId,
    CandidateName
FROM tbl_CandidateInfo
WHERE
    (CONVERT(DATETIME,CONVERT(VARCHAR(12),CandidateAppliedDate,105),105) >=CONVERT(DATETIME, @FromDate, 105))
    AND
    (CONVERT(DATETIME,CONVERT(VARCHAR(12),CandidateAppliedDate,105),105) <=CONVERT(DATETIME, @ToDate, 105))


Hoping that this will help.
:)

No comments:

Post a Comment