I have a situation where I need to test for a “blank” date field and cannot get a returned value.
It seems easier to explain in this < 2 min video than typing a long winded explanation.
Essentially if my max(Trans Date) from the subquery is ____ then I want to use this other date where “____” from the subquery is not returning any of the following: ‘’, ’ ', or Null.
select
[Part].[PartNum] as [Part_PartNum],
[Part].[PartDescription] as [Part_PartDescription],
[Part].[LastSigV8_c] as [Part_LastSigV8_c],
[LastSigTrans].[Calculated_LastTranDate] as [Calculated_LastTranDate],
(case LastSigTrans.Calculated_LastTranDate
when ‘1/1/1900’ then 1
else 0
end) as [Calculated_booleancheck]
from Erp.PartBin as PartBin
inner join Erp.Part as Part on
Part.Company = PartBin.Company
and Part.PartNum = PartBin.PartNum
and ( Part.InActive = 0 )
left outer join (select
[PartTran].[PartNum] as [PartTran_PartNum],
(max(PartTran.TranDate)) as [Calculated_LastTranDate]
from Erp.PartTran as PartTran
where (PartTran.TranType in (‘MFG-CUS’, ‘MFG-STK’, ‘MFG-WIP’, ‘PUR-INS’, ‘PUR-MTL’, ‘PUR-STK’, ‘PUR-SUB’, ‘STK-ASM’, ‘STK-CUS’, ‘STK-MTL’, ‘WIP-MFG’))
group by [PartTran].[PartNum]) as LastSigTrans on
Part.PartNum = LastSigTrans.PartTran_PartNum
where (PartBin.OnhandQty > 0 and PartBin.PartNum = ‘81DCDR100DA’)
group by [Part].[PartNum],
[Part].[PartDescription],
[Part].[LastSigV8_c],
[LastSigTrans].[Calculated_LastTranDate]
I see that PartTran.TranDate is stored with a 2 digit year. Try using the PartTran.SysDate to filter instead. I tried to duplicate the issue on my end, but all of our part transactions have dates in that field. You could also try casting your PartTran.TranDate as another kind of variable and then output it directly to see what value might be stored there.
I think I see the issue. The subquery isn’t pulling any records. You’d think that the “LastTranDate” field would be Null, but it is at another level of nonexistence.
I find it rarely useful in practice, though. Can’t tell you how many times I start with one like that, get my hopes up for some clean code, and then end up with some horrific hierarchy of and clauses instead.
Like the one use case I have is to do a day(dateadd(...)) and see if it falls on a weekend and then adjust accordingly.