[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

SQL date calculations?

Posted on 2001-06-08
14
Medium Priority
?
171 Views
Last Modified: 2011-08-18
Hi I was wondering if I could sort a database by extracting all dates within 7 days of the present date.

I'm trying something like this:
SELECT WHEN, DOWHAT, ID FROM Source WHERE (Date - WHEN) <= 00/07/00 ORDER BY WHEN;

I was pretty sure it wouldn't work, but you can't blame a guy for trying.  =)

0
Comment
Question by:nerfgunz
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 3
  • 2
  • +3
14 Comments
 
LVL 1

Accepted Solution

by:
morgan_peat earned 200 total points
ID: 6168379
You can do this through VB, or in the DB.

VB:
SELECT stuff FROM table WHERE date >= DateAdd("d", -7, Now)

Or through DB, which is DB specific (eg. SQL Server has a DateAdd, and a GetDate function)
0
 
LVL 3

Expert Comment

by:jrspano
ID: 6168381

select field1, field2 from table1 where DATEDIFF(day, datefieldindb, getdate()) <= 7
0
 

Author Comment

by:nerfgunz
ID: 6168523
Yes i am trying to do it in vb.  However both of these methods are not working for me.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 70

Expert Comment

by:Éric Moreau
ID: 6168536
Which database engine are you using? Different engines = Different syntax.
0
 

Author Comment

by:nerfgunz
ID: 6168544
access 2000
0
 
LVL 5

Expert Comment

by:dgorin
ID: 6168612
Try this

sql = "SELECT WHEN, DOWHAT, ID FROM Source WHERE WHEN <= #" & DateDiff("d", -7, Now()) & "# ORDER BY WHEN"

Date values in Access sql need to be delimited by the # character, most other SQL's by the ' character.



0
 

Author Comment

by:nerfgunz
ID: 6168836
what is "d"?
0
 

Author Comment

by:nerfgunz
ID: 6168932
dqorin, it doesnt work.
0
 

Author Comment

by:nerfgunz
ID: 6169173
I'm trying this now, but it gives me ALL the data in the table.

dcOverduePending.RecordSource = "SELECT WHEN, DOWHAT, ID FROM " & Source & " WHERE DateDiff(""d"", WHEN, " & Date & ") <= 7 ORDER BY WHEN;"
0
 

Author Comment

by:nerfgunz
ID: 6169201
going to start a new thread.
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 6169841
I just test this query against the NWind database and it is working:
Adodc1.RecordSource = "SELECT DateDiff('d', OrderDate, Date()), * " & _
                      "From Orders " & _
                      "Where DateDiff('d', OrderDate, Date()) >= 7 " & _
                      "ORDER BY OrderDate"

Your query should read:
dcOverduePending.RecordSource = "SELECT WHEN, DOWHAT, ID FROM " & Source & " WHERE DateDiff('d', WHEN, Date()) <= 7 ORDER BY WHEN"
0
 
LVL 3

Expert Comment

by:Hornet241
ID: 6169939

Try this

Dim TargetDate as Date

TargetDate = Now - 7

SELECT WHEN, DOWHAT, ID FROM " & Source & " WHERE When > #" & TargetDate & "#"

0
 
LVL 5

Expert Comment

by:dgorin
ID: 6169943
Sorry, should have been DateAdd, not DateDiff.  DateAdd returns a date datatype, DateDiff returns a long integer.  The "d" indicates the interval is by days.

sql = "SELECT WHEN, DOWHAT, ID FROM Source WHERE WHEN <= #" & DateAdd("d", -7, Date()) & "# ORDER BY
WHEN"



0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 6198580
Please maintain this question.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses

649 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