Date validation in query
Get record between specific date range. means retrieve the all record on the basis of date range.
my store procedure help you solve this date range problem. Find below store procedure and try it. i hope it will helpful to you.
Sometime i found that some programmer found a problem regarding retrive data from database on basis of date range condition.
We have provide followoing store procedure for retriveing all data by date range.
suppose that you have a table (employee) with following column.
pkid int
name varchar(50)
address varchar(100)
DOB datetime
sample record
--------------
1 varun gurgaon 01/08/1983
2 ajay agra 23/08/1983
3 vijay jaipur 01/01/2000
4 ram delhi 05/05/2012
Now you want to retrieve all employee records who's DOB between 08/1/1983 to 08/31/1983.
Note: date must be pass in MM/dd/yyyy format.
create a procedure with two parameter name like @fromdate and @todate
Parameter value is @fromdate='08/01/1983' and @todate='08/31/1983'
CREATE PROCEDURE [dbo].[usp_fetch_Employee]
@fromdate datetime,
@todate datetime
AS
BEGIN
SELECT name,address,DOB
FROM employee
where
CAST(CONVERT(VARCHAR(40), DOB,101) AS DATETIME)>= CAST(CONVERT(VARCHAR(40),@fromdate,101) AS DATETIME)
and CAST(CONVERT(VARCHAR(40), DOB,101) AS DATETIME)<=CAST(CONVERT(VARCHAR(40),@todate,101) AS DATETIME)
END
Finally Result is:
varun gurgaon 01/08/1983
ajay agra 23/08/1983
I hope it will fine.
Happy code
Varun Bansal
http://varunbansal007.blogspot.com/