简体   繁体   English

如何从SQL中的给定月份和年份中获取月份的第一天和最后一天

[英]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.

相关问题 从年份和月份变量获取月份的第一天和最后一天 - Get First & Last Day of Month from Year & Month variables 在 MariaDb 查询中,给定年份、月份和星期,如何获取一周中的第一天的星期几? - How to get the day of month, of the first day of a week, given the year the month and the week in a MariaDb query? 如何通过输入月份和年份来获取 mysql 中月份的最后一天 - How to get last day of month in mysql by giving month and year as input 如何在 sql 的查询之间添加一个月的第一天和最后一天? - How to add first day and last day of month in between query in sql? 有没有办法获得上个月的第一天和当月的最后一天? - Is there a way to get the First day of the previous month and Last day of current month? SQL-一个月的最后一天 - SQL - Last Day of Month 如何从格式化的列日期中获取特定的日期月份或年份 - how to get specific day month or year from a column date formated 如何从日期中提取日、月和年? - How to extract the Day, Month and Year from the date? PHP,按月/日/年按月间隔从表中获取事件 - PHP, Get event from table in month interval by day/month/year SQL:查询以获取用户在给定月份、年份的至少 1 天内出现的行 - SQL: Query to get rows where the user was present for at least 1 day of a given month, year
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM