获取当前月份的日期范围?
我想创建一个 SQL 语句,稍后在我的代码中使用,该语句获取当前月份的日期范围。
示例:这是八月,所以日期范围是,
StartDate = 08/01/11
EndDate = 08/31/11
但是,如果是二月,
StartDate = 02/01/11
EndDate = 02/28/11
Select *
from mytable
where (check_date >= StartDate) AND (check_date <= EndDate)
感谢您提供的任何帮助
I would like to create a SQL statement to later be used in my code, that gets the date range for the current month.
Example: This is August, so the date range would be
StartDate = 08/01/11
EndDate = 08/31/11
however, if it was February
StartDate = 02/01/11
EndDate = 02/28/11
Select *
from mytable
where (check_date >= StartDate) AND (check_date <= EndDate)
thanks for any help you may be able to give
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用自零以来的月份技巧找到本月的开始。该月的最后一天是一个月后减去一天:
这将打印:
The you can find the start of this month with the months-since-zero trick. The last day of the month is one month later, minus one day:
This prints:
我需要非常类似的东西,从特定日期获取月份的日期范围,这可以在 SQL 搜索中使用 - 这意味着不仅日期必须正确,而且时间必须从 00:00:00 到 23:59:59 如果在 24 小时制中,但只要显示 DATETIME 格式即可,它也适用于 12 小时制。
也许这对某人有用。该解决方案基于 Andomar 的回答:
您将得到类似 2012.01.01 00:00:00 - 2012.01.31 23:59:59 的结果。
I needed very similar thing, to get month's date range from specific date, which was possible to use in SQL search - that means not only dates must be correct, but time must be from 00:00:00 to 23:59:59 if in 24 hour system, but it just matter of displaying DATETIME format, it will work with 12 hours system too.
Maybe it will be useful for someone. This solution is based on Andomar's answer:
You will get result like 2012.01.01 00:00:00 - 2012.01.31 23:59:59.
如果您需要月份范围只是为了检查“check_date”是否属于特定月份,您也许可以使用类似的条件
If you need the month range just for checking if "check_date" belongs to a specific month, you maybe can use a condition like
您可以创建一个返回给定日期的结束日期的函数。
所以你可以传递开始日期的函数,它会像这样
You could create a function that returns the end date for the given date.
So the function you can pass the start date and it will be like this