SQL获取本月前三个月的月份的第一天

编程入门 行业动态 更新时间:2024-10-23 05:41:26
本文介绍了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获取本月前三个月的月份的第一天

本文发布于:2023-10-16 00:22:32,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1495920.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:个月   前三   SQL

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!