如何在 SQL 中为 CURRENT_TIMESTAMP 添加月份?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2424110/
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 05:39:06  来源:igfitidea点击:

How do I add months to a CURRENT_TIMESTAMP in SQL?

sqlsql-server

提问by Gerrie Schenck

How can I add months to the CURRENT_TIMESTAMP in SQL Server?

如何向 SQL Server 中的 CURRENT_TIMESTAMP 添加月份?

The solution probably lies in DATEADD() but this works with a date only, not a datetime.

解决方案可能在于 DATEADD() 但这仅适用于日期,而不适用于日期时间。

Thanks.

谢谢。

回答by Adriaan Stander

This works perfectly fine

这工作得很好

SELECT DATEADD(month,1,CURRENT_TIMESTAMP)

From DATEADD (Transact-SQL)

来自DATEADD (Transact-SQL)

date

Is an expression that can be resolved to a time, date, smalldatetime, datetime, datetime2, or datetimeoffset value.

日期

是可以解析为时间、日期、smalldatetime、datetime、datetime2 或 datetimeoffset 值的表达式。

回答by Andrew

The Current_Timestamp is the ansi equivalent of GetDate() in SQL, so it is perfetly acceptable to use within a DateAdd function.

Current_Timestamp 是 SQL 中 GetDate() 的 ansi 等效项,因此在 DateAdd 函数中使用它是完全可以接受的。

select dateadd(m,3,current_timestamp)

Adds 3 months to the current timestamp.

将 3 个月添加到当前时间戳。

回答by SAQUIB KHAN

add_months(Tablename,No.of months)

add_months(Tablename,No.of months)