sql get data where date is greater than current month - sql

i want to retrieve records where a date field is set to future months
does this look correct
Select * from table1 WHERE
datesetto >MONTH(dateadd(dd, -1, GetDate())))

select * from tablename where month(columndate)>month(getdate()) and
year(columndate)>=year(getdate())

SELECT * FROM table1
WHERE datesetto >= DATEADD(month, DATEDIFF(month, 0, getdate())+1, 0)
Explanation:
DATEDIFF(month, 0, getdate()) calculates how many months have passed since 1900-01-01.
DATEADD(month, DATEDIFF(month, 0, getdate()), 0) returns the beginning of this month.
DATEADD(month, DATEDIFF(month, 0, getdate())+1, 0) returns the beginning of next month.

Try this:
Select * from table1 WHERE
((DATEPART(MONTH,datesetto) > DATEPART(MONTH,GETDATE())
AND DATEPART(YEAR,datesetto) = DATEPART(YEAR,GETDATE()))
OR (DATEPART(YEAR,datesetto) > DATEPART(YEAR,GETDATE())))
DATEPART(Month,GETDATE()) will give the month of the current date and then you can compare it with the datesetto
Update: The above query will give data for any months greater than the current month and any month greater in the year than the current year.

Related

Get records of last 2 months (current year), and last month(last year)

