I am trying to write 2 queries to delete records where dates are greater than a certain date:
The first one:
delete from RPT_HistSnapEng_temp
where ForecastDate> DATEADD(WEEK,7,CAST(GETDATE() AS DATE))
This query deletes records when forecastdate is greater than 7 weeks from today
The second one is:
delete from RPT_HistSnapEng_temp
where ForecastDate< DATEADD(WEEK,-6,CAST(GETDATE() AS DATE))
This query deletes records when forecastdate is less than 6 weeks from today.
So basically, this should filter out records from Dec 2015 - Nov 2016 and only show records from previous 6 weeks and next 7 weeks from today.
Even though the query runs, its not deleting records. I cannot hardcode dates because I will be using this query on a rolling basis inside a SSIS package.
Your current where clauses are trying to grab records that are both less than a date in the past AND greater than a date in the future. I think you (and the other answer) should be using or.
But, since this looks like a temp table that you are loading to then report with, I would adjust your insert to simply grab the records you are looking for, rather than loading more than you need and then deleting.
select *
from RPT_HistSnapEng -- base table name?
where cast(ForecastDate as date) between dateadd(week, -6, cast(current_timestamp as date)) and dateadd(week, 7, cast(current_timestamp as date))
Just add your insert to that if it gets the records you need.
However, to directly answer your question about deletes, you can change this query to simply use NOT between:
delete
from RPT_HistSnapEng_temp
where cast(ForecastDate as date) not between dateadd(week, -6, cast(current_timestamp as date)) and dateadd(week, 7, cast(current_timestamp as date))
As you can see, I like the use of between (which is inclusive of the date arguments) for this type of range check rather than getting caught up in using >= and < or confusing the and and or which you've seemingly done. I also like the ANSI standard current_timestamp over the t-sql specific getdate() but they are equivalent.
Try "ww" or "wk" instead of "week" in the dateadd function. Try a SELECT statement to get the records you want to delete:
SELECT ID, ForecastDate
FROM RPT_HistSnapEng_temp
WHERE CAST(ForecastDate AS DATE) > DATEADD(ww,7,CAST(GETDATE() AS DATE))
OR CAST(ForecastDate AS DATE) < DATEADD(WEEK,-6,CAST(GETDATE() AS DATE))
ORDER BY ForecastDate
To Delete just remove the SELECT and the ORDER BY:
DELETE
FROM RPT_HistSnapEng_temp
WHERE CAST(ForecastDate AS DATE) > DATEADD(ww,7,CAST(GETDATE() AS DATE))
OR CAST(ForecastDate AS DATE) < DATEADD(WEEK,-6,CAST(GETDATE() AS DATE))
Related
So what I am trying to do is when I run the query, I want to return all records that were in the month two months from the current month. For example, lets say the current month is November, when the query runs, I want returned all records from September and only September. If I run the query in lets say October, I want all records from August and only August. I am trying to do this in MS SQL. Thanks for any advice.
In SQL Server, you can use:
where datecol >= dateadd(month, -3, datefromparts(year(getdate()), month(getdate()), 1)) and
datecol < dateadd(month, -2, datefromparts(year(getdate()), month(getdate()), 1))
This is index- and optimizer- friendly. If you don't care about performance, you can use datediff():
where datediff(month, datecol, getdate()) = 2
This can be done in a nice 1 liner.
WHERE NOW() BETWEEN Date1 AND Date2;
You can have the month part in a variable and then it can be used in the Where clause to filter the month part of the date value is equal to the varoable value.
Query
Declare #month as int;
Set #month=datepart(month, getdate()) - 2;
Select * from yourTableName
Where month(dateCol) = #month;
The function GETDATE() can be used to retrieve the current month.
The function DATEADD(datepart,number,date) can be used to perform operations on dates. For more info look at the official docs
Thus, to retrieve the records from two months before (-2) the current month you can use the following:
DATEADD(month, -2, GETDATE())
In conclusion an example query to select all records that were in the month two months from the current month:
SELECT * FROM table
WHERE MONTH(month_column) = DATEADD(month, -2, GETDATE())
sources:
WHERE Clause to find all records in a specific month
SQL query for today's date minus two months
I have data prep procedure in SQL. I want to have data preparation at the end of every month
Say I want the procedure run on last day of month e.g. on 31 January 2020 it should prep data from 1 January to 31 January.
So it's kind of moving window over all months of the year. Because I need data for evaluation at the end of each month.
I tried this, however, this does not give automation. Its sort of manual running end of every month
select '2020-10-01' as beginDate_AnalysisWindow
, '2020 -01-31' as endDate_AnalysisWindow
into #AnalysisWindow --create temporary table #AnalysisWindow
I also tried the following, however, I’m not sure if it does for the whole month or just one day?
SELECT START_OF_MONTH_DATE AS beginDate_AnalysisWindow
,END_OF_MONTH_DATE AS endDate_AnalysisWindow
INTO #AnalysisWindow
FROM [dbo].[Date] WITH (NOLOCK)
WHERE DATE = DATEADD(dd, - 1, CAST(GETDATE() AS DATE))
Could someone pls help me/give me some suggestions.
Thanks in advance
If you want the last day of the current month, use eomonth():
WHERE DATE = CONVERT(date, EOMONTH(GETDATE()))
Note: This assumes that the date column has no time component. If that is the case:
WHERE CONVERT(date, DATE) = CONVERT(date, EOMONTH(GETDATE()))
SQL Server will still use an index (if available) even for this type of conversion.
EDIT:
To get the current months data, one method is:
WHERE DATE <= CONVERT(date, EOMONTH(GETDATE())) AND
DATE > CONVERT(date, EOMONTH(GETDATE(), -1))
The second argument to EOMONTH() is a months offset.
You can also try:
where getdate() <= EOMONTH(GETDATE()) AND
getdate() > DATEADD(DAY, 1, EOMONTH(GETDATE(), -1))
Instead of getdate(), you can use your date column.
I'm trying to drop all the records from my DB where the records are OLDER than 3 days.
I've written something like this:
delete from Results WHERE [Date] > DATEADD(d, -3, getdate())
I wanted to check whether this sql query is correct since I'm worried that I might make a mistake and delete more than I should...
this this query correct?
Your code is deleting everything newer than 3 days old, and should be:
DELETE FROM Results WHERE [Date] < DATEADD(DAY, -3, GETDATE())
Otherwise, this is fine for deleting everything that is older than exactly 72 hours, although as others have said you should definitely be testing either on a copy of the data or at least by using SELECT statement
SELECT * FROM Results WHERE [date] < DATEADD(DAY, -3, GETDATE())
However, if you want to delete everything that happened before three days ago (e.g. It is now Monday, and I want to delete everything that happened before Friday) you would need to eliminate the time aspect.
DELETE FROM Results WHERE CONVERT(DATE, [date]) < DATEADD(DAY, -3, GETDATE())
Always check WHERE clause in SELECT clause before update or delete statement :
SELECT DATEADD(d, -3, getdate())
First check your WHERE clause in SELECT statement.It gives your expected date means continue with same WHERE clause in DELETE.
I am selecting fields like client id, name, and service date. I am trying to write in my where clause, that every day I run my query it will capture a date range of the first of the current month to the current day.
One method would be to compare the month and the year:
where year(col) = year(getdate()) and
month(col) = month(getdate()) and
day(col) <= day(getdate()) -- this is optional, if there is no future data
Another method would be to compare to the beginning of the month. I would approach this as:
where col >= cast(dateadd(day, 1 - day(getdate), getdate()) as date)
(This assumes there is no future data in the table.)
or, better yet:
where col >= datefromparts(year(getdate()), month(getdate()), 1)
Select *
From YourTable
where SomeDateField bewteen cast(DateAdd(DD,-Day(GetDate())+1,GetDate()) as Date) and cast(getDate() as Date)
and ...
Edit: I used the BETWEEN to trap possible future date/scheduled events
In a table in my datatase I have a datatime column which stores the time at which the record is added. How can I delete all records which are older than a day when I run a stored procedure (considering the current time) ?
You can build a DELETE statement making use of the datediff and the getdate functions.
Usage example:
DELETE FROM yourTable WHERE DATEDIFF(day,getdate(),thatColumn) < -1
When it comes to SQL, you have to specify what you mean by "older than a day".
DATEDIFF: it uses day boundary midnight so run it at 19th October 00:05 and you'll delete rows 6 minutes old (18th October 23:59)
24 hours?
Yesterday midnight? Run code on 19th October, delete rows before 18th?
Also, don't put a function on a column.
This assumes 24 hours to the minute:
DELETE
MyTableWhere
WHERE
MyColumn < DATEADD(day, -1, GETDATE())
This assumes yesterday midnight:
DELETE
MyTableWhere
WHERE
MyColumn < DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()), -1)
Delete <TableName>
Where DATEDIFF(day, <ColumnName>, getdate()) > 0
Raj
I generally advise against actually deleting data from your database because you never know when you may need to go back and recover or rollback to previous records because of data corruption or an audit, etc. Instead I would add an bit column title something like "IsDeleted" and set day old entries to true using an update statement.
Something like
'UPDATE tablename SET IsDeleted = 1 WHERE (DATEDIFF(day,DateCreatedOn,GETDATE()) > 0)
Where DateCreatedOn is where your record's created on or timestamp date would go
Assuming date column to be "RecordCreatedDate"
DELETE FROM yourtable WHERE RecordCreatedDate < DATEADD(d, -1, GETDATE())
I only caution that if your database has millions of rows your should have an index on the RecordCreatedDate column and possibly do smaller batch deletes if you will be removing large amounts of data.
delete from YourTable where DateColumn < getdate()-1
or
ts >= now() - INTERVAL 1 DAY
where ts is a name of the datetime column