Solved

SQL query use variable instead of hard coded dates?

Posted on 2013-06-25
9
1,033 Views
Last Modified: 2013-06-28
Hi,

I'm pulling data from an SQL database via MS access  2010. The SQL query works fine with the date and time hard coded. However, the user needs to set the start and end date and time manually.
For this I'm using a date picker and formating the date/time returned to yyyy-mm-dd 00:00:00 which is stored in a string variable (although I think the 102 in the convert statement means yy.mm.dd??)

How would I use this variable instead of the hard coded date? I'm really no good with SQL!

Here is the end part of the SQL query

BETWEEN CONVERT(DATETIME, '2013-06-01 00:00:00', 102) AND CONVERT(DATETIME, '2013-06-07 00:00:00', 102)) AND  (TMSACMON.LOGNO IN (35, 36, 37, 5)) ORDER BY TMSACMON.ACDATE"

The SQL is being executed in VBA like this..

Set objMyCmd.ActiveConnection = objMyConn
       objMyCmd.CommandText = "Blah Blah"
        objMyCmd.CommandType = adCmdText
0
Comment
Question by:wildarmsdave
  • 5
  • 4
9 Comments
 
LVL 48

Expert Comment

by:PortletPaul
Comment Utility
can you post the vba that contains the full query please? this will help you get a complete answer.

>> I think the 102 in the convert statement means yy.mm.dd
102 gives: YYYY.MM.DD

for the format you are providing (which is a good one) this would suffice:
CONVERT(DATETIME, '2013-06-01 00:00:00')

however, please be aware that "between" probably does not do what you expect it to
let's say in your date picker you select the First of May and 31st of May
> with the expectation all data for May will be chosen by the query

IF, the actual data has "date with time" then "between" will NOT choose all of May
it would miss all events that occurred during May 31

The most reliable method for choosing date ranges is a combination of >= with <
but you also need to add one day to May 31
e.g.

where fieldname >= convert(datetime,'2013-05-01 00:00:00')
and fieldname < convert(datetime,'2013-06-01 00:00:00') -- one day more added

another difference is that "between" includes the second mentioned datetime
but if you use less than, then this is (correctly) not included

for more on this topic please see: "Beware of Between"
0
 

Author Comment

by:wildarmsdave
Comment Utility
Hi PortletPaul,

Thanks for the reply. The full SQL query is listed below (it is pretty long!) I hope this helps in some way?


Open in new window


objMyCmd.CommandText = "SELECT TMSACMON.ACDATE, TMSACMON.LOGNO, TERMDEF.LOCATION, TMSACMON.EMPREF, TMSACMON.INITIALS, TMSACMON.SURNAME, TMSACMON.ACDIR AS DIRECTION, TMSACMON.REASON FROM TMSACMON INNER JOIN TERMDEF ON TMSACMON.LOGNO = TERMDEF.REF WHERE(TMSACMON.ACDATE BETWEEN CONVERT(DATETIME, '2013-06-01 00:00:00', 102) AND CONVERT(DATETIME, '2013-06-07 00:00:00', 102)) AND  (TMSACMON.LOGNO IN (35, 36, 37, 5)) ORDER BY TMSACMON.ACDATE"


Open in new window

0
 
LVL 48

Expert Comment

by:PortletPaul
Comment Utility
that is not long :) but I'm a klutz with VBA (been about 127 years since I wrote any)

It will be "something" like this (I think)
Dim dteStart As Date
Dim dteEnd As Date
dteStart = #2013-06-01#
dteStart = #2013-06-07#

objMyCmd.CommandText = "SELECT TMSACMON.ACDATE, TMSACMON.LOGNO, TERMDEF.LOCATION, TMSACMON.EMPREF, TMSACMON.INITIALS, TMSACMON.SURNAME, TMSACMON.ACDIR AS DIRECTION, TMSACMON.REASON" _
& "FROM TMSACMON INNER JOIN TERMDEF ON TMSACMON.LOGNO = TERMDEF.REF WHERE (" _
& "TMSACMON.ACDATE BETWEEN #' & #" & dteStart & "#" _
& "AND #" & dteEnd & "# ) AND (TMSACMON.LOGNO IN (35, 36, 37, 5)) ORDER BY TMSACMON.ACDATE"

Open in new window

your job would be to get the datepicker information into those variables
also not you may need to add 1 day to the end date (as discussed earlier)

I cannot promise my poor vba works by copy/paste I'm afraid.
but if not hopefully others will assist
0
 

Author Comment

by:wildarmsdave
Comment Utility
Excel likes the Query (i.e it the text doesn't turn red) but on running it I get an error saying "Incorrect syntax near the keyword INNER"  ????
0
IT, Stop Being Called Into Every Meeting

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!

 
LVL 48

Expert Comment

by:PortletPaul
Comment Utility
Dim dteStart As Date
Dim dteEnd As Date
dteStart = #2013-06-01#
dteStart = #2013-06-07#

objMyCmd.CommandText = "SELECT TMSACMON.ACDATE, TMSACMON.LOGNO, TERMDEF.LOCATION, TMSACMON.EMPREF, TMSACMON.INITIALS, TMSACMON.SURNAME, TMSACMON.ACDIR AS DIRECTION, TMSACMON.REASON" _
& "FROM TMSACMON INNER JOIN TERMDEF ON TMSACMON.LOGNO = TERMDEF.REF WHERE (" _
& "TMSACMON.ACDATE BETWEEN #"  & dteStart & "#" _
& "AND #" & dteEnd & "# ) AND (TMSACMON.LOGNO IN (35, 36, 37, 5)) ORDER BY TMSACMON.ACDATE"

Open in new window

made a small change on line 8 (there were too many # symbols)
but I don't know if this fixes that error
0
 

Author Comment

by:wildarmsdave
Comment Utility
Hi PortletPaul,

Thanks for this but I'm still getting the same error message?

Ta,

Dave.
0
 
LVL 48

Expert Comment

by:PortletPaul
Comment Utility
sorry "I'm a klutz with VBA", if this question doesn't attract someone with the knowledge needed soon maybe open a new question
0
 

Author Comment

by:wildarmsdave
Comment Utility
Hi PortletPaul,

I've fixed this (revised query below). I think the # character is only used in MSACCESS VBA but I could be wrong. Thank you for trying, you stll get the points for your hard work!

Ta,

Dave.

objMyCmd.CommandText = "SELECT TMSACMON.ACDATE, TMSACMON.LOGNO, TERMDEF.LOCATION, TMSACMON.EMPREF, TMSACMON.INITIALS, TMSACMON.SURNAME, TMSACMON.ACDIR AS DIRECTION, TMSACMON.REASON " & "FROM TMSACMON INNER JOIN TERMDEF ON TMSACMON.LOGNO = TERMDEF.REF WHERE (" & "TMSACMON.ACDATE BETWEEN '" & dteStart & "'" & " AND '" & dteEnd & "') AND (TMSACMON.LOGNO IN (35, 36, 37, 5)) ORDER BY TMSACMON.ACDATE"
0
 
LVL 48

Accepted Solution

by:
PortletPaul earned 500 total points
Comment Utility
great work Dave, v.pleased you got there.

I wouldn't normally put # I have to say, it was "SQL database via MS access" that had me. I seem to recall the reverse being the case in another question, so: "I'm confused on this point" :)

Cheers, Paul
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

SQL Command Tool comes with APEX under SQL Workshop. It helps us to make changes on the database directly using a graphical user interface. This helps us writing any SQL/ PLSQL queries and execute it on the database and we can create any database ob…
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.

763 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

15 Experts available now in Live!

Get 1:1 Help Now