We need to filter the last 4 months (as per your comments) from the current month of the system.
We use the function DATEADD(MONTH, -4, CONVERT(date, GETDATE())) to subtract 4 months from our current date and use the CONVERT(date, GETDATE()) to ignore the schedule.
Observing: If you want to consider the schedule, change the CONVERT(date, GETDATE()) for GETDATE(). If you need to change the amount of months for the last 5, 6, 7... months change the parameter -4 for the amount of days (negatively, as we are subtracting the months) desired -5, -6, -7.
We use the function DATEADD(MONTH, -1, CONVERT(DATE, GETDATE())) to ignore the current month.
SELECT *
FROM VIEW_INCD
WHERE DH_CRIA_INCD BETWEEN DATEADD(MONTH, -4, CONVERT(date, GETDATE())) AND DATEADD(MONTH, -1, CONVERT(DATE, GETDATE()))
ORDER BY DH_CRIA_INCD DESC
To run a query just for 1 month as you said in the comments, try running the command below:
SELECT *
FROM VIEW_INCD
WHERE DH_CRIA_INCD BETWEEN DATEADD(mm, DATEDIFF(m, 0, GETDATE()) - 1, 0)
AND DATEADD(s, -1, DATEADD(mm, DATEDIFF(m, 0, GETDATE()), 0))
ORDER BY DH_CRIA_INCD DESC
The first part of BETWEEN we are spending the first day of the previous month (because of the -1).
Observing: If you want to change the add-1, -2, -3 month after the DATEADD function, but the -1 refers to the previous 1 month, the -2 refers to previous 2 months, and so on.
Example: Whereas GETDATE() will return the day 08/08/2018, the function below will return 01/07/2018.
DATEADD(mm, DATEDIFF(m, 0, GETDATE()) - 1, 0)
The second part of BETWEEN we are spending the last day of the previous month.
Observing: If you want to change the add month -1, -2, -3 after the DATEADD function, but the -1 refers to 2 previous months, the -2 refers to 3 previous months, and so on.
Example: Whereas GETDATE() will return the day 08/08/2018, the function below will return 30/06/2018.
DATEADD(s, -1, DATEADD(mm, DATEDIFF(m, 0, GETDATE()) - 1, 0))
You need to see all dates below the current date starting from the previous month?
– Pedro Paulo
all previous months of the current date, one query per month
– guilhermedjc
If you need to inform the subtraction amount every time, isn’t it easier to inform the date you want to see in the case of the month/year? I could not understand very well why it is this way, this is in a Procedure? The query is always done directly in the database ?
– Caique Romero
I’m assembling a Dashboard in PHP, which shows in a table the amount of problems of the current month (I already did), and up to 4 previous months. Yes, straight from the bank.
– guilhermedjc
The problem is that the query I did, it only works if it is the same year, if a person is going to use Dashboard in January next year for example, will only be able to see the current month, and the previous 4 months will be reset because the year is different
– guilhermedjc
Note: it is month by month, quantity of current month: x, quantity of previous month: x, etc..
– guilhermedjc
So is there a limitation of at most 4 months ago? (I ask this, because this information is relevant to form a solution).
– Caique Romero
For this Dashboard yes, will always show the amount of problems of the current month, until 4 previous months
– guilhermedjc