Reputation: 2371
I need suggestion to write a query which satisfy these condition's.
I am passing three parameters to a stored procedure:
to date
from date
is active
Requirement is like this:
to date
then i need to show only those who registar till datefrom date
show only those who are after that dateI 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
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