简体   繁体   English

访问 SQL 查询 - 从每月的特定日期选择日期范围

[英]Access SQL query - select date range from specific day of the month

I have been scratching my head over this for a while, but i would like to make a query that selects values from the 24th of the current / last month to the 24th of the next/current month depending on the month we are in - this is basically from one payroll to the next.我一直在为此挠头一段时间,但我想进行一个查询,根据我们所在的月份,从当前/上个月的 24 日到下个月/当月的 24 日之间选择值 - 这个基本上是从一份工资单到下一份工资单。 any suggestions on the SQL query?关于 SQL 查询的任何建议?

So for instance - if the current date is the 20th March then i would want all data from 24th Feb to the 24th March, but if the date was over the 24th of this month say 30th March then i would want data from 24th March to 24th April - if that makes sense.因此,例如 - 如果当前日期是 3 月 20 日,那么我想要从 2 月 24 日到 3 月 24 日的所有数据,但如果日期超过本月 24 日,比如说 3 月 30 日,那么我想要从 3 月 24 日到 24 日的数据四月 - 如果这有意义的话。

Thanks, Phill谢谢,菲尔

Just subtract 23 days and extract the year and the month:只需减去 23 天并提取年份和月份:

select year(dateadd("d", -23, datecol)) as year,
       month(dateadd("d", -23, datecol)) as month

With this expression:用这个表达:

iif(
  day(date()) >= 24,
  dateserial(year(date()), month(date()), 24),
  dateserial(year(dateadd('m', -1, date())), month(dateadd('m', -1, date())), 24)
)

you can get the starting date of your condition (though I'm not sure about the >= sign, maybe it should by just > ? ), so use it like this:你可以得到你的病情的开始日期(虽然我不确定>=符号,也许它应该只是> ? ),所以像这样使用它:

select t.col1, t.col2, ... 
from (
  select *, 
    iif(
      day(date()) >= 24,
      dateserial(year(date()), month(date()), 24),
      dateserial(year(dateadd('m', -1, date())), month(dateadd('m', -1, date())), 24)
    ) as start_date
  from tablename
) as t
where t.date_column between t.start_date and dateadd('m', 1, t.start_date)

For a slimmer method, use DateAdd and DateDiff :对于更精简的方法,请使用DateAddDateDiff

Select *
From YourTable
Where DateDiff('m', DateAdd('d', -23, [YourDateField]), DateAdd('d', -23, Date())) = 0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM