How to select current dates from current month

非 Y 不嫁゛ 提交于 2020-01-15 03:32:06

问题


I would like to retrieve the data between 1 -30 of the current month [ i am using MSACCESS Dbase to do so] Below is the query that i am trying --

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

Data that i am holding in my MSACCESS Dbase -

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

so output should have only 2 records but my query is giving no results


回答1:


Here is the query I think you need. All the functions it uses are always available in Access SQL regardless of whether the query is run from within an Access session or from without (as in your c# situation).

The db engine will evaluate both those DateSerial expressions once, then use their results to filter the result set. This approach will be especially fast with an index on 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;

Here is an Access Immediate window session which explains the logic for those DateSerial expressions ...

? 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 


来源:https://stackoverflow.com/questions/18669319/how-to-select-current-dates-from-current-month

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!