SQL 下个月的第一天
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22623971/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
First day of the next month
提问by jaramore
I'm trying get the results where it only displays OrderDates before the LAST day of the CURRENT month. I'm guessing it would be like this...
我正在尝试获取仅在当前月份的最后一天之前显示 OrderDates 的结果。我猜会是这样的……
SELECT OrderDate
FROM Orders
WHERE OrderDate < (code for first day of the next month?)
回答by t-clausen.dk
First day of next month:
下个月的第一天:
sql-server 2012+
sql-server 2012+
DATEADD(d, 1, EOMONTH(current_timestamp))
sql-server 2008 and older:
sql-server 2008 及更早版本:
DATEADD(m, DATEDIFF(m, -1, current_timestamp), 0)
回答by E.J. Brennan
Your question is somewhat ambiguous, but this will give you '(code for the first day of the month)'
您的问题有些模棱两可,但这会给您“(本月第一天的代码)”
SELECT OrderDate
FROM Orders
WHERE ORDERDATE < DATEADD(month, DATEDIFF(month, 0, GETDATE()), 0)
回答by viju
SELECT DATEADD(month, DATEDIFF(month, 0, getdate())+1, 0) AS StartOfMonth
回答by Vignesh Kumar A
回答by Ramesh Chaudhary
Let's understand the method to get first day of current month, we can fetch day component from the date (e.g. @mydate) using DATEPART and subtract this day component to get last day of previous month. Add one day to get first day of current month. E.g. @mydate = 10/8/2019, subtract day component (8 days) will give us 9/30/2019. Now add one day in this outcome to get first of the current month - 10/1/2019.
让我们了解获取当月第一天的方法,我们可以使用 DATEPART 从日期(例如@mydate)中获取日期组件并减去这一天组件以获得上个月的最后一天。添加一天以获取当月的第一天。例如@mydate = 10/8/2019,减去天部分(8 天)将得到 9/30/2019。现在在这个结果中添加一天以获得当月的第一天 - 10/1/2019。
Formula - DATEADD(day,1,DATEADD(day, -DATEPART(day,@mydate), @mydate))
公式 - DATEADD(day,1,DATEADD(day, -DATEPART(day,@mydate), @mydate))
Now First of the next month- Add one month in above formula DATEADD(month,1,(DATEADD(day,1,DATEADD(day, -DATEPART(day,@mydate), @mydate))))
现在下个月的第一天- 在上面的公式中添加一个月 DATEADD(month,1,(DATEADD(day,1,DATEADD(day, -DATEPART(day,@mydate), @mydate))))
回答by Bobok
SELECT OrderDate FROM Orders WHERE orderdate < (LAST_DAY(CURRENT DATE) + 1)
回答by Prem Kumar
Select Convert(date,Dateadd(dd,1 - DATEPART(dd,getdate()), DATEADD(mm,1,getdate())),103)