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

x
?
Solved

Handling datevalues in SQL

Posted on 2009-05-07
3
Medium Priority
?
268 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 2000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

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…
Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

656 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