Please check the problem description below
SELECT Item.File, Item.Code,
SUBSTRING(CONVERT(varchar(8), Search.OrdDate), 5, 2) + '/' + SUBSTRING(CONVERT(varchar(8), Search.OrdDate), 7, 2) + '/' + SUBSTRING(CONVERT(varchar(8), Search.OrdDate),1,4) as orderdate ,
SUBSTRING(CONVERT(varchar(8), Items.CompleteDt), 5, 2) + '/' + SUBSTRING(CONVERT(varchar(8), Items.CompleteDt), 7, 2) + '/' + SUBSTRING(CONVERT(varchar(8), Items.CompleteDt),1,4) as completedate,
datediff(DAY,
SUBSTRING(CONVERT(varchar(8), Search.OrdDate), 5, 2) + '/' + SUBSTRING(CONVERT(varchar(8), Search.OrdDate), 7, 2) + '/' + SUBSTRING(CONVERT(varchar(8), Search.OrdDate),1,4) ,
SUBSTRING(CONVERT(varchar(8), Items.CompleteDt), 5, 2) + '/' + SUBSTRING(CONVERT(varchar(8), Items.CompleteDt), 7, 2) + '/' + SUBSTRING(CONVERT(varchar(8), Items.CompleteDt),1,4)
) + 1 AS [NumOfDays]
FROM Items INNER JOIN
Search ON
Item.File = Search.FirmFile
where Items.Code like 'Policy'
Problem Description
The above query works well, if there is a valid date in CompleteDt.
However, if there is a 0 then it is displays an error
Syntax error converting datetime from character string.
How should I check for 0’s in the CompleteDt field and cancel the date diff computation.
Also could I possible set the two the orderdate and complete date in a variable .. and use the variables to compute the datediff..??