SELECT DISTINCT p.codInterno,
CASE
WHEN f.idFabricante IS NULL THEN 99999999999999
ELSE f.idFabricante
END AS fabri,
'0',
p.descResumida,
p.descProduto,
p.descProduto,
p.codElemento,
GETDATE(),
GETDATE(),
0,
0
FROM [HORPDB00\PDBSIAC].[SiacPRD].[dbo].[IntPluProduto] p
INNER JOIN [HORPDB00\PDBSIAC].[SiacPRD].[dbo].[IntControle] c on c.idProcesso = p.idProcesso
LEFT JOIN [HORPDB00\VANR].[tlmark].[dbo].[Produto] vanProduto on vanProduto.CdPrdSAP = p.codInterno
LEFT JOIN tbFabricante f on f.idFabricante = CAST(vanProduto.CdFbr AS int)
WHERE NOT EXISTS(select 1 from tbProduto where idSAP = p.codInterno)
AND vanProduto.cdFbr <> 'NNN0'
AND CAST(c.dtHrProcesso AS DATE) > DATEADD(ss, 64800, DATEADD(d, -1, CAST(CONVERT(varchar(10), GETDATE(), 103) AS smalldatetime)))
OR CAST(c.dtHrProcesso AS DATE) = CAST(GETDATE() AS DATE)
AND c.codModulo = 14
AND vanProduto.ICMSubst <> 'T'
At this point:
AND CAST(c.dtHrProcesso AS DATE) > DATEADD(ss, 64800, DATEADD(d, -1, CAST(CONVERT(varchar(10), GETDATE(), 103) AS smalldatetime)))
OR CAST(c.dtHrProcesso AS DATE) = CAST(GETDATE() AS DATE)
The above filter does nothing but, starting from the current date, subtracts 1 day, and after adding 18 hours (64800 seconds). For example, for the current date 12/27/2016 06:00:00: Subtract 1 day: 12/26/2016 06:00:00 Matches the result to: 26/12/2016 00:00:00 Add 64800 seconds (18hrs): 12/26/2016 6:00 PM
Conversion error: The conversion of a varchar data type to a smalldatetime data type resulted in an out-of-range value.