Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Database > SQL Language
|
SQL Language SQL Language discussions not specific to a particular RDBMS program or vendor.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Language section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 24th, 2009, 11:32 AM
Authorized User
 
Join Date: Sep 2007
Posts: 62
Thanks: 0
Thanked 0 Times in 0 Posts
Default Searching between 2 DateTime Values using SELECT stmt in Vb.Net

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
Quote:
StartTime = “2/23/2009 9:00:00 AM”
EndTime = “2/23/2009 11:30:00 AM”
So I need to find all the records in between 9:00 AM and 11:30 AM on 2/23/2009.

i tried this code
Code:
strSQL = "select OrderID from Orders Where OrderDate >= ('" & StartTime & "') AND OrderDate <= ('" & EndTime & "') "
but i got the error showing below
Quote:
An unhandled exception of type 'System.Data.OleDb.OleDBException' occured in system.data.dll
Then i tried this code
Code:
strSQL = "select OrderID from Orders Where OrderDate >= DATEVALUE('" & StartTime & "') AND OrderDate <= DATEVALUE('" & EndTime & "') "
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 9:00 AM and 11:30 AM on 2/23/2009, but I can see there are records.

Then i tried this code
Code:
strSQL = "select OrderID from Orders Where OrderDate >= TimeValue('" & StartTime & "') AND OrderDate <= TimeValue('" & EndTime & "') "
And when I use TimeValue, it returns the time from the string and set date as jan 1st. so it returns no records between 9:00 AM and 11:30 AM on 2/23/2009, but I can see there are records.
Then i tried this code
Code:
strSQL = "select OrderID from Orders Where (OrderDate BETWEEN DATEVALUE('" & StartTime & "') AND DATEVALUE('" & EndTime & "')) "
Code:
strSQL = "select OrderID from Orders Where (OrderDate BETWEEN ('" & StartTime & "') AND ('" & EndTime & "')) "
but none of the codes above is returning records between 9:00 AM and 11: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 9:00 AM and 11: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.

Last edited by remya1000; February 24th, 2009 at 02:53 PM..
 
Old February 24th, 2009, 01:42 PM
Friend of Wrox
 
Join Date: Sep 2005
Posts: 166
Thanks: 2
Thanked 33 Times in 33 Posts
Default

Hi Remya,

The main problem you've got is that your criteria is wrong: 12.30 AM (half past midnight) comes before 10.00AM, so you will never get any results. You need to use 12.30PM (half past midday).

Also dates within access are different to some other systems in that you use # to work with them. So your query should become something like

Code:
SELECT OrderID FROM Orders WHERE OrderDate BETWEEN #02/23/2009 10:00:00 AM# AND #02/23/2009 12:30:00 PM#
Hope this helps
Phil
 
Old February 24th, 2009, 03:03 PM
Authorized User
 
Join Date: Sep 2007
Posts: 62
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Philip_Cole, thanks for the reply.

Actually inside my program i used EndTime - 2/23/2009 12:30:00 PM. but while typing in this forum i made a mistake and that changed to 2/23/2009 12:30:00 AM.

Even when i try
StartTime = 2/23/2009 9:00:00 AM
EndTime = 2/23/2009 10:00:00 AM
using all the codes above, it didn't worked.

As you said i tried this code too,
Code:
StartTime = 2/23/2009 9:00:00 AM
EndTime = 2/23/2009 10:00:00 AM
 
strSQL = "select OrderDate from Orders Where OrderDate BETWEEN # StartTime # AND # EndTime # "
Code:
StartTime = 2/23/2009 9:00:00 AM
EndTime = 2/23/2009 10:00:00 AM
 
strSQL = "select OrderDate from Orders Where OrderDate BETWEEN ' # StartTime # ' AND ' # EndTime # ' "
but i got the error showing below
Quote:
An unhandled exception of type 'System.Data.OleDb.OleDBException' occured in system.data.dll
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 9:00 AM and 11:30 AM on 2/23/2009).

If you have any idea how to do this, please help me.

Thanks in advance

Last edited by remya1000; February 24th, 2009 at 03:06 PM..
 
Old February 24th, 2009, 03:49 PM
Authorized User
 
Join Date: Sep 2007
Posts: 62
Thanks: 0
Thanked 0 Times in 0 Posts
Default

when i tried these codes and it start working...
Code:
 strSQL = "select OrderDate from Orders Where OrderDate between (datevalue('" & StartTime & "') + timevalue('" & StartTime & "')) and (datevalue('" & EndTime & "') + timevalue('" & EndTime & "')) "
Code:
strSQL = "select OrderDate from Orders Where OrderDate >= (datevalue('" & StartTime & "') + timevalue('" & StartTime & "')) and OrderDate <= (datevalue('" & EndTime & "') + timevalue('" & EndTime & "')) "
But when i try
Code:
 strSQL = "select COUNT(*) from Orders Where OrderDate between (datevalue('" & StartTime & "') + timevalue('" & StartTime & "')) and (datevalue('" & EndTime & "') + timevalue('" & EndTime & "')) "
OR
 strSQL = "select SUM(gTotal) from Orders Where OrderDate between (datevalue('" & StartTime & "') + timevalue('" & StartTime & "')) and (datevalue('" & EndTime & "') + timevalue('" & EndTime & "')) "
Code:
strSQL = "select COUNT(*) from Orders Where OrderDate >= (datevalue('" & StartTime & "') + timevalue('" & StartTime & "')) and OrderDate <= (datevalue('" & EndTime & "') + timevalue('" & EndTime & "')) "
OR
strSQL = "select SUM(gTotal) from Orders Where OrderDate >= (datevalue('" & StartTime & "') + timevalue('" & StartTime & "')) and OrderDate <= (datevalue('" & EndTime & "') + timevalue('" & EndTime & "')) "
Then its showing the below error
Quote:
An unhandled exception of type 'System.Data.OleDb.OleDBException' occured in system.data.dll
but when i try this
Code:
 strSQL = "select COUNT(*) from Orders "
Code:
strSQL = "select SUM(gTotal) from Orders "
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.





Similar Threads
Thread Thread Starter Forum Replies Last Post
convert string to datetime in VB.net lisabb ASP.NET 2.0 Basics 3 June 19th, 2007 06:52 AM
Searching for objects in VB.NET mike_abc Pro VB.NET 2002/2003 1 February 10th, 2005 08:33 AM
Searching datetime field SoC Classic ASP Databases 2 October 29th, 2004 10:38 PM
COUNT ON SELECT DISTINCT stmt savoym SQL Language 7 August 28th, 2003 07:58 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.