简体   繁体   中英

SELECT WHERE date time greater than day (and not necessarily time)

I have a table which has a datetime column. I wish to return all the records whose date is greater than the input date. I used a date picker control for input date.

In other words, if my data table has only four rows,

2014-04-23 10:40:27.592 Abby
2014-05-23 09:40:27.592 Bobby
2014-06-01 08:53:59.320 Cathy
2014-06-05 08:53:59.320 Debbie

When my DatePicker control has input of 2014-06-03 , the output should be:

2014-06-05 08:53:59.320 Debbie

The convert the value of dateTimePicker Control to string before calling the select method.

string FromDate = dateTimePickerFromDate.ToString()

In my select method, I have:

if (fromDate != null)
{
    return GetSQLDataSet(" [employeeKnownAs]  + ' ' +[employeeLastName] AS 'empName', [InsertDate]", "EmpData", "", "EmpData.InsertDate >= " + fromDate.ToString(), "InsertDate ASC", DbConnect.myDataConnection);

Unfortunately, it return null no matter what dates I choose from the dateTimePicker.

因此,您需要将datetime转换为正确的字符串?

string FromDate = dateTimePickerFromDate.ToString("yyyy-MM-dd")

Your DatePicker anyways returns DATE part only as you have mentioned in your post my DatePicker control has input of 2014-06-03 . So, you will have to CAST your SQL DATETIME value to DATE in your SQL query. Your Query should look something like

select 
[employeeKnownAs],
[employeeLastName] AS 'empName', 
[InsertDate]
from EmpData
where cast(InsertDate as DATE) >=  fromDate.ToString()

You can as well use SQL SERVER CONVERT() function with style format 101 like

CONVERT(VARCHAR,InsertDate,101) >= fromDate.ToString()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM