What's a good way to check if two datetimes are on the same calendar day in TSQL?

前端 未结 10 1454
小鲜肉
小鲜肉 2021-02-12 02:15

Here is the issue I am having: I have a large query that needs to compare datetimes in the where clause to see if two dates are on the same day. My current solution, which suck

10条回答
  •  南旧
    南旧 (楼主)
    2021-02-12 03:10

    I would use the dayofyear function of datepart:

    
    Select *
    from mytable
    where datepart(dy,date1) = datepart(dy,date2)
    and
    year(date1) = year(date2) --assuming you want the same year too
    

    See the datepart reference here.

提交回复
热议问题