【问题标题】:Month and Year Part from GetDateGetDate 中的月份和年份部分
【发布时间】:2012-10-30 19:28:04
【问题描述】:

我在我的 asp.net 应用程序中使用 gridview,我想将数据集限制为在当前月份和年份提交的请求

我是应用程序开发的新手,并以以下方式思考

从 getdate() 函数中获取月份和年份部分,然后在 where 条件下将其与我们表中 receiveddate 列的月份和年份进行比较

我的选择语句如下所示

select requestno,receiveddate,businessneed from customerrequests 其中接收日期 = getdate()

上面的sql语句我是怎么实现的

【问题讨论】:

    标签: sql-server asp.net-mvc


    【解决方案1】:

    有两种方法可以做到这一点:

    • 在 SQL 中通过传递月份和年份的两个参数。这就是您的问题听起来的样子,只需这样做即可完成:

      select requestno,receiveddate,businessneed from customerrequests where YEAR(receiveddate) = YEAR(getdate()) AND MONTH(receiveddate) = MONTH(getdate())

    • 另一种方法是在客户端构造一个日期对象并将其传递给您的查询,然后使用常规日期比较技术。

    希望对您有所帮助。

    【讨论】:

      【解决方案2】:

      您可以执行以下操作:

      DECLARE @FirstDate DATETIME    
      SELECT @FirstDate = DATEADD(month, DateDiff(Month, 0, GETDATE()), 0)
      
      SELECT 
            requestno,receiveddate,businessneed 
      FROM 
            customerrequests 
      WHERE 
            receiveddate >= @FirstDate
      

      希望这有帮助!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-12-29
        • 2016-07-31
        • 1970-01-01
        • 2022-12-07
        • 2016-06-25
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多