• Your problem stems from the fact that the SQL Server Query Optimizer can rearrange conditions as it sees fit. With the equality constraint it apparently picks an execution plan where it only attempts to convert to int the values that are actually numeric. With the inequality, it picks a different plan and now it tries to convert texts to int - and fails.

    Can you try using where Period >= '201312' (by embedding the constant in quotes, it becomes a string and now no conversion is needed).

    Do test, for the > semantics are different for string then they are for integers. If the period substring is always six digits for the rows that qualify the other conditions, you'll be fine, but otherwise my suggestion can return incorrect results. I may have an alternative solution if that is the case, but please try this first.


    Hugo Kornelis, SQL Server/Data Platform MVP (2006-2016)
    Visit my SQL Server blog: https://sqlserverfast.com/blog/
    SQL Server Execution Plan Reference: https://sqlserverfast.com/epr/