Search code examples
sql-serverdatetimedatediff

How does dateadd, datetime and datediff works together?


Can someone please help me understand this snippet of code:

DATEADD (dd, -1, DATEADD(mm, DATEDIFF(mm, 0, CONVERT(datetime, 
er.DATE_EFFECTIVE)) + 2, 0))

Solution

  • I'm posting this answer for other users that might be seeking out help. So please refer to Cody McPheron's comment:

    • datetime is a data type
    • DateDiff is the distance between two datetimes according to whatever the first parameter is (mm for month, day for day, etc)
    • DateAdd will give you a new datetime that is adjusted according to the first two parameters.
    • DateAdd (dd, -1, targetColumn) will give you one day earlier than targetColumn is set to