select dbi.Value from#db dbi
WHERE field LIKE'%lastknow%'AND (CASEWHEN isDate(dbi.value) =1THENCAST (dbi.Value AS DATETIME2(3))END) &lt; DATEADD (DAY,-7, GETDATE())
That “CASE” statement stands out quite a bit doesn’t it. Let’s explore why this query needs a CASE expression. Begin by creating a temp table that contains a sample of the data stored in the temp table.