简体   繁体   中英

Select keyword require > instead of =

if i use = instead of > in line below TextBox1.Text = "regdate > '12/12/2012 8:27:09'";

it return me zero rows . Any reason ?

        DataView dv1 = new DataView ();
        DataSet ds = new DataSet ();
        DataTable dt = new DataTable();

        /*
         col1   col2            col3    col4            col5
         1      sw@yahoo.com    sw      Stephen Walther 12/12/2012 8:27:09 PM
         2      as@yahoo.com    as      Al Stevens      12/12/2012 8:27:09 PM
        */

// Here is the code and criteria for selecting few rows based on datetime .

        TextBox1.Text = "regdate > '12/12/2012'";

        //connection created 

        SqlConnection con = new SqlConnection(SqlDataSource1.ConnectionString);
        con.Open();
        SqlDataAdapter da = new SqlDataAdapter ("SELECT * FROM users",con);
        da.Fill (ds);

        // filtering criteria applied .
        DataRow[] dr = ds.Tables["Table"].Select(TextBox1.Text);

         //columns created
        dt.Columns.Add("col1", typeof(int));
        dt.Columns.Add("col2", typeof(string));
        dt.Columns.Add("col3", typeof(string));
        dt.Columns.Add("col4", typeof(string));
        dt.Columns.Add("col5", typeof(DateTime));

         // data added
        foreach (DataRow item in dr)
        {
            dt.Rows.Add(item.ItemArray);
        }

        // view created
        dv1 = dt.DefaultView ;
        GridView1.DataSourceID = string.Empty; 
        GridView1.DataSource = dv1 ;
        Page.DataBind(); 

Because the time component is stopping the match - it is trying to match the date AND time.

You could try to use some kind of DATEDIFF function to just match the date portion

How to test two datetimes for equality in T-SQL (ignoring their time components)?

BUT - when amending the code just be really careful with this - not to start putting text box input into the SQL statement. Injection attack risk aplenty.

I think you are OK as it stands but....

Because it's a datetime field, so it will compare the whole date and time value, not just the date.

The value 12/12/2012 8:27:09 PM and 12/12/2012 are not the same, because the 12/12/2012 gets expanded to the 12/12/2012 12:00:00 AM value to be comparable to the datetime value. The datetime value doesn't get truncated to be comparable to the date value.

Generally when data types doesn't match for a comparison or calculation, the smaller type gets expanded to the larger type, so that there is no unintentional data loss.

The database stores the date in (about) millisecond resolution. When "12/12/2012 8:27:09 PM" is displayed, there is probably a millisecond part that is not displayed, but will throw off a comparison with a date that doesn't specify those milliseconds.

The ">" comparison will succeed because the stored date is higher than your specified date, precisely because of those milliseconds.

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