简体   繁体   中英

How to use dateadd function in SQL Server 2017

I am trying to add character type in SQL Server using DATEADD function.

I want the 25th of the next month of the variable.

Example

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

I want result = 20180125

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

I want result = 20180225

Thanks in advance

通过使用EOMONTH函数在yyyymm字符串中添加'01'并添加25天来获取月份结束日期。

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

How about this?

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

You can shorten this to:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM