-1
Guys, how do I make a condition with DATEDIFF deletes data from a table in 12 in 12 months?
Example:
You are in June 2020 - Then it erases everything before 01/06/2019.
Another example:
You are in December 2021 - Then it erases everything before 01/12/2020.
I was testing it just to see what happens, but it didn’t work..:
Select * from rvr_tb Where ano_mes / 100 < dateadd(yyyy, +1, getdate())
But you end up reading 202104, for example, but I want to show only those you have in column 202004. I can’t define this, because this data changes.
And what it means
ano_mes / 100
? What kind of dataano_mes
? If you wish 1 year ago should not usedateadd(yyyy, -1, getdate())
?– anonimo
@anonimo then, one thing I found wrong and is making it difficult for me to delete the data from the previous year, the ano_mes column is as int. We know that if 202004 is equal to 04/2020, but if I will use this field in functions like date, it no longer works. I don’t know how to convert and I don’t know what I can do.
– Souza
Try using the function
DATEFROMPARTS ( year, month, day )
to create a date field from 3 integers. Or create an integer like(YEAR(GETDATE()) - 1)*100 + MONTH(GETDATE())
.– anonimo
@anonymity but not the 'day' part is only year and month
– Souza
If you want to work with the date type use, for example, 01 for the day.
– anonimo
@anonymity does not work using DATEFROMPARTS
– Souza
SELECT DATEFROMPARTS(2020, 06, 01);
results2020-06-01
.– anonimo
Important you [Dit] your question and explain objectively and punctually the difficulty found, accompanied by a [mcve] of the problem and attempt to solve. To better understand and enjoy the site is worth reading the Stack Overflow Survival Guide in English.
– Bacco