I need to get records of last 2 months and last month(last year) based on my table field paidDate, using SQL server 2016.
Suppose, I run the query on Feb 1st/2nd, 2020. I need the monthly data from December 2019, January 2020, as well as January 2019.
What's the SQL query for this? Is it possible to club all of these scenario into one?
Then for the previous 2 months the paidDate would be :
A) Higher or equal than the first day of 2 months ago
B) Lower than the first day of the current month.
Similar for the month of a year ago.
So try something like this:
SELECT *
FROM YourTable
WHERE
(
paidDate >= DATEADD(month, -2, DATEADD(month, DATEDIFF(month, 0, GetDate()), 0))
AND paidDate < DATEADD(month, DATEDIFF(month, 0, GetDate()), 0)
)
OR
(
paidDate >= DATEADD(month, -13, DATEADD(month, DATEDIFF(month, 0, GetDate()), 0))
AND paidDate < DATEADD(year, -1, DATEADD(month, DATEDIFF(month, 0, GetDate()), 0))
)
LukStorm has the better answer in terms of performance (and I've upvoted it). But if you want complete months and don't care about indexing, then I would suggest datediff():
where datediff(month, paiddate, getdate()) in (1, 2, 13)
This gets the complete months that are 1 month, 2 months, and 13 months in the past.
You can try the logic as below-
SELECT *
FROM your_table
WHERE
(
YEAR(paidDate) = YEAR(DATEADD(MM,-1, getdate()))
AND
MONTH(paidDate) = MONTH(DATEADD(MM,-1, getdate()))
)
OR
(
YEAR(paidDate) = YEAR(DATEADD(MM,-2, getdate()))
AND
MONTH(paidDate) = MONTH(DATEADD(MM,-2, getdate()))
)
OR
(
YEAR(paidDate) = YEAR(DATEADD(MM,-13, getdate()))
AND
MONTH(paidDate) = MONTH(DATEADD(MM,-13, getdate()))
)

SQL 15th of the month for Next Year

SQL query to get the 15 of the month for the following year.
Today
select getdate() = 2017-08-23 17:05:24.143
Looking for: 2018-8-15 00:00:00
I know how to get a year from today:
select dateadd(year,1,datediff(day,0,getdate()))
I know how to get the beginning of the month:
SELECT DATEADD(month, DATEDIFF(month, 0, GETDATE()), 0)
However I am having trouble combining the two.
You can use datefromparts for SQL Server versions 2012 and above.
select datefromparts(year(getdate())+1,month(getdate()),15)
Truncate the current day to the start of the month with the code you have, but add 12 months (so, a year), and add 14 days.
select dateadd(day,14,dateadd(month, 12+datediff(month, 0, getdate()), 0))
SELECT DATEADD(month, DATEDIFF(month, 0, DATEADD(year, 1, DATEDIFF(day, 0, GETDATE())), 0)
Vamsi Prabhala's answer deserves to be accepted.
But, you should consider creating a Calendar Table because it greatly simplifies working with dates in general.
Here is a pretty simply query that yields the results that you want:
select * from Calendar C where C.year = datepart(year, getdate()) + 1
and C.day_of_month = 15 and C.month = datepart(m, getdate())
Rextester Demo

SQL: Selecting current month last year

I currently select data from the current month LAST year. This works fine with
SELECT cast(CREATED as date) AS DATE, COUNT([ID]) AS BOOKINGS, SUM([TOTCOST]) AS Sales
from BOOKING
WHERE
CREATED >= DATEADD(month, datediff(month, 0, getdate())-12, 0)
AND
CREATED < DATEADD(month, datediff(month, 0, getdate())-11, 0)
GROUP BY cast(CREATED as date)
ORDER BY cast(CREATED as date)
However, rather than return 1st to 31st July, I would like to return 2nd July 2014 to 1st August 2014 inclusive. So effectivly +1 day on that current month.
Is this possible?
Yes, simply add 1 to the result of your calculation.
SELECT cast(CREATED as date) AS DATE, COUNT([ID]) AS BOOKINGS, SUM([TOTCOST]) AS Sales
from BOOKING
WHERE
CREATED >= (DATEADD(month, datediff(month, 0, getdate())-12, 0) + 1)
AND
CREATED < (DATEADD(month, datediff(month, 0, getdate())-11, 0) + 1)
GROUP BY cast(CREATED as date)
ORDER BY cast(CREATED as date)

Select all where date in Last month sql [duplicate]

This question already has answers here:
Get the records of last month in SQL server
(23 answers)
Closed 9 years ago.
How can I get last month date like
select * from table where date in ( last month )
I dont want the last 30 days
AND how can I get last month automatically
Assuming you want all items where the date is within the last month i.e. between today and 30/31 days ago:
Select *
From Table
Where Date Between DATEADD(m, -1, GETDATE()) and GETDATE()
You can use this
Select * from table where date between #startdate and #enddate
Or
SELECT * FROM DATE_SAMPLE WHERE
DATEPART(YEAR, SAMPLE_DATE) = '2013' AND
DATEPART(MONTH,SAMPLE_DATE) = '01' AND
DATEPART(DAY, SAMPLE_DATE) = '01'
Is it usefull for you?
select * from table
where month(date)=month(getdate())-1
Edit
if you mean last month from today. or previous month from a specific date then you need to do something like this
SELECT DATEPART(MONTH, DATEADD(MONTH, -1, [Date]))
Or to get records from previous month of the year you can do something like this
SELECT * FROM Table
WHERE MONTH(Date) = DATEPART(MONTH, DATEADD(MONTH, -1, [Date]))
AND YEAR(Date) = DATEPART(YEAR, DATEADD(MONTH, -1, [Date])) --<-- or pass year for which year you are checking
To make your aquery SARGable (Suggested by t-clausen.dk)
select * from table
where date >=dateadd(m, datediff(m, 0, current_timestamp)-1, 0)
and date < dateadd(m, datediff(m, 0, current_timestamp)-1, 0)
Read here more about sargable Queries when working with date/datetime datatypes.
You can try to use the between statement to get the specific dates:
Select * from table where date between '01-01-2014' and '14-01-2014'

How to display Previous month total dates in sequence

I want to display Previous month dates.May i know the query which is used to display all dates
Expected Output:
Current date = '2012-09-13'
I want to display my result as
1
2
3
4
,
,
,
,
31
these dates should come from month 8
try this:
SELECT NUMBER
FROM MASTER..SPT_VALUES
WHERE TYPE='P'
AND NUMBER BETWEEN
DATEPART(DD,DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE())-1,0))
AND DATEPART(DD,DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()),-1))
Replcae Getdate() with your date
Please try:
;WITH DATES (date)
AS(
SELECT DATEADD(month, DATEDIFF(month, 0, dateadd(month,-1,getdate())), 0)
UNION ALL
SELECT DATEADD(DAY,1,date)
FROM DATES
WHERE DATEADD(DAY,1,date)<=DATEADD(month, DATEDIFF(month, 0, getdate()), 0)-1
)SELECT DAY(date) AS DAYS FROM DATES