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 2012 Forums
 Transact-SQL (2012)
 Need Query Help

Author  Topic 

BitsAndBytes
Starting Member

10 Posts

Posted - 2014-05-15 : 09:33:17
Good morning everyone. I have a query that isn't behaving like I want. The query is to be used in an SSRS report. I want the query to return all records if nothing is selected for a start and end date. The way I have it now nothing shows up if the begin and start dates aren't supplied. Can someone please take a look at the below SP and show me how to alter it so that all records are returned if the start and end dates are omitted? Or maybe my approach is wrong and someone can point me in the right direction?




USE [mybookstore]
GO

/****** Object: StoredProcedure [dbo].[GetSales] Script Date: 5/15/2014 9:26:50 AM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[GetSales]
(
@pBeginDate DateTime = NULL,
@pEndDate DateTime = NULL
)
AS
BEGIN

select H.InvID, H.OrdDate, H.ExpDate, H.PoNumber, H.CustNumber, H.ShippedDate, D.InvPk, D.ProductID,
D.Quantity * D.UnitPrice as Sales, S.SalesRepNumber, S.FullName, C.CustName
from InvHeader H inner join InvDetails D on H.InvID = D.InvID
inner join Customers C on H.CustNumber = C.CustNumber
inner join SalesRep S on C.Salesperson = S.SalesRepNumber
where H.ShippedDate IS NOT NULL
and (H.ShippedDate BETWEEN (@pBeginDate) AND (@pEndDate))

END

gbritton
Master Smack Fu Yak Hacker

2780 Posts

Posted - 2014-05-15 : 10:03:33
Change your WHERE clause to:


WHERE H.ShippedDate IS NOT NULL AND
(
(@pBeginDate IS NOT NULL AND @pEndDate IS NOT NULL AND H.ShippedDate BETWEEN (@pBeginDate) AND (@pEndDate)
OR (@pBeginDate is NULL OR @PEndDate is null)
)


However, what do you want to do is the start date is specified but the end date is omitted? Or vice-versa? (You should account for the two other conditions)
Go to Top of Page

DonAtWork
Master Smack Fu Yak Hacker

2167 Posts

Posted - 2014-05-15 : 10:13:31
ha! glad i refreshed the page or i would have had to post












How to ask: http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Go to Top of Page

BitsAndBytes
Starting Member

10 Posts

Posted - 2014-05-15 : 10:30:59
Thanks for the help.

How do you and you others in the know, handle that situation of where only start or end date is specified? I'm a rookie. Looks like if end date isn't supplied then query till current date would be a good idea, if so how do I do that? But as far as the begin date not being specified do you guys default to the first day of the current year or something like that?
Go to Top of Page

gbritton
Master Smack Fu Yak Hacker

2780 Posts

Posted - 2014-05-15 : 10:44:13
quote:
Originally posted by BitsAndBytes

Thanks for the help.

How do you and you others in the know, handle that situation of where only start or end date is specified? I'm a rookie. Looks like if end date isn't supplied then query till current date would be a good idea, if so how do I do that? But as far as the begin date not being specified do you guys default to the first day of the current year or something like that?



That's a question I'd send back to the business owners. Whatever they say, it should be easy enough to handle.
Go to Top of Page
   

- Advertisement -