Solved

Sqldatasource WHERE with dates question

Posted on 2010-09-03
4
469 Views
Last Modified: 2012-05-10
I am trying to do something that I thought would be simple but it doesnt seem to be.  I have a date field stored in SQL.  I want to create a sqldatasource linked to a gridview with a where clause so it will only display the row if the date is within 7 days of the current date.  So as soon as the value is over a week old, it gets removed from the gridview.  I have tried everything I can think of but it either says I am comparing a date to a string or gives me syntax errors.  Can someone tell me how to do this?  Thank you.
0
Comment
Question by:mattpayne59
  • 2
4 Comments
 
LVL 7

Accepted Solution

by:
bouscal earned 500 total points
ID: 33599031
Your WHERE clause should read;

WHERE DATEDIFF(dd,yourdatefield,GETDATE()) < 7

0
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 33599174
0
 

Author Comment

by:mattpayne59
ID: 33599191
This is the statement I get from your instruction:

SELECT * FROM [IntakeID] WHERE (([Approved] IS NOT NULL) AND DATEDIFF(dd,ApprovedDate,GETDATE()) < 7

Can I use ApprovedDate like this or do I have to do it in a different way?  It is a valid SQL date field.  When I test this I get "Incorrect syntax near the keyword 'SET'." and I have no idea what that means.
0
 

Author Comment

by:mattpayne59
ID: 33599299
Forget it.  I had an extra ( in there.  This works great:

SELECT * FROM [IntakeID] WHERE  ([Approved] IS NOT NULL) AND DATEDIFF(dd,[ApprovedDate],GETDATE()) < 7

Thank you for your help.
0

Featured Post

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

837 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question