Solved

Handling datevalues in SQL

Posted on 2009-05-07
3
262 Views
Last Modified: 2012-05-06
Hi I'm having some trouble getting a recordset to only include records dated > 1yr ago.

Fromdate = DateValue(DateAdd("d", -365, Now))
Todate = DateValue(Now)
       
        Sql = ""
        Sql = Sql & " Select [R_PatID] ,  [R_TestDate] from TEST_RESULTS"
        Sql = Sql & " where [R_PatID] = " & DSCohort.Fields("Pat_ID")
        Sql = Sql & " and  [R_TestDate] between " & DateValue(Fromdate) & " and " & DateValue(Todate)
        Sql = Sql & " order by [R_TestDate] asc"



The data is stored in a date time field (dd/mm/yyyy) in an excel 2003 file but I'm getting old dates such as 14/01/2003, 24/10/2004  being returned on Debug.Print DSResultRecords.Fields("R_Testdate") & " " & DSResultRecords.RecordCount

I have tried using :
1/ datevalue [r_testdate]  -  syntax error
2/ [R_testdate] > ' " & Fromdate & " ' "  - returns old values too.
3/ year [R_testdate] > " & year(fromdate - 1) . Works but includes all of last years dates ie 17months worth if I run it in may 09
 

Is there another way of doing this query to return only records dated from exactly 12 months ago??

Many thanks

0
Comment
Question by:peterdarazs
[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
  • 2
3 Comments
 
LVL 65

Expert Comment

by:rockiroads
ID: 24333390
why dont you use DateDiff instead?

DateDiff("interval","olddate","newdate")

set interval to be "d" for number of days difference
or "yyyy" or years difference

eg

Sql = Sql & " and  DateDiff('yyyy',[R_TestDate],now())  >= 1"



0
 
LVL 65

Accepted Solution

by:
rockiroads earned 500 total points
ID: 24333412
to use your existing method, you could format the dates
eg

Fromdate = format(DateAdd("yyyy", -1, Now()),"yyyymmdd")
Todate = format(now(),"yyyymmdd")

 Sql = Sql & " and  Format([R_TestDate],'yyyymmdd') between " & Fromdate & " and " & ToDate

0
 

Author Closing Comment

by:peterdarazs
ID: 31579283
Ok, thanks  - that explains it.
0

Featured Post

Business Impact of IT Communications

What are the business impacts of how well businesses communicate during an IT incident? Targeting, speed, and transparency all matter. Find out more in this infographic.

Question has a verified solution.

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

Suggested Solutions

'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

734 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