joshua
joshua

Reputation: 2371

how to write a query to satisfy search conditon

I need suggestion to write a query which satisfy these condition's.

I am passing three parameters to a stored procedure:

Requirement is like this:

I tried but that is complete waste.

If (@viewPending = 1 or @viewPending = 0 )
    Begin
        IF @TDate IS NULL AND @FDate IS NOT NULL
            SELECT 
            RegDate,
            CenterName  As [Center Name],
            OwnerName   As [Owner Name],
            MobileNo    As [Mobile],
            MailID      As [EMail ID],
            isVerified  As [Verified]
        FROM  TBL_iREGFORM
        WHERE REGDATE >= @FDate AND isVerified in (Case When  @viewPending =1 Then 0 Else 1 | 0 End) 
      IF @TDate IS NOT NULL AND @FDate IS  NULL
            SELECT 
            RegDate,
            CenterName  As [Center Name],
            OwnerName   As [Owner Name],
            MobileNo    As [Mobile],
            MailID      As [EMail ID],
            isVerified  As [Verified]
        FROM  TBL_iREGFORM
        WHERE REGDATE <= @FDate AND isVerified in (Case When  @viewPending =1 Then 0 Else 1 | 0 End)
       IF @TDate IS NOT NULL AND @FDate IS NOT NULL
            SELECT 
            RegDate,
            CenterName  As [Center Name],
            OwnerName   As [Owner Name],
            MobileNo    As [Mobile],
            MailID      As [EMail ID],
            isVerified  As [Verified]
        FROM  TBL_iREGFORM
        WHERE REGDATE BETWEEN  @FDate AND @TDATE AND isVerified in (Case When  @viewPending =1 Then 0 Else 1 | 0 End)
    ELSE

            SELECT 
            RegDate,
            CenterName  As [Center Name],
            OwnerName   As [Owner Name],
            MobileNo    As [Mobile],
            MailID      As [EMail ID],
            isVerified  As [Verified]
        FROM  TBL_iREGFORM
        WHERE REGDATE =CONVERT(VARCHAR(10),GETDATE(),103) AND isVerified in (Case When  @viewPending =1 Then 0 Else 1 | 0 End)

Upvotes: 1

Views: 79

Answers (1)

Paul Alan Taylor
Paul Alan Taylor

Reputation: 10680

SELECT * FROM
  MyTable
WHERE
  ( @startDate IS NULL OR RegisterDate >= @startDate )
AND ( @endDate IS NULL OR RegisterDate <= @endDate )
AND ( @active IS NULL OR Active = @active )

Upvotes: 4

Related Questions