以下代码返回什么? sql server 2012

时间:2013-11-20 13:29:57

标签: sql sql-server date

我正在挖掘一些代码,我不是sql的专家。有谁可以告诉我,以下内容会返回什么?

select DATEADD(day, DATEPART(weekday, GETDATE()) * - 1, GETDATE())

2 个答案:

答案 0 :(得分:2)

以外行人的话说,这会返回“上周六”。如果“今天”是星期六,它仍然会在星期六之前返回。

-- This will make it very apparent
-- Please note that the DatePart() function is language/settings dependant. So while on one server set up it may return "last Saturday", on others it may return "last Sunday"
select DATEADD(day, DATEPART(weekday, '11/01/2013') * -1, '11/01/2013')
select DATEADD(day, DATEPART(weekday, '11/02/2013') * -1, '11/02/2013')
select DATEADD(day, DATEPART(weekday, '11/03/2013') * -1, '11/03/2013')
select DATEADD(day, DATEPART(weekday, '11/04/2013') * -1, '11/04/2013')
select DATEADD(day, DATEPART(weekday, '11/05/2013') * -1, '11/05/2013')
select DATEADD(day, DATEPART(weekday, '11/06/2013') * -1, '11/06/2013')
select DATEADD(day, DATEPART(weekday, '11/07/2013') * -1, '11/07/2013')

答案 1 :(得分:0)

DATEPART(weekday, GETDATE())

每周回复你。

  • sunday = 1
  • 星期一= 2

  • 等...

然后:

select DATEADD(day, DATEPART(weekday, GETDATE()) * - 1, GETDATE())

示例周三= 4

将(4天* -1)添加到今天=今天 - 4天