Im currently working on a sql database that carries out several calculations based on external data.
Basically I have a field called ‘Runhrs’ and i take the max and min values of a specific day or a range of days.
The calculation will sometimes return (for example) 26 hours but as we know its not possible to have more than 24 hors in a day. The Calculation is correct but the data itself seems to have the errors but nothing can be done about the datas errors.
Therefore how do i limit the max value? The SQL statement is as shown:
SELECT MAX(CONVERT(FLOAT,ISNULL(Runhrs,Runho))) - MIN(CONVERT(FLOAT,ISNULL(Runhrs,Runho))) AS RUNHO, CONVERT(VARCHAR,TIME_STAMP,103) AS TIME_STAMP
FROM HL_LOGS
WHERE TIME_STAMP BETWEEN @BEGIN AND @END AND ID_LOCATION = @ID
GROUP BY CONVERT(VARCHAR,TIME_STAMP,103), CONVERT(VARCHAR,TIME_STAMP,111)
ORDER BY CONVERT(VARCHAR,TIME_STAMP,111)
Thanks in advance,
Neil.
Try using a CASE clause: