Date type convervision

  • I have code below not working in SQL 2012

    declare @d1, @d2, @d3 datetime

    ...where @d1 between (@d2-15) and (@d3 + 15)

    Error:

    Implicit conversion from data type datetime to int is not allowed. Use the CONVERT function to run this query

    How to fix it?

  • Use DATEADD

    declare @d1, @d2, @d3 datetime

    ...where @d1 between DATEADD(DD,-15,@d2) and DATEADD(DD,15,@d3)

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2
  • For more info on all the date and time functions check out the documentation at Microsoft.

    "The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood"
    - Theodore Roosevelt

    Author of:
    SQL Server Execution Plans
    SQL Server Query Performance Tuning

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply