【问题标题】:How to select current dates from current month如何从当前月份中选择当前日期
【发布时间】:2013-09-07 03:14:37
【问题描述】:

我想检索当前月份 1 -30 之间的数据 [我正在使用 MSACCESS Dbase 这样做] 下面是我正在尝试的查询 --

SELECT count(usercategory) as category_count ,usercategory  FROM user_category 
where IssueDate between DATEADD('m', DATEDIFF('m', 0, DATE()) - 0 , 0) and  DATEADD('m',     DATEDIFF('m', 0, DATE()) + 1, - 1 ) group by usercategory

我保存在我的 MSACCESS 数据库中的数据 -

Category1   9/7/2013 12:00:00 AM
Category1   9/8/2013 12:00:00 AM
Category2   10/8/2013 12:00:00 AM

所以输出应该只有 2 条记录 但我的查询没有给出任何结果

【问题讨论】:

    标签: c# c#-4.0 ms-access c#-2.0


    【解决方案1】:

    这是我认为您需要的查询。它使用的所有函数在 Access SQL 中始终可用,无论查询是从 Access 会话内部运行还是从外部运行(如在您的 c# 情况下)。

    db 引擎将对这两个DateSerial 表达式进行一次评估,然后使用它们的结果来过滤结果集。这种方法在IssueDate 上的索引会特别快。

    SELECT
        Count(usercategory) AS category_count,
        usercategory
    FROM user_category 
    WHERE
            IssueDate >= DateSerial(Year(Date()), Month(Date()), 1)
        AND IssueDate < DateSerial(Year(Date()), Month(Date()) + 1, 0)
    GROUP BY usercategory;
    

    这是一个 Access Immediate 窗口会话,它解释了那些 DateSerial 表达式的逻辑...

    ? Date()
    9/6/2013 
    ? Year(Date())
     2013 
    ? Month(Date())
     9 
    ' get the date for first of this month ...
    ? DateSerial(Year(Date()), Month(Date()), 1)
    9/1/2013 
    ' now get the date for the last of this month ...
    ? DateSerial(Year(Date()), Month(Date()) + 1, 0)
    9/30/2013 
    

    【讨论】:

    • 感谢您的快速帮助...但现在我收到错误“未定义函数 YEAR”:(我使用服务器资源管理器在 VS 中运行查询 --pls 建议
    • 这一定是一个误导性的错误消息,因为Year() 在 Access SQL 中始终可用。您有可用的访问权限吗?如果是这样,请先尝试那里的查询......该努力应该提供有关问题性质的更好信息。
    • 当我在 VS 中运行查询时 - 我得到“没有为一个或多个必需参数提供值”
    • @Dipesh 尝试使用NowBETWEEN 进行编辑。还要确保您使用的是 Access Engine,例如accdb12
    • @Dipesh 我在原始版本中有一个逻辑错误。 DateSerial 应该使用 Date() 而不是 IssueDateYear()Month()
    猜你喜欢
    • 2019-11-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-29
    相关资源
    最近更新 更多