SELECT SchoolID
FROM ParameterDetails
INNER JOIN
EstablishmentParameterValues AS v
ON v.ParameterID = ParameterDetailID
WHERE NameResourceKey = 'NonTeachingStaffStartTime'
AND DATEDIFF(mi, CONVERT (DATETIME, (CONVERT (NVARCHAR (20), CONVERT (DATE, GETDATE()), 110) + ' ' + v.ParameterValue)), GETDATE()) <= 60
Above query gives the error
The conversion of a nvarchar data type to a datetime data type resulted in an out-of-range value
When I use write
DATEDIFF(mi, CONVERT (DATETIME, (CONVERT (NVARCHAR (20), CONVERT (DATE, GETDATE()), 110) + ' ' + v.ParameterValue)), GETDATE()) <= 60
in Select Clause it does not give error, then why is it giving error in Where Clause..
V.ParameterValues table contains below kind of values:-
8:45
8:45
08:40:00
08:30:00
8:45
ParameterValue
which don't resemble times (maybe for something other thanNonTeachingStaffStartTime
) then you may be doomed until you split this into two separate queries. There's no guarantee on what orderWHERE
clause arguments are evaluated, and even if you e.g. moved part of this into a subquery, there's always SQL Server should not raise illogical errors to inform you that that strategy won't protect you. – Damien_The_Unbeliever