Solved

Handling datevalues in SQL

Posted on 2009-05-07
3
264 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

The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

Question has a verified solution.

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

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

691 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