语法问题根据计算

时间:2016-06-08 19:35:08

标签: sql-server

我不同意 - 我正在寻找正确的语法来更改where子句,首先返回where子句的第一部分的结果,然后返回该日期结果的第一个月的第一部分。需要有关正确语法的帮助。

感谢,

ķ

-- DATEADD(mm, DATEDIFF(mm, 0, GETDATE()), 0)

修改Where语句

  

块引用

CAST([PKDate] AS DATE)> CAST(GETDATE() - 91日期)

  

块引用

总是反映一天中第一天的91天计算结果 - 即。如果91天的日期= 2016年3月3日,则实际日期应为3/1/2016

SELECT [PKDate]
, [calendar_MONTH] as MthNo
, [is_weekend]
, [is_workday]
, Day(EOMONTH([PKDate])) as DaysinMth
FROM [calendar].[dbo].[days]
WHERE   CAST([PKDate] AS DATE) > CAST(GETDATE() - 91 AS DATE)
AND CAST([PKDate] AS DATE) <= CAST(GETDATE() - 1 AS DATE)
GROUP BY [PKDate], calendar_MONTH
, is_weekend
, [is_workday]

2 个答案:

答案 0 :(得分:0)

没关系,我明白了!

SELECT [PKDate]
, a.[calendar_month] as MthNo
, [is_weekend]
, [is_workday]
, Day(EOMONTH([PKDate])) as DaysinMth
, DATEADD(mm, DATEDIFF(mm, 0, CAST(GETDATE() - 91 AS DATE)), 0) as AdjDate
FROM [calendar].[dbo].[days] a
WHERE   CAST([PKDate] AS DATE) > DATEADD(mm, DATEDIFF(mm, 0, CAST(GETDATE() - 91 AS DATE)), 0)
    AND CAST([PKDate] AS DATE) <= CAST(GETDATE() - 1 AS DATE)
GROUP BY [PKDate]
    ,   a.calendar_MONTH
    ,   is_weekend
    ,   [is_workday]

答案 1 :(得分:0)

这可能更简单......

DATEADD(DAY, -91, GETDATE())