Solved

Handling datevalues in SQL

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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

As they say in love and is true in SQL: you can sum some Data some of the time, but you can't always aggregate all Data all the time! Introduction: By the end of this Article it is my intention to bring the meaning and value of the above quote to…
If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
This video discusses moving either the default database or any database to a new volume.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

759 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now