Achar total de dias utilizando SQL

Delphi

12/09/2013

Ola, para achar o total de dias entre a data do pedido e do faturamento é
select datediff(day,a.datped,getdate()) as tement, a.numped, a.valven, a.datped, a.datfat, a.codven, b.nomven
from ftd001 a, Arq004 b, Arq005 c
where a.datcan is null
and a.tippag = 4
and b.tipven = 'V'
and b.codven = a.codven
and c.tipdoc = 'R'
and c.codcli = a.codcli
and c.codven = a.codven
order by a.datped, c.codcli

o caso é como eu faço para achar o total de dias de de entrega de um pedido se o campo datfat que é data da fatura for nula?

obrigado
"Deus Abençoe"
Wilton Júnior

Wilton Júnior

Curtidas 0

Respostas

Wilton Júnior

Wilton Júnior

12/09/2013

'já achei um meio obrigado :)
GOSTEI 0
Joel Rodrigues

Joel Rodrigues

12/09/2013

Poderia compartilhar conosco a solução encontrada?
GOSTEI 0
Wilton Júnior

Wilton Júnior

12/09/2013

Duas solucoes
select a.numped, a.valven, a.datped, a.datfat,
case
when (datfat is null) then
datediff(day,a.datped,getdate())
when (datfat is not null) then
datediff(day,a.datped,a.datfat)
end as tement
from ftd001 a, Arq005 b
where a.datcan is null
and a.tippag = 4
and b.tipdoc = 'R'
and b.codcli = a.codcli
and b.codven = a.codven
--and b.cliafa = 0
order by a.datped, b.codcli

ou
select a.numped, a.valven, a.datped, a.datfat,
if cxdateedit1.text = '' then
datediff(day,a.datped,getdate()) as tement
else
datediff(day,a.datped,a.datfat) as tement

from ftd001 a, Arq005 b
where a.datcan is null
and a.tippag = 4
and b.tipdoc = 'R'
and b.codcli = a.codcli
and b.codven = a.codven
--and b.cliafa = 0
order by a.datped, b.codcli
GOSTEI 0
Joel Rodrigues

Joel Rodrigues

12/09/2013

Obrigado por compartilhar.
Estou marcando o tópico como concluído.
GOSTEI 0
POSTAR