473,404 Members | 2,137 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

Searching between 2 DateTime Values using SELECT stmt in Vb.Net

115 100+
I’m using VB.net 2003 application program.

I am trying to do a select statement whereby I'm searching between 2 datetime values that are being stored as datetime. records are stored inside Access.

For example, I’m searching between 2 datetime
StartTime = “2/23/2009 10:00:00 AM”
EndTime = “2/23/2009 12:30:00 AM”
So I need to find all the records in between 10:00 AM and 12:30 AM on 2/23/2009.


i tried this code
Expand|Select|Wrap|Line Numbers
  1. strSQL = "select OrderID from Orders Where OrderDate >= ('" & StartTime & "') AND OrderDate <= ('" & EndTime & "') "
  2.  
but i got the error showing below
An unhandled exception of type 'System.Data.OleDb.OleDBException' occured in system.data.dll

Then i tried this code
Expand|Select|Wrap|Line Numbers
  1. strSQL = "select OrderID from Orders Where OrderDate >= DATEVALUE('" & StartTime & "') AND OrderDate <= DATEVALUE('" & EndTime & "') "
  2.  
But when I use DATEVALUE, it takes the date from the string and set time as midnight (00:00:00). So it returns no records between 10:00 AM and 12:30 AM on 2/23/2009, but I can see there are records.


Then i tried this code
Expand|Select|Wrap|Line Numbers
  1. strSQL = "select OrderID from Orders Where OrderDate >= TimeValue('" & StartTime & "') AND OrderDate <= TimeValue('" & EndTime & "') "
  2.  
And when I use TimeValue, it returns the time from the string and set date as jan 1st. so it returns no records between 10:00 AM and 12:30 AM on 2/23/2009, but I can see there are records.

Then i tried this code
Expand|Select|Wrap|Line Numbers
  1. strSQL = "select OrderID from Orders Where (OrderDate BETWEEN DATEVALUE('" & StartTime & "') AND DATEVALUE('" & EndTime & "')) "
  2.  
Expand|Select|Wrap|Line Numbers
  1. strSQL = "select OrderID from Orders Where (OrderDate BETWEEN ('" & StartTime & "') AND ('" & EndTime & "')) "
  2.  
but none of the codes above is returning records between 10:00 AM and 12:30 AM on 2/23/2009, but I can see there are records.

i searched and found all those examples. but that didn't work. Is there anyway i can search between 2 datetime values. i need to find all the records that lies between that time period (for example: between 10:00 AM and 12:30 AM on 2/23/2009).

If you have any idea how to do this, please let me know. if you can provide an example, then that will be great help for me.

Thanks in advance.
Feb 24 '09 #1
5 5003
remya1000
115 100+
when i tried these codes and it start working...
Expand|Select|Wrap|Line Numbers
  1.  strSQL = "select OrderDate from Orders Where OrderDate between (datevalue('" & StartTime & "') + timevalue('" & StartTime & "')) and (datevalue('" & EndTime & "') + timevalue('" & EndTime & "')) "
  2.  
Expand|Select|Wrap|Line Numbers
  1. strSQL = "select OrderDate from Orders Where OrderDate >= (datevalue('" & StartTime & "') + timevalue('" & StartTime & "')) and OrderDate <= (datevalue('" & EndTime & "') + timevalue('" & EndTime & "')) "
  2.  
But when i try
Expand|Select|Wrap|Line Numbers
  1.  strSQL = "select COUNT(*) from Orders Where OrderDate between (datevalue('" & StartTime & "') + timevalue('" & StartTime & "')) and (datevalue('" & EndTime & "') + timevalue('" & EndTime & "')) "
  2.  
  3. OR
  4.  
  5.  strSQL = "select SUM(gTotal) from Orders Where OrderDate between (datevalue('" & StartTime & "') + timevalue('" & StartTime & "')) and (datevalue('" & EndTime & "') + timevalue('" & EndTime & "')) "
  6.  
  7.  
Expand|Select|Wrap|Line Numbers
  1. strSQL = "select COUNT(*) from Orders Where OrderDate >= (datevalue('" & StartTime & "') + timevalue('" & StartTime & "')) and OrderDate <= (datevalue('" & EndTime & "') + timevalue('" & EndTime & "')) "
  2.  
  3. OR
  4.  
  5. strSQL = "select SUM(gTotal) from Orders Where OrderDate >= (datevalue('" & StartTime & "') + timevalue('" & StartTime & "')) and OrderDate <= (datevalue('" & EndTime & "') + timevalue('" & EndTime & "')) "
  6.  
Then its showing the below error
An unhandled exception of type 'System.Data.OleDb.OleDBException' occured in system.data.dll
but when i try this
Expand|Select|Wrap|Line Numbers
  1.  strSQL = "select COUNT(*) from Orders "
  2.  
