May 31, 2020 at 10:41 am
Hi
I have query
select T0.DocType,T0.Debit , T1.Credit , T1.account from tbl1 where Mdate <= @frDate.
In this i want to add condition if @frdate Month = 3 and Date = 31 then documents of Type say TC should not be considered
Thanks
May 31, 2020 at 3:28 pm
Try adding this to your WHERE clause:
AND NOT (
DocType = 'TC'
AND MONTH(@frdate) = 3
AND DAY(@frdate) = 31
)
The absence of evidence is not evidence of absence
- Martin Rees
The absence of consumable DDL, sample data and desired results is, however, evidence of the absence of my response
- Phil Parkin
May 31, 2020 at 3:40 pm
Try adding this to your WHERE clause:
AND NOT (
DocType = 'TC'
AND MONTH(@frdate) = 3
AND DAY(@frdate) = 31
)
Is it this?
AND NOT (
DocType = 'TC'
AND MONTH(Mdate) = 3
AND DAY(Mdate) = 31
)
Aus dem Paradies, das Cantor uns geschaffen, soll uns niemand vertreiben können
May 31, 2020 at 3:44 pm
In this i want to add condition if @frdate Month = 3 and Date = 31 ...
The absence of evidence is not evidence of absence
- Martin Rees
The absence of consumable DDL, sample data and desired results is, however, evidence of the absence of my response
- Phil Parkin
May 31, 2020 at 4:11 pm
this
declare @frDate date='2020-03-31';
with a_cte(DocType, Debit, Credit, Account, Mdate) as
(select 'TC', 10, 20, 123, '2020-03-30'
union all
select 'TC', 10, 20, 123, '2020-03-31'
union all
select 'TC', 10, 20, 123, '2020-04-01'
)
select
DocType, Debit, Credit, Account, Mdate
from a_cte where Mdate <= @frDate
AND NOT (
DocType = 'TC'
AND MONTH(@frdate) = 3
AND DAY(@frdate) = 31
);
or this
declare @frDate date='2020-03-31';
with a_cte(DocType, Debit, Credit, Account, Mdate) as
(select 'TC', 10, 20, 123, '2020-03-30'
union all
select 'TC', 10, 20, 123, '2020-03-31'
union all
select 'TC', 10, 20, 123, '2020-04-01'
)
select
DocType, Debit, Credit, Account, Mdate
from a_cte where Mdate <= @frDate
AND NOT (
DocType = 'TC'
AND MONTH(Mdate) = 3
AND DAY(Mdate) = 31
);
Aus dem Paradies, das Cantor uns geschaffen, soll uns niemand vertreiben können
May 31, 2020 at 5:00 pm
I'm actually a bit concerned about the original query because it basically returns EVERYTHING from the beginning of the table up to a point in time. That could be a whole lot of stuff and that leads to two problems...
Further, modifying an already questionable query to search for Month and Day in this fashion will make the query non-SARGable and that's going to make an index seek impossible even if they do straighten out the original query.
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 7 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply