如何获取 SQL Server 2008 的最后一个日期

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26651365/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 02:53:29  来源:igfitidea点击:

How to get last date of month SQL Server 2008

sqlsql-serversql-server-2008

提问by Phattharawit Sombatrat

I created a function "ufngetFirstDateOfMonth" and "ufngetLastDateOfMonth" stored in Microsoft SQL Server 2008. My purpose is to send some date into the function and it will return the first date of month with '00:00:00' or the last date of month with '23:59:59'.

我创建了一个存储在 Microsoft SQL Server 2008 中的函数“ufngetFirstDateOfMonth”和“ufngetLastDateOfMonth”。我的目的是将一些日期发送到函数中,它将返回带有“00:00:00”或最后日期的月份的第一个日期'23:59:59' 的月份。

I call the function like this:

我这样调用函数:

exec ufngetLastDateOfMonth('2014-10-15')  

and normally it returns '2014-10-31 23:59:59'
but when I send the last date of months that have 31 days (august, january,...):

通常它会返回 '2014-10-31 23:59:59'
但是当我发送有 31 天(八月、一月...)的月份的最后日期时:

exec ufngetLastDateOfMonth('2014-10-31')

it return '2014-10-30 23:59:59' whick is not correct Actally, it should be '2014-10-31 23:59:59'

它返回 '2014-10-30 23:59:59' 这不正确实际上,它应该是 '2014-10-31 23:59:59'

Something goes wrong here...

这里出了点问题...

This is my function:

这是我的功能:

CREATE FUNCTION [dbo].[ufnLastDateOfMonth](@Date date)
RETURNS varchar(50)

AS
BEGIN

DECLARE @New_Date varchar(50)

select @New_date = cast(dateadd(dd,-(DAY(@Date )),DATEADD(mm,1,@Date ))as varchar(50)) + ' 23:59:59'

RETURN @New_Date

END

回答by Tanner

To get the last day you can do this:

要获得最后一天,您可以这样做:

SELECT DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,'2014-08-12')+1,0))

Adding to your function:

添加到您的功能:

select @New_date = DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,@date)+1,0))

Source:

来源:

SQL SERVER – Find Last Day of Any Month – Current Previous Next

SQL SERVER - 查找任何一个月的最后一天 - 当前上一个下一个

回答by Syakur Rahman

For those who are using SQL Server 2012, EOMONTHfunction could be an alternative.

对于那些使用 SQL Server 2012 的人来说, EOMONTH函数可能是另一种选择。

DECLARE @date DATETIME = '12/1/2011';
SELECT EOMONTH ( @date ) AS Result;
GO

Source: https://msdn.microsoft.com/en-us/library/hh213020.aspx

来源:https: //msdn.microsoft.com/en-us/library/hh213020.aspx

回答by Gordon Linoff

Go to the first day of the month. Add one month. Then subtract one day. Or, in your case, one second:

转到该月的第一天。加一个月。然后减去一天。或者,就您而言,一秒钟:

select @New_date = dateadd(second, -1, dateadd(month, 1, dateadd(day, -(DAY(@Date) + 1)) ) )

You should use convert()if you want this as a string. I would instead suggest that the function return a datetime.

convert()如果你想把它作为一个字符串,你应该使用。相反,我建议该函数返回一个datetime.

回答by Manish Sharma

Use this

用这个

DECLARE @curdate datetime;
SET @curdate = GETDATE(); -- you can pass your date here
--SET @curdate = '2014-10-15'; Example
SELECT DATEADD(MONTH,1+DATEDIFF(MONTH,0,@curdate),-1);

OR

或者

SELECT DATEADD(MONTH,1+DATEDIFF(MONTH,0,GETDATE()),-1);

回答by Khwahish Batra

you can try this

你可以试试这个

select DATEADD(d, -1, DATEADD(m, DATEDIFF(m, 0, GETDATE()) + 1, 0)) As EndDateOfCurrentMonth

or this

或这个

select DATEADD(month, DATEDIFF(month, 0, GETDATE()), 0) As StartDateOfCurrentMonth

回答by Md Shahriar

Just try this:

试试这个:

SELECT EOMONTH('2000/2/1') Last_Date

Note:EOMONTHfunction is available in SQL Server version >= 2012

注意:EOMONTH函数在SQL Server 版本 >= 2012 中可用