May 3, 2016 at 5:57 am
Hi,
I need to wipe out just today's data and insert new ones . Yesterday's data should be untouched . But the procedure is wiping off yesterday's record using the code below . Server Time is 5 hours ahead of CST . That's why I'm using -5
CREATE TABLE dbo.Temp1
(
HistoryDate Date,
Task VARCHAR(100),
Count INT)
)
DECLARE @HistoryDate DATETIME = DATEADD(dd, 0, DATEDIFF(dd, 0, dateadd(hour,-5,getdate())))
DELETE FROM dbo.Temp1 WHERE HistoryDate = @HistoryDate
INSERT INTO dbo.Temp1 (
[Task]
,HistoryDate
,[Count]
)
SELECT
'ABCD 1',
@HistoryDate,
1 AS Count
Thanks,
PSB
May 3, 2016 at 7:09 am
PSB (5/3/2016)
Hi,I need to wipe out just today's data and insert new ones . Yesterday's data should be untouched . But the procedure is wiping off yesterday's record using the code below . Server Time is 5 hours ahead of CST . That's why I'm using -5
CREATE TABLE dbo.Temp1
(
HistoryDate Date,
Task VARCHAR(100),
Count INT)
)
DECLARE @HistoryDate DATETIME = DATEADD(dd, 0, DATEDIFF(dd, 0, dateadd(hour,-5,getdate())))
DELETE FROM dbo.Temp1 WHERE HistoryDate = @HistoryDate
INSERT INTO dbo.Temp1 (
[Task]
,HistoryDate
,[Count]
)
SELECT
'ABCD 1',
@HistoryDate,
1 AS Count
Thanks,
PSB
You have a couple of options here.
1. NEVER run this code between the hours of 00:00:00 and 05:00:00 then your desired output will work as is.
However, I suspect you are probably running this throughout the day or have or will have that kind of requirement as your data grows in size, thus deleting records throughout the day. Which begs to question how are getting the old records......I digress Im sure you have that worked out already....back to the problem at hand....
2. If you must run this during the hours of midnight to 5am then this should work.
DELETE FROM dbo.Temp1 WHERE HistoryDate = @HistoryDate and Day(@HistoryDate)=Day(GETDATE())
May 3, 2016 at 7:19 am
I have to run this code once every hour .
May 3, 2016 at 7:26 am
If server is 5 hour ahead of CST, but you're still taking the time from the server. Why would you need to change the time? What time defines "today" and "yesterday"?
May 3, 2016 at 8:11 am
I want the CST time recorded . If I don't do a -5 then May3rd, 7pm would be inserted as May 4th,00 am .
May 3, 2016 at 8:14 am
PSB (5/3/2016)
I want the CST time recorded . If I don't do a -5 then May3rd, 7pm would be inserted as May 4th,00 am .
If the server is running in CST, then May3rd, 7pm will be May3rd, 7pm.
Is the server in a different time zone?
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
May 3, 2016 at 8:18 am
PSB (5/3/2016)
I want the CST time recorded . If I don't do a -5 then May3rd, 7pm would be inserted as May 4th,00 am .
Maybe I'm misunderstanding the situation and the sample DDL isn't representative, but no times are recorded at all. The table column is of the DATE datatype.
Is that as it's supposed to be?
Cheers!
May 3, 2016 at 8:26 am
Server is UTC time zone .
I just need to delete today's records and insert new ones and keep yesterday and older records. Rest (time zone) is not mandatory.
May 3, 2016 at 8:40 am
This is a quick test on the date you would be using when run at different times. Are the values correct? Other than the formula, your code doesn't seem to be having a problem.
WITH
E(n) AS(
SELECT n FROM (VALUES(0),(0),(0),(0),(0),(0),(0),(0),(0),(0))E(n)
),
E2(n) AS(
SELECT a.n FROM E a, E b
),
cteTally(n) AS(
SELECT TOP 48 DATEADD( hh, ROW_NUMBER() OVER(ORDER BY (SELECT NULL))-1, '20160503') n
FROM E2
)
SELECT n, DATEADD(dd, 0, DATEDIFF(dd, 0, dateadd(hour,-5,n)))
FROM cteTally;
May 3, 2016 at 8:59 am
PSB (5/3/2016)
Server is UTC time zone .I just need to delete today's records and insert new ones and keep yesterday and older records. Rest (time zone) is not mandatory.
DELETE FROM dbo.Temp1 WHERE HistoryDate = @HistoryDate and Day(@HistoryDate)=Day(GETDATE())
Aside from all the logical nightmares you are undoubtedly going through this will ensure that the historydate is the same date as today before deleting any records, which is really only effective for the 5 hour window from midnight to 5am that your -5 code does not account for.
May 3, 2016 at 9:58 am
PSB (5/3/2016)
Server is UTC time zone .I just need to delete today's records and insert new ones and keep yesterday and older records. Rest (time zone) is not mandatory.
If the server is UTC time zone, maybe you should be using GETUTCDATE() instead of GETDATE().
Drew
J. Drew Allen
Business Intelligence Analyst
Philadelphia, PA
May 3, 2016 at 5:11 pm
DECLARE @HistoryDate DATETIME
SET @HistoryDate = dateadd(hour,-5, DATEADD(dd, 0, DATEDIFF(dd, 0, getdate() )) )
_____________
Code for TallyGenerator
Viewing 12 posts - 1 through 11 (of 11 total)
You must be logged in to reply to this topic. Login to reply