[英]How to get end of month date from a “YearMonth” varchar column?
We have YearMonth VARCHAR(10) column and I am trying to create a date column which will give me last date of the month based on the YearMonth column. 我们有YearMonth VARCHAR(10)列,并且我试图创建一个date列,该列将根据YearMonth列为我提供该月的最后日期。
For Example:
201612 - 12/31/2016
201602 - 02/29/2016
201702 - 02/28/2017
Assume your varchar is 201612 假设您的varchar是201612
Select EOMonth(cast(YourVarchar+'01' as date))
Returns 退货
2016-12-31
If you wanted the MM/DD/YYYY format 如果您想要MM / DD / YYYY格式
Select Convert(varchar(10),EOMonth(cast(YourVarchar+'01' as date)),101)
If you are using a version of SQL before 2012: 如果您使用的是2012年之前的SQL版本:
Add a month to the date with DATEADD, then subtract a day also with DATEADD 使用DATEADD向日期添加一个月,然后也使用DATEADD减去一天
SELECT DATEADD(DAY, -1, DATEADD(MONTH, 1, CONVERT(DATETIME, '201612' + '01')))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.