Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 start date and end date paramter

Author  Topic 

akpaga
Constraint Violating Yak Guru

331 Posts

Posted - 2013-09-16 : 16:12:20
Hi friends

I want to pass startdate and endate parameters for a field customer date
during a select query. What is the efficient way of handling this.
Also i want select all when no start and end date are provided.

Thank You in advance

James K
Master Smack Fu Yak Hacker

3873 Posts

Posted - 2013-09-16 : 16:47:56
Create a stored procedure like shown below, and pass in the parameters when you call the stored proc. If you don't pass in one or both parameter(s), it will assume not filter by that criterion.
CREATE PROCEDURE dbo.SomeProcedure
@StartDate DATE = NULL,
@EndDate DATE = NULL -- inclusive
AS

SELECT col1, col2, col3
FROM yourTable
WHERE
( YourDateCol >= @StartDate OR @StartDate IS NULL)
AND
( YourDateCol < DATEADD(dd,1,@EndDate) OR @EndDate IS NULL);
GO
Go to Top of Page

akpaga
Constraint Violating Yak Guru

331 Posts

Posted - 2013-09-16 : 17:24:34
Thank you james..appreciate your help.
Go to Top of Page

James K
Master Smack Fu Yak Hacker

3873 Posts

Posted - 2013-09-16 : 17:40:51
You are very welcome - glad to help.
Go to Top of Page
   

- Advertisement -