Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

.NET Forum


You are currently viewing our .NET Forum as a guest. Please register to participate.
Login



Reply
Searching between 2 DateTime Values using SELECT stmt in Vb.Net
Old 02-24-2009, 11:40 AM Searching between 2 DateTime Values using SELECT stmt in Vb.Net
Experienced Talker

Posts: 35
Name: remya
Trades: 0
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; 02-24-2009 at 03:14 PM..
remya1000 is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 02-24-2009, 03:55 PM Re: Searching between 2 DateTime Values using SELECT stmt in Vb.Net
Experienced Talker

Posts: 35
Name: remya
Trades: 0
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.
remya1000 is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Searching between 2 DateTime Values using SELECT stmt in Vb.Net
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.13474 seconds with 12 queries