• Ed Wagner (1/31/2016)


    Jonathan AC Roberts (1/31/2016)


    CAST(LEFT(OMD, 8) as datetime) + 30 < GETDATE();

    This is not good practice, I'm not sure it will even work in versions of SQL Server later than 2005.

    You should be using the DATEADD function, i.e.:

    DATEADD(dd,30,CONVERT(datetime,LEFT(OMD, 8)) < GETDATE();

    The storage of a date is never a good practice. A good idea would be to storage dates in a date/datetime column.

    The conversion of a string to a datetime can cause errors if the string contains invalid date values. Even if it doesn't, it'll still be much slower than it would be using a date data type. Doing a calculation on a column will also be slower than it needs to be because the engine has to perform the calculation on every row in the table and then perform the comparison.

    A good way of doing the comparison would be to compare a date column against a date that's 30 days ago.

    Example: WHERE some_date < DATEADD(day, -30, GETDATE())

    Integer addition and subtraction against a datetime data type does work after SQL 2005.

    I agree, I didn't mention changing the type of the column to datetime as I just assumed he was just trying to get the job done on a badly implemented design.

    Also, putting the DATEADD(day, -30, GETDATE()) on the right hand side, as you did, makes the query sargable should indexes ever bee added to the date column.