比较没有当前月份的 SQL 日期
我在 Access 中有一个表,其中包含字符串列和日期列。我想获取表中日期低于 2010 年 10 月 22 日(本月除外)的所有行。所以,我需要从 30.09.2010 到...的行
我绑定了一些东西,但我发现这是不对的:
SELECT name FROM table WHERE YEAR(date)<=2010 AND MONTH(date)<10
但是这个解决方案不好,我没有其他想法。您能帮我提供一个通用的解决方案吗? 谢谢
I have a table in Access with string columns and a date column. I want to get all the rows from the table when the date is lower than 22.10.2010, except this month. So, i need the rows from 30.09.2010 to ...
I tied something, but I figured out it's not right:
SELECT name FROM table WHERE YEAR(date)<=2010 AND MONTH(date)<10
But this solution it's not good, and i have no other idea. Could you help me with a general solution, pls?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
一个月的第 0 天是上个月的最后一天:
因此:
The zeroth day of a month is the last day of the previous month:
Therefore:
SELECT name FROM table WHERE ( YEAR(date)<2010 ) OR ( YEAR(date)=2010 AND MONTH(date)<10 )
SELECT name FROM table WHERE ( YEAR(date)<2010 ) OR ( YEAR(date)=2010 AND MONTH(date)<10 )
使用权?
将年、月和“1”串在一起(以获取
date
月份的第一天)并将其转换为Date
。SQL
从日期中减去
DAY
(减一)即可得到该月的第一天。然后返回小于该值的所有行。Access?
String together the year, month, and "1" (to get the first day of the month of
date
) and convert that to aDate
.SQL
Subtract the
DAY
(minus one) from the date in question from the date to get the first of the month. Then return all rows less than this value.您减去到目前为止该月的天数并使用该日期进行比较
上面的查询将为您提供直到上个月最后一天结束时的所有记录。
干杯。
you subtract the number of days in the month so far and use that date for the comparison
The above query will give you all the records till the end of the last day of the last month.
Cheers.
可以使用以下功能。这些甚至适用于闰年。
'如果你通过#2016/01/20#,你会得到#2016/01/31#
'如果你通过#2016/01/20#,你会得到31
The below functions can be used. These even work for leap years.
'If you pass #2016/01/20# you get #2016/01/31#
'If you pass #2016/01/20# you get 31