Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2012 Forums
 Transact-SQL (2012)
 DATETIME -1 ENTRIES

Author  Topic 

Blessed1978
Yak Posting Veteran

97 Posts

Posted - 2014-02-27 : 20:31:01
i would like to update my stored procedure, which is ran daily, to only enter current day and 1 day prior. The tsql is doing joins on 4 tables i would like to get the previous days entries along with the current day it is being loaded on a column labled last_update_date.

i have something like this last_update_date >= dateadd(day,datediff(day,1,GETDATE()),0)


however it is still pulling all the historical days which i dont want, because it will create duplicate rows. if the day already exist it should skip over it and only enter current day -1(previous).

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2014-02-27 : 20:49:56
[code]last_update_date >= dateadd(day,datediff(day,1,GETDATE()),0)[/code]
that condition will only give you records where last_update_date that is greater than yesterday's date.
quote:
however it is still pulling all the historical days which i dont want

what are the last_update_date for those rows ?


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

Blessed1978
Yak Posting Veteran

97 Posts

Posted - 2014-02-28 : 07:06:52
the last update date is the last day the record was updated so if it is currently 2014-02-28 00:00:00:000 it should look back to 2014-02-27 00:00:00:000 and 2014-02-28 00:00:00:000
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2014-02-28 : 08:02:01
so isn't it giving you what you wanted ?

If not, post your sample data, full query and expected result


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

Robowski
Posting Yak Master

101 Posts

Posted - 2014-02-28 : 09:10:16
USE Tempdb;
GO

SET NOCOUNT ON;

IF OBJECT_ID ('#CustomerA', 'U') IS NOT NULL
DROP TABLE #CustomerA;
SELECT 1 as ID ,
CAST('28/02/2014' as DATE) as TranDate
INTO #CustomerA;

INSERT INTO #CustomerA
SELECT 2 as ID ,
CAST('27/02/2014' as DATE) as TranDate;

INSERT INTO #CustomerA
SELECT 3 as ID ,
CAST('26/02/2014' as DATE) as TranDate;
GO


SELECT *
FROM #CustomerA as A
WHERE TranDate >= CAST((GETDATE() -1) as DATE)


quote:
Originally posted by khtan

so isn't it giving you what you wanted ?

If not, post your sample data, full query and expected result


KH
[spoiler]Time is always against us[/spoiler]



Go to Top of Page

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2014-02-28 : 11:05:59
What do you want for output?

If you can describe in words what you want to happen it makes it, potentially, easier to understand than code that doesn't work/give the proper results.
Go to Top of Page
   

- Advertisement -