The select statement with current where clause is ok if HasFilter = 1 otherwise the last two condition must be ignore. How can I complete my query?
ALTER Procedure [dbo].[spGetNotPrintedCards]
@FromDate DateTime,
@ToDate DateTime,
@HasFilter Bit
As
Select CustomerName,
Family,
[ExpireDate],
Track1,
Track2,
Track3,
CVV2
From OfoghCardsRequest.dbo.CardRequests
Where Printed = 0 And
CreateDate > @FromDate And
CreateDate < @ToDate
The simplest way, I think, is the following:
Select CustomerName,
Family,
[ExpireDate],
Track1,
Track2,
Track3,
CVV2
From OfoghCardsRequest.dbo.CardRequests
Where Printed = 0 And (
(@Hasfilter = 0 OR @Hasfilter IS NULL) OR
(CreateDate > @FromDate And CreateDate < @ToDate))
If @HasFilter
is not equal to 1, i.e. if @HasFilter = 0
or @HasFilter IS NULL
, then
((@Hasfilter = 0 OR @HasFilter IS NULL) OR
(CreateDate > @FromDate And CreateDate < @ToDate))
is always true, thus your where clause boils down to: Printed = 0
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With