TSQL 只需要返回月份的最后一天,如何删除年份、月份和日期?时间?

发布于 2024-09-08 13:54:37 字数 559 浏览 7 评论 0原文

我正在 T-SQL 中编写一个函数,无论日期输入如何,都会返回该月的最后一天。

这是我的代码:

Alter Function dbo.FN_Get_Last_Day_in_Month2
(@FN_InputDt    Datetime)
Returns smalldatetime
as 
Begin

Declare @Result  smalldatetime
Set    @Result =

       case when @FN_InputDt <> 01-01-1900 then 
       DATEADD(m, DATEDIFF(M, 0,@FN_InputDt)+1, -1)
       Else 0 End

Return @Result

End 

代码无法正常工作,这是显示不良行为的测试:

SELECT dbo.fn_get_last_day_in_month (07-05-2010)

这是(不正确的)结果:

2010-07-31 00:00:00

I am writing a function in T-SQL returning the last day of the month regardless of the date input.

Here is my code:

Alter Function dbo.FN_Get_Last_Day_in_Month2
(@FN_InputDt    Datetime)
Returns smalldatetime
as 
Begin

Declare @Result  smalldatetime
Set    @Result =

       case when @FN_InputDt <> 01-01-1900 then 
       DATEADD(m, DATEDIFF(M, 0,@FN_InputDt)+1, -1)
       Else 0 End

Return @Result

End 

The code is not working correctly, here is a test that shows the bad behavior:

SELECT dbo.fn_get_last_day_in_month (07-05-2010)

Here is the (incorrect) result:

2010-07-31 00:00:00

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

凹づ凸ル 2024-09-15 13:54:37

2010年7月5日...5月7日或7月5日是什么?您需要使用安全的日期格式,请查看为 SQL Server 设置标准日期格式

示例来自 如何查找年、月等中的第一天和最后一天

DECLARE @d DATETIME
SET @d = '20100705'  -- notice ISO format

SELECT
    DATEADD(yy, DATEDIFF(yy, 0, @d), 0) AS FirstDayOfYear,
    DATEADD(yy, DATEDIFF(yy, 0, @d)+1, -1) AS LastDayOfYear,
    DATEADD(qq, DATEDIFF(qq, 0, @d), 0) AS FirstDayOfQuarter,
    DATEADD(qq, DATEDIFF(qq, 0, @d)+1, -1) AS LastDayOfQuarter,
    DATEADD(mm, DATEDIFF(mm, 0, @d), 0) AS FirstDayOfMonth,
    DATEADD(mm, DATEDIFF(mm, 0, @d)+1, -1) AS LastDayOfMonth,
    @d - DATEDIFF(dd, @@DATEFIRST - 1, @d) % 7 AS FirstDayOfWeek,
    @d - DATEDIFF(dd, @@DATEFIRST - 1, @d) % 7 + 6 AS LastDayOfWeek

仅用于日使用日期或日期部分

 select DAY(getdate()),
     DATEPART(dd,GETDATE())

What is 07-05-2010...May 7th or July 5th? You need to use a safe date format, take a look at Setting a standard DateFormat for SQL Server

example from How to find the first and last days in years, months etc

DECLARE @d DATETIME
SET @d = '20100705'  -- notice ISO format

SELECT
    DATEADD(yy, DATEDIFF(yy, 0, @d), 0) AS FirstDayOfYear,
    DATEADD(yy, DATEDIFF(yy, 0, @d)+1, -1) AS LastDayOfYear,
    DATEADD(qq, DATEDIFF(qq, 0, @d), 0) AS FirstDayOfQuarter,
    DATEADD(qq, DATEDIFF(qq, 0, @d)+1, -1) AS LastDayOfQuarter,
    DATEADD(mm, DATEDIFF(mm, 0, @d), 0) AS FirstDayOfMonth,
    DATEADD(mm, DATEDIFF(mm, 0, @d)+1, -1) AS LastDayOfMonth,
    @d - DATEDIFF(dd, @@DATEFIRST - 1, @d) % 7 AS FirstDayOfWeek,
    @d - DATEDIFF(dd, @@DATEFIRST - 1, @d) % 7 + 6 AS LastDayOfWeek

for just the day use day or datepart

 select DAY(getdate()),
     DATEPART(dd,GETDATE())
謸气贵蔟 2024-09-15 13:54:37

将返回值转换为 SQL 日期时间类型,然后调用“DAY”函数以整数形式获取日期。请参阅此处的函数参考:

http://msdn.microsoft.com/en- us/library/ms176052.aspx

不确定您正在使用哪个数据库,但这应该是所有数据库的标准函数。

Cast the return value to a SQL datetime type, and then call the "DAY" function to get the day in as an integer. See the function reference here:

http://msdn.microsoft.com/en-us/library/ms176052.aspx

Not sure which database you're using, but this should be a standard function across all databases.

生生漫 2024-09-15 13:54:37

我会返回一个 DATETIME,我过去在 SMALLDATETIME 方面遇到过麻烦。

DECLARE @Result DATETIME

SET @Result = DATEADD(m , 1, @FN_Input);

RETURN CAST(FLOOR(CAST(DATEADD(d, DATEPART(d, @Result) * -1, @Result) AS FLOAT)) AS DATETIME)

另外,我认为您可能是 SQL 完全无视日期格式的受害者。总是,总是,总是,在测试 SQL 函数中输入字符串时,请使用以下格式;

'05 Jul 2010'

您的函数可能有效,但它将您的日期解释为 7 月 5 日 - 而不是 5 月 7 日。

I'd return a DATETIME, I've had trouble with SMALLDATETIME in the past.

DECLARE @Result DATETIME

SET @Result = DATEADD(m , 1, @FN_Input);

RETURN CAST(FLOOR(CAST(DATEADD(d, DATEPART(d, @Result) * -1, @Result) AS FLOAT)) AS DATETIME)

Also, I think you may be a victim of SQL's complete disregard of date formatting. Always, always, always, when typing a string into test a SQL function use the following format;

'05 Jul 2010'

Your function probably works but it interpreted your date as 5th July - not 7th May.

听,心雨的声音 2024-09-15 13:54:37
DECLARE @date DATETIME = '20130624';
SELECT Day(EOMONTH ( @date )) AS LastDay;
GO
DECLARE @date DATETIME = '20130624';
SELECT Day(EOMONTH ( @date )) AS LastDay;
GO
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文