Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How ignore some condition in where clause according to third parameter?

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
like image 835
Meysam Tolouee Avatar asked Oct 20 '25 11:10

Meysam Tolouee


1 Answers

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

like image 84
Giorgos Betsos Avatar answered Oct 23 '25 01:10

Giorgos Betsos



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!