Expand|Select|Wrap|Line Numbers
  1. strSQL = "select SUM(gTotal) from Orders "
  2.  
Then its returns the values.

Is it possible to use COUNT(*) or SUM(gTotal) while i'm checking DateTime Value? If you have any idea please help me.

Thanks in advance.
Feb 24 '09 #2
Frinavale
9,735 Expert Mod 8TB
Is OrderDate a varchar or a Date?
Feb 24 '09 #3
remya1000
115 100+
OrderDate is Date/Time field.
Feb 25 '09 #4
NeoPa
32,556 Expert Mod 16PB
Check out Literal DateTimes and Their Delimiters (#) and let me know afterwards if you are still experiencing any problems.
Feb 25 '09 #5
Stewart Ross
2,545 Expert Mod 2GB
Hi. The main problem with your first try at this one is that you are passing date/time literals as if they are text within single quotes, and you do the same later when using DateValue and TimeValue, which take date/time arguments, not text strings.

The convention for the JET database engine when referring to date literals is to use # (the pound or hash character) as a delimiter. Substituting in your original SQL string:

Expand|Select|Wrap|Line Numbers
  1. strSQL = "select OrderID from Orders Where OrderDate >= #" & StartTime & "# AND OrderDate <= #" & EndTime & "#"
You can use the Between operator to shorten this a little:
Expand|Select|Wrap|Line Numbers
  1. strSQL = "select OrderID from Orders Where OrderDate Between #" & StartTime & "# AND #" & EndTime & "#"
Within VB itself if you need to use DateValue or TimeValue in code you would not use delimiters if the arguments are already of date/time type. However, you are building a SQL string that is passed outside of the VB environment and interpreted by the database engine. In this case you are not actually passing the variable to the function as a date/time type; you are passing the value of that variable as a literal. For that reason you'd include the # delimiters in your SQL string:

Expand|Select|Wrap|Line Numbers
  1. strSQL = "select OrderID from Orders Where OrderDate Between DATEVALUE(#" & StartTime & "#) + TimeValue(#" & StartTime & "#) AND DATEVALUE(#" & EndTime & "#) + TimeValue (#" & EndTime & "#)"
However, this last statement is just a much more complicated way of doing the first one shown above.

In summary, you need the # delimiters if you are passing date literals to the operators or function concerned. When you build SQL strings you cannot reference VB variables outside of the VB environment, so you have to pass their literal values enclosed within delimiters as part of the SQL string.

For more on date/time values in ANSI SQL and Access see our insight article date-time literals and their delimiters

-Stewart

ps take note of the format for dates to which SQL statements should conform, as listed in the insight article. I note that you have already specified that your date component is in m/d/y format, which should be compliant with the ANSI standard.
Feb 25 '09 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Loi Tan Vo | last post by:
Hi, I have a table containing for ex. a column named MenberDate. The column has property as datetime. Values look like 13.04.2004 (dd.mm.yyyy) in the table. Select 'INSERT INTO Menber(Name,...
12
by: Phil Powell | last post by:
<cfquery name="getAll" datasource="#request.dsn#"> SELECT U.userID, U.fname, U.lname, U.phone, U.lastLoggedIn, U.choiceId, U.experience, T.label AS teamLabel, R.label AS roleLabel FROM User U...
4
by: les | last post by:
I have a form to search records by date of birth. The form has 3 dropdowns for day, month and year: Dim strDOB as string = ("'" & dobday.selectedvalue & " " & dobmonth.selectedvalue & " " &...
33
by: Geoff Jones | last post by:
Hiya I have a DataTable containing thousands of records. Each record has a primary key field called "ID" and another field called "PRODUCT" I want to retrieve the rows that satisy the following...
0
by: Jeff Boes | last post by:
I hope this helps someone else ... I had struggled some time ago with attempts to get a rank of values query to work, but then I gave up and set it aside. I had another reason to attack it, and in...
0
by: Jeff Boes | last post by:
Gah, I'm dreadfully sorry. The original functions were cut-and-pasted from a "\df+" window, which meant they lost their quoted-ness, which means if you try to cut and paste from my message to a SQL...
5
by: Nirmala123 | last post by:
hi... I want to sort the table using combobox values. I give the code here. address.html: <html> <head> <title>Add a new entry</title> </head>
2
by: Yew12 | last post by:
We are trying to get the following script to display the full date and time. The field we are calling does have both date and time in. Unfortunatly its only returning the date. So I tried putting...
0
yasirmturk
by: yasirmturk | last post by:
Standard Date and Time Functions The essential date and time functions that every SQL Server database should have to ensure that you can easily manipulate dates and times without the need for any...
1
by: remya1000 | last post by:
I’m using VB.net 2003 application program. I am trying to do a select statement whereby I'm searching between 2 datetime values that are being stored as datetime. records are stored inside...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.