[英]How to get the First day and last day of the month from given month and year in sql
Hi I have a month number and year eg : month 2 and year 2014 嗨,我有一个月数和年份,例如:第二个月和2014年
how can i get the first day of that month like 2014-02-01 and last day of month 2014-02-28? 如何获得该月的第一天(如2014-02-01)和该月的最后一天2014-02-28?
i have seen many posts on getting first and last date of month based on given date,but i need it based on given month and year 我已经看到很多关于根据给定日期获取月份的第一个和最后一个日期的帖子,但是我需要根据给定的月份和年份获取它
Thanks in advance 提前致谢
In SQL Server 2012 and later 在SQL Server 2012及更高版本中
DECLARE @year int = 2014
DECLARE @month int = 12
SELECT
DATEFROMPARTS ( @year, @month, 1) AS MonthStart,
EOMONTH (DATEFROMPARTS ( @year, @month, 1) ) AS MonthEnd
Results 结果
MonthStart MonthEnd
---------- ----------
2014-12-01 2014-12-31
(1 row(s) affected)
Further details DATEFROMPARTS: http://msdn.microsoft.com/en-gb/library/hh213228.aspx EOMONTH: http://msdn.microsoft.com/en-us/library/hh213020.aspx 其他详细信息日期分区: http : //msdn.microsoft.com/en-gb/library/hh213228.aspx EOMONTH: http : //msdn.microsoft.com/en-us/library/hh213020.aspx
In mysql : 在mysql中:
SELECT DATE_SUB(LAST_DAY(DATE_ADD(NOW(), INTERVAL 1 MONTH)),
INTERVAL DAY(LAST_DAY(DATE_ADD(NOW(), INTERVAL 1 MONTH)))-1 DAY) AS firstOfNextMonth,
LAST_DAY(DATE_ADD(NOW(),
INTERVAL 1 MONTH)) AS lastOfNextMonth
in sql server : 在sql server中:
select convert(varchar,dateadd(d,-(day(getdate()-1)),getdate()),106) 'Date'
union all
select convert(varchar,dateadd(d,-day(getdate()),dateadd(m,1,getdate())),106)
first day of the current month:
本月第一天:
SELECT DATEADD(mm, DATEDIFF(mm, 0, GETDATE()), 0)
To get the last day of the current month:
要获取当前月份的最后一天:
SELECT DATEADD (dd, -1, DATEADD(mm, DATEDIFF(mm, 0, GETDATE()) + 1, 0))
Refernce : Get first and last day of month in SQL 2012 and SQL 2008 引用: 在SQL 2012和SQL 2008中获取每月的第一天和最后一天
Not an answer to the OP, however may be helpful to others 不是对OP的答案,但是可能对其他人有帮助
Solution in postgres: Postgres中的解决方案:
SELECT to_date((2012 || '-' || 9)::text, 'YYYY-MM') AS first_day,
(to_date((2012 || '-' || 9)::text, 'YYYY-MM') + interval '1 month' - interval '1 day')::date AS last_day
On MS SQL Server 2008, the following seems to work: 在MS SQL Server 2008上,以下方法似乎可以正常工作:
DECLARE @Month INT = 10;
DECLARE @Year INT = 2016;
DECLARE @FirstDayOfMonth DATETIME = CAST(@Year AS varchar) + '-' + CAST(@Month AS VARCHAR) + '-1';
DECLARE @LastDayOfMonth DATETIME = DATEADD(d, -1, DATEADD(m, DATEDIFF(m, 0, @FirstDayOfMonth) + 1, 0));
SELECT @FirstDayOfMonth, @LastDayOfMonth; -- 2016-10-01, 2016-10-31
I had the same Problem a while back, so i created and saved the following codes. 前一段时间我遇到了同样的问题,所以我创建并保存了以下代码。 You can substitute the
GetDate()
with your own column name: 您可以用自己的列名替换
GetDate()
:
Select
DATEADD(mm, DATEDIFF(mm, 0, Getdate())-1, 0) as '1st day of last month',
dateadd(day,-(day(Getdate())),Getdate()) as 'last day of last month',
dateadd(mm,datediff(mm,0,Getdate()),0)as '1st day of this month',
dateadd(dd,-1,dateadd(mm,Datediff(mm,0,Getdate())+1,0)) as 'last day of this month',
dateadd(mm,datediff(mm,0,Getdate())+1,0) as '1st day of Next month',
Dateadd(dd,-1,dateadd(mm,datediff(mm,0,Getdate())+2,0))as 'Last day of Next month',
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.