Click here to Skip to main content
15,894,646 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I know how to filter dates inside of 1 column, but how do you filter between 2 columns?

Example
"Select * from Employeetable hiredate between @startyear and @endyear, sqlcon"
sqladapt.SelectCommand.Parameters.AddWithValue("@startyear", start.Text)
       sqladapt.SelectCommand.Parameters.AddWithValue("@endyear", end.Text)


there are quite a few examples for a single column but I cannot find much on using 2 date columns.

can someone help me with this

What I have tried:

I have tried
"SELECT * FROM [Employeetable] where [Hiredate] > @startyear and [Lastday] < @endyear, sqlcon"
sqladapt.SelectCommand.Parameters.AddWithValue("@startyear", start.Text)
        sqladapt.SelectCommand.Parameters.AddWithValue("@endyear", end.Text)



both are dates
Posted
Updated 11-Apr-18 17:39pm

1 solution

I take it the dates are stored as dates in the database? If that is correct, then the parameter should also be a date.

Try converting the text into a datetime first and then use the value as parameter. In other words
C#
...
DateTime startdate;
DateTime enddate;
if (!DateTime.TryParse(start.Text, out startddate)) {
   // do some error handling
}
if (!DateTime.TryParse(end.Text, out enddate)) {
   // do some error handling
sqladapt.SelectCommand.Parameters.AddWithValue("@startyear",startdate)
sqladapt.SelectCommand.Parameters.AddWithValue("@endyear", enddate)
...

Also you can set the data type of the parameter when adding the parameter. See OleDbParameterCollection.Add Method (String, OleDbType) (System.Data.OleDb)[^]

ADDITION:
---------
VB version

VB
Dim startdate As DateTime
Dim enddate As DateTime
If (Not DateTime.TryParse(start.Text, startdate)) Then
    ' do some error handling
End If
If (Not DateTime.TryParse(end.Text, enddate)) Then
    '' do some error handling
End If
sqladapt.SelectCommand.Parameters.AddWithValue("@startyear", startdate)
sqladapt.SelectCommand.Parameters.AddWithValue("@endyear", enddate)
 
Share this answer
 
v2
Comments
Member 11856456 12-Apr-18 11:09am    
I will try this when I get in, I will just have to convert from C# to vb.net. will vb.net have "out" as part of their choices? You have if (!DateTime.TryParse(start.Text, out startddate)) which has out in it.
Wendelius 12-Apr-18 13:07pm    
Sorry, didn't notice the VB tag. VB version now added to the solution.

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