Click here to Skip to main content
15,896,111 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I have two fields in my DB.
1) Invoice_Date and Invoice_Time

They recored date and time from my invoices.

I want to pick data under two queries:
1)Using From_Time DateTimePicker and To_Time DateTimePicker (they display as 19:20:18 PM)
Here I want to pick data for say between 13:10:12 PM to 18:10:20 PM, no matter what date it is.

2) Secondly I want data for a specific data range (from datatimepickers) to specific time range (time datatimepickers)

Please advise how to do it.

I tried:

VB
Where InvTime between @Time1 and @Time2


But it did not generate any data, while data is there for the given time range.

Please help.
Thanks
Posted
Comments
OriginalGriff 15-Sep-13 10:25am    
How have you stored the times? What are the columns defined as in your database?
Furqan Sehgal 15-Sep-13 13:32pm    
Time has been stored in datatime type column of the DB
it stores it like 09/11/13 12:14:20
syed shanu 15-Sep-13 21:20pm    
Can you post your query here and what you have tried.
in where condintion you can check with date between your fromdateandtime and todateandtime. you can use sql server date time convert method chk this links.
http://www.sql-server-helper.com/tips/date-formats.aspx , http://www.w3schools.com/sql/sql_dates.asp , http://anubhavg.wordpress.com/2009/06/11/how-to-format-datetime-date-in-sql-server-2005/

Your first requirement was to select only based on time without regard to the date. The following example casts each DateTime data type as a Time data type so that only the time value is compared. Solution assumes that DateTimePicker returns a time of the format hh:mm:ss tt or HH:mm:ss.
Dim strSQL As String = "Select col1,col2,col3,InvTime from table1 where CAST(InvTime AS Time) between CAST(@Time1 AS Time) AND CAST(@Time2 AS Time);"
Dim obCommand As SqlCommand = New SqlCommand(strSQL, cn)
Dim obParm As New SqlParameter
obParm.SqlDbType = SqlDbType.DateTime
obParm.Direction = ParameterDirection.Input
obParm.ParameterName = "@Time1"
obParm.Value = CDate(From_Time_DateTimePicker.Value)
obCommand.Parameters.Add(obParm)
obParm = New SqlParameter
obParm.SqlDbType = SqlDbType.DateTime
obParm.Direction = ParameterDirection.Input
obParm.ParameterName = "@Time2"
obParm.Value = CDate(To_Time_DateTimePicker.Value)
obCommand.Parameters.Add(obParm)
obParm = Nothing
Try
    rs = obCommand.ExecuteReader
Catch myException As SqlException
    Call ShowSQLException(myException)
End Try



Your second requirement was to use dates from a Date DateTimePicker and times from a Time DateTimePicker. Solution assumes that Date DateTimePicker returns MM/dd/yyyy format and Time DateTimePicker returns a time of the format hh:mm:ss tt or HH:mm:ss.
Dim strSQL As String = "Select col1,col2,col3,InvTime from table1 where InvTime between @DateTime1 AND @DateTime2;"
Dim obCommand As SqlCommand = New SqlCommand(strSQL, cn)
Dim obParm As New SqlParameter
obParm.SqlDbType = SqlDbType.DateTime
obParm.Direction = ParameterDirection.Input
obParm.ParameterName = "@DateTime1"
obParm.Value = CDate(From_Date_DateTimePicker.Value & " " & From_Time_DateTimePicker.Value)
obCommand.Parameters.Add(obParm)
obParm = New SqlParameter
obParm.SqlDbType = SqlDbType.DateTime
obParm.Direction = ParameterDirection.Input
obParm.ParameterName = "@DateTime2"
obParm.Value = CDate(To_Date_DateTimePicker.Value & " " & To_Time_DateTimePicker.Value)
obCommand.Parameters.Add(obParm)
obParm = Nothing
Try
    rs = obCommand.ExecuteReader
Catch myException As SqlException
    Call ShowSQLException(myException)
End Try
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900