I am running into this error whenever I run my query.
I believe to fix this issue I have to Format the column which holds those values into a number by itself. I have already tried the Format() and Replace() functions, but they did not work for me. Any other suggestions?
SELECT
e.accountingcode AS [Vehicle],
Concat(e.year + ' ', e.make + ' ', e.model + ' ', Cast( e.[Description] AS VARCHAR(80))) AS NAME,
Iif(cff.stringtype = '', cff1.stringtype, cff.stringtype) AS 'Operator',
fta.fluid,
so.odometer,
CASE
WHEN
COALESCE(Lag(e.accountingcode) OVER ( partition BY e.accountingcode
ORDER BY
e.accountingcode, so.odometer), 0) <> e.accountingcode
THEN
0
ELSE
Lag(so.odometer) OVER ( partition BY e.accountingcode
ORDER BY
e.accountingcode, so.odometer)
END
AS 'Previous Odometer', so.odometer - Iif((
CASE
WHEN
COALESCE(Lag(e.accountingcode) OVER ( partition BY e.accountingcode
ORDER BY
e.accountingcode, so.odometer), 0) <> e.accountingcode
THEN
0
ELSE
Lag(so.odometer) OVER ( partition BY e.accountingcode
ORDER BY
e.accountingcode, so.odometer)
END
) = 0, so.odometer,
CASE
WHEN
COALESCE( Lag(e.accountingcode) OVER ( partition BY e.accountingcode
ORDER BY
e.accountingcode, so.odometer), 0) <> e.accountingcode
THEN
0
ELSE
Lag(so.odometer) OVER ( partition BY e.accountingcode
ORDER BY
e.accountingcode, so.odometer)
END
) AS 'Miles Traveled', sh.hour,
CASE
WHEN
COALESCE(Lag(e.accountingcode) OVER ( partition BY e.accountingcode
ORDER BY
e.accountingcode, sh.hour), 0) <> e.accountingcode
THEN
0
ELSE
Lag(sh.hour) OVER ( partition BY e.accountingcode
ORDER BY
e.accountingcode, sh.hour)
END
AS 'Previous Hours', sh.hour - Iif((
CASE
WHEN
COALESCE(Lag(e.accountingcode) OVER ( partition BY e.accountingcode
ORDER BY
e.accountingcode, sh.hour), 0) <> e.accountingcode
THEN
0
ELSE
Lag(sh.hour) OVER ( partition BY e.accountingcode
ORDER BY
e.accountingcode, sh.hour)
END
) = 0, sh.hour,
CASE
WHEN
COALESCE(Lag(e.accountingcode) OVER ( partition BY e.accountingcode
ORDER BY
e.accountingcode, sh.hour), 0) <> e.accountingcode
THEN
0
ELSE
Lag(sh.hour) OVER ( partition BY e.accountingcode
ORDER BY
e.accountingcode, sh.hour)
END
) AS 'Hours Used' , fta.fifototalcost / fta.totalgallons AS [Avg Price], fta.totalgallons AS [Total Gallons], fta.fifototalcost AS [Total Cost]
The e.AccountingCode column contains the value that is at error.
Best Answer
The problem seems to be on the Coalesce's.
Source
For example:
Gives the error:
This works fine:
Result:
If the lag function on e.accountingcode can't be NULL, then you could use
ISNULL()
, but you should look into the differnces with coalesce.Source
Or in other words, if the first value is not a NULL value, return that datatype, if it is NULL and a replacement value, in our case 0 is passed, then it will be that datatype. If all are NULL then it will be an integer.
Solution #1, changing 0 to '0'
Solution #2, changing coalesce to ISNULL()
Educate yourself on the differences with coalesce, for example only do this when the LAG() function on e.accountingcode can't return NULL, which kind of defeats the purpose of the isnull