SQL选择过去两个月的所有条目

时间:2015-09-27 18:21:27

标签: sql sql-server select sql-server-2012

我想动态选择过去两个月的所有条目,而不在我的查询中输入日期范围。

这是我的简单代码:

SELECT Customer_Name, Date FROM table_Customer; all data between last  two month

提前感谢您的帮助

SELECT
    ME.FullName,
    R.RuleDefaultName,
    PR.ObjectName,
    PR.CounterName,
    P.DateTime,
    P.SampleCount,
    P.MinValue,
    P.MaxValue,
    P.AverageValue,
    P.StandardDeviation
FROM
    Perf.vPerfHourly P
INNER JOIN vManagedEntity ME ON
    P.ManagedEntityRowId = ME.ManagedEntityRowId
INNER JOIN vPerformanceRuleInstance PRI ON
    P.PerformanceRuleInstanceRowId = PRI.PerformanceRuleInstanceRowId
INNER JOIN vPerformanceRule PR ON
    PRI.RuleRowId = PR.RuleRowId
INNER JOIN vRule R ON
    PRI.RuleRowId = R.RuleRowId

2 个答案:

答案 0 :(得分:4)

SELECT Customer_Name, Dt
FROM table_Customer
where dt >= dateadd(day, -60, getdate())

SELECT Customer_Name, Dt
FROM table_Customer
where dt >= dateadd(month, -2, getdate())

您应该确保不要将保留关键字用作列名。

确保使用相应的dt列替换date。该解决方案假设您需要从当前日期开始的前2个月数据。

答案 1 :(得分:0)

Select *
From Customers
Where 
    OrderDate between Dateadd(M,-2,OrderDate) And Getdate()