question

mahi222 avatar image
mahi222 asked

Query to exclude 2 days of records based on timestamp

Please help me on this how to exclude two days of data and everyday it should insert todays data for your reference see the below query DELETE A FROM [dbo].[t_account] A JOIN [dbo].[t_DMT_Upload_Account_Log]l ON A.ACCOUNTID=L.ACCOUNTID WHERE datediff(d,getdate(),[DateStamp])<=2
datetime
2 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

David Wimbush avatar image David Wimbush commented ·
What data type is that DateStamp column?
0 Likes 0 ·
Tom Staab avatar image Tom Staab ♦ commented ·
I'm a little confused by your question and code. You said you need to insert today's data, but you gave us a delete statement. Is the idea to always just keep data from the past 2 days? In other words, delete the old data and insert today's data?
0 Likes 0 ·

1 Answer

·
thesqlguyatl avatar image
thesqlguyatl answered
Assuming your [DateStamp] field is a date-related data type and the precision of datediff() with getdate() and your date are adequate for your purpose, then the following where clauses should work. To include data older than the past 2 days in the criteria... WHERE DATEDIFF(DAY,[DateStamp],GETDATE()) > 2; To include the past 2 days of data in the criteria... WHERE DATEDIFF(DAY,[DateStamp],GETDATE()) BETWEEN 0 AND 2;
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.