January 6, 2014 at 6:59 am
Hi,
I'm trying to use a DATEDIFF statement so I only see data from the last 7 days
How would I write that if I don't have a time stamp in a table to reference?
If I could reference something I know I could do
Where DATEDIFF (dd, example.tabletime, getdate ()) < 7
I need to do it without referencing a value in a table and not sure how
January 6, 2014 at 7:02 am
If you haven't got a timestamp date/time column in your table, how can you tell if something is more than 7 days old?
Does your table have an assocoation with any other tables containing dates?
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
January 6, 2014 at 9:00 am
Thanks for the reply
I modified my query but it returns an error
Delcare @dt date
Set @dt = GETDATE()
select distinct v1.name 'Machine Name', v1. 'Username', t1.displayname 'Unlicensed Application', t1.installdate 'Install Date'
from Inv_AddRemoveProgram t1
left outer join vcomputer v1
on t1._resourceguid = v1.guid
where searchdate between @dt-7 and @dt
ERROR: Must declare the scalar variable "@dt"
January 6, 2014 at 9:03 am
joey6401c (1/6/2014)
Thanks for the replyI modified my query but it returns an error
Delcare @dt date
Set @dt = GETDATE()
select distinct v1.name 'Machine Name', v1. 'Username', t1.displayname 'Unlicensed Application', t1.installdate 'Install Date'
from Inv_AddRemoveProgram t1
left outer join vcomputer v1
on t1._resourceguid = v1.guid
where searchdate between @dt-7 and @dt
ERROR: Must declare the scalar variable "@dt"
DECLARE @dt date
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
January 6, 2014 at 9:10 am
joey6401c (1/6/2014)
Thanks for the replyI modified my query but it returns an error
Delcare @dt date
Set @dt = GETDATE()
select distinct v1.name 'Machine Name', v1. 'Username', t1.displayname 'Unlicensed Application', t1.installdate 'Install Date'
from Inv_AddRemoveProgram t1
left outer join vcomputer v1
on t1._resourceguid = v1.guid
where searchdate between @dt-7 and @dt
ERROR: Must declare the scalar variable "@dt"
Is SearchDate a column in your table? If not, that won't work.
Don't just copy code from someone else without understanding what it does (or in this case doesn't) do. What Mitesh posted was an example of how you filter a resultset based on a column in your table.
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
January 6, 2014 at 9:16 am
ChrisM@Work (1/6/2014)
If you haven't got atimestampdate/time column in your table, how can you tell if something is more than 7 days old?Does your table have an assocoation with any other tables containing dates?
That probably means that you're using SQL Server 2005 or the database is in the 2005 compatability mode because the DATE datatype isn't available there. You should have also gotten an error that DATE isn't a valid datatype, in this case.
--Jeff Moden
Change is inevitable... Change for the better is not.
January 6, 2014 at 10:50 am
I didn't know if searchdate was a statement I could use
I wrote in my original post that I need to write it without referencing a timestamp in a table
I know I could do it somehow with getdate() just not sure how to write it correctly
I want to basically say where DATEDIFF < GETDATE()-7
This doesn't work but I was thinking something along these lines
January 6, 2014 at 11:33 am
You can calculate the date that is 7 days before today, but that won't help you limit your query results because you don't have a date field to compare it against. To limit the rows returned based on date, you need to know when each row was created. Without a date column in the table, I see now way to return the most recent n days worth of rows.
January 8, 2014 at 12:54 pm
Thanks for your help. I ended up writing it a different way without using dates because I realized that specifying dates wouldn't help with what I was trying to do anyway. I basically wrote it excluding what already exists and once a week after it runs and I get my results I'm going to add the output as additional exclusions. Unfortunately it will require minimal weekly maintenance but that's ok.
Thanks again!
Viewing 10 posts - 1 through 9 (of 9 total)
You must be logged in to reply to this topic. Login to reply