1
I have a query in SQL Server that returns me the data from 1 ago
SELECT titulo, categoria
FROM posts
WHERE dataCadastro BETWEEN DATEADD(year,-1,GETDATE()) AND GETDATE()
ORDER BY dataCadastro DESC";
The problem is I need this consultation to get back to me too all data of all months in the range. I want the results to come from the first day of the last month in the range up to the current date.
The problem is that the exact results are coming 1 year ago so the results that are coming since the day 22/01/2017. I need the data provided 01/01/2017.
Use the DATEADD
is best in this case?
"I need the data since 01/01/2017" This date is fixed? If it is, you can set it directly in the code:
BETWEEN CAST('20170101' AS SMALLDATETIME) AND GETDATE()
– rLinhares
No, it’s not a fixed date. I need it to always come from the first day of the last month. If a year ago is 22/01 then I need you to come 01/01. If it was 15/03, I need you to come 01/03.
– DiChrist
How the column is declared
dataCadastro
: date? datetime? another format?– José Diz
@Dichrist would be that? How to show every day between two dates?
– Marconi