如何使用 SQL Server 与当前周进行比较?

2021-09-10 00:00:00 sql tsql sql-server

如何将 SQL Server 日期列与当前周进行比较?

How do I compare an SQL Server date column against the current week?

例如:

WHERE [Order].SubmittedDate = *THIS WEEK*

推荐答案

您可以将日期转换为周数,并将其与当前日期的周数进行比较.同样,您还需要比较年份,以免获得去年的周数.

You could convert your date to a week number and compare this to the week number from the current date. Likewise, you'll need to compare the year as well, so that you don't get last year's weeks.

WHERE DATEPART(wk, [Order].SubmittedDate) = DATEPART(wk, GETDATE())
AND DATEPART(yy, [Order].SubmittedDate) = DATEPART(yy, GETDATE())

相关文章