First day of the next month
Asked Answered
B

8

33

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...

SELECT OrderDate
FROM Orders
WHERE OrderDate < (code for first day of the next month?)
Beckman answered 25/3, 2014 at 1:43 Comment(2)
please search SO before posting a question: asked many times previouslyMarlenmarlena
So you can have orders in the future? Just want to check that you do intend to support post-dating these and that you're not making the problem harder than it needs to be when you could just look at current_timestamp.Antoinetteanton
R
76

First day of next month:

sql-server 2012+

DATEADD(d, 1, EOMONTH(current_timestamp))

sql-server 2008 and older:

DATEADD(m, DATEDIFF(m, -1, current_timestamp), 0)
Riposte answered 25/3, 2014 at 8:30 Comment(0)
P
2

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)
Peshitta answered 25/3, 2014 at 1:46 Comment(1)
I think he wants first day of next month, but for that all you need to do is add a +1 after the closing paren for the DATEDIFF() call, so close enough.Antoinetteanton
D
2
SELECT DATEADD(month, DATEDIFF(month, 0, getdate())+1, 0) AS StartOfMonth
Dali answered 5/4, 2017 at 5:32 Comment(0)
K
2

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.

Formula - 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))))

Kopeisk answered 29/1, 2020 at 7:1 Comment(0)
M
1

Try this

SELECT OrderDate
FROM Orders 
WHERE ORDERDATE < DATEADD(dd,-(DAY(DATEADD(mm,1,getdate()))-1),DATEADD(mm,1,getdate()))

Take a look at here

Mousetail answered 25/3, 2014 at 3:17 Comment(0)
E
0
SELECT OrderDate FROM Orders WHERE orderdate < (LAST_DAY(CURRENT DATE) + 1)
Eumenides answered 21/8, 2018 at 14:58 Comment(2)
While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value.Hooghly
While this code may answer the question, it is better to explain how to solve the problem and provide the code as an example or reference. Code-only answers can be confusing and lack context.Begird
A
0
    Select Convert(date,Dateadd(dd,1 - DATEPART(dd,getdate()), DATEADD(mm,1,getdate())),103)
Antibiotic answered 15/8, 2019 at 15:59 Comment(1)
While this code may/may not solve the question, including an explanation of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please edit your answer to add explanations.Sawyor
G
0

For sql-server 2012 ->

Using DateFromParts will do the trick. +1 to get the next month

SELECT OrderDate
FROM Orders
WHERE OrderDate < DATEFROMPARTS(YEAR(Getdate()),MONTH(Getdate())+1,1)

https://learn.microsoft.com/en-us/sql/t-sql/functions/datefromparts-transact-sql

Grogram answered 3/5, 2022 at 11:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.