本文介绍了SQL获取本月前三个月的月份的第一天的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图选择当前日期前三个月的第一天。
I am trying to select the first day of the month 3 months before the current date.
例如,如果当前日期为:'2015-11- 08'我的结果希望是:'2015-08-01'
so for example if the current date was: '2015-11-08' my result would want to be: '2015-08-01'
我希望此格式为yyyy-mm-dd。
I would prefer if this was in yyyy-mm-dd format.
我尝试这样开始但没有运气:
I tried this to start with but had no luck:
SELECT DATEADD(dd, -DAY('2015-09-01') + 1, @today)我尝试了很多事情,但似乎无法破解,任何建议或帮助将不胜感激。预先谢谢您
I have tried numerous things but cannot seem to crack it, any advice or help would be appreciated. Thank you in advance
推荐答案逻辑很简单:
- 减去月份中的日期减去日期后的1天
- 减去三个月
在SQL Server中:
In SQL Server:
select dateadd(month, -3, dateadd(day, 1 - day(dte), dte))当前日期:
select cast(dateadd(month, -3, dateadd(day, 1 - day(getdate()), getdate())) as date)并作为字符串:
select convert(varchar(10), dateadd(month, -3, dateadd(day, 1 - day(getdate()), getdate())), 120)更多推荐
SQL获取本月前三个月的月份的第一天
发布评论