Monday, 6 May 2013

SQL Query for From Date and To Date in Database and Parameter

In this post I am giving example of sql query.

sometimes we need From Date and To Date filter in our application and there is already From Date and To Date fields exist in our Database so how to query it I will show in belowe stored procedure.

Here check the where condition in the code.

CREATE PROCEDURE [dbo].[SPDailyDeviceLog]

@FDate date = NULL,
 @TDate date = NULL
AS
BEGIN

 SET NOCOUNT ON;

 select * from DailyLogs as l
 join Devices as d on d.DeviceID=l.DeviceID
 join Users as u on u.UserID = l.UserID
where ((@FDate BETWEEN convert(Date,l.CheckedInTime) AND convert(Date,l.CheckedOutTime)) OR (@FDate < convert(Date,l.CheckedInTime)) OR (@FDate IS NULL) or (l.CheckedInTime is NULL))
  and ((@TDate BETWEEN convert(Date,l.CheckedInTime) AND convert(Date,l.CheckedOutTime)) OR (@TDate > convert(Date,l.CheckedOutTime)) OR (@TDate IS NULL) or (l.CheckedOutTime is NULL))
   order by l.DailyLogID desc
END


No comments:

Post a Comment