如何在 SQL Server 2017 中使用 dateadd 函数

How to use dateadd function in SQL Server 2017

我正在尝试使用 DATEADD 函数在 SQL 服务器中添加字符类型。

我要变量下个月的25号

例子

DECLARE @Date char(6)
SET @Date = '201712'

我要结果 = 20180125

DECLARE @Date char(6)
SET @Date = '201801'

我要结果 = 20180225

提前致谢

通过使用 EOMONTH 函数将“01”添加到 yyyymm 字符串并向其添加 25 天来获取月末日期。

select dateadd(day,25,eomonth(@date+'01'))

这个怎么样?

dateadd(month, 1, dateadd(day, 25, cast(@date + '01' as date)))

您可以将其缩短为:

dateadd(month, 1, cast(@date + '25' as date))