如何搜索记录数月 [英] how to search record for months

查看:54
本文介绍了如何搜索记录数月的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用此代码日期搜索数据。



我想逐月搜索gridview中的所有记录。

I use this code for searching data date wise.

I want to search all the records in gridview month wise.

DateTime date = Convert.ToDateTime(TextBox1.Text);
SqlCommand cmd = new SqlCommand("SELECT * FROM Tech_data where Call_assign = @Call_assign and dateadd(dd, datediff(dd,0, [date_time]), 0) = @date_time  ");

cmd.Connection = con;
cmd.Parameters.Add("@Call_assign", SqlDbType.VarChar).Value = DropDownList1.SelectedValue;
cmd.Parameters.Add("@date_time", SqlDbType.DateTime).Value = date.Date;

SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
Tech_grid.DataSource = ds;
Tech_grid.DataBind();

推荐答案

我无法确定你要对条款做什么: dateadd(dd,datediff(dd,0,[date_time]),0)。它只是将date_time列的日期/时间与@date_time参数进行比较,同时使用一些额外的处理器周期。由于DateTime数据类型精确到几分之一秒,因此您获得匹配的机会很小。



以下选择同一月份的行和year作为@date_time参数。这是你的问题吗?

I have not been able to determine what you are trying to do with the clause: dateadd(dd, datediff(dd,0, [date_time]), 0). It just compares the date/time of the date_time column to the @date_time parameter while using a few extra processor cycles. Since DateTime DataTypes are accurate to fractions of a second, the chances are small that you will get a match.

The following selects those rows that are in the same month and year as the @date_time parameter. Is this your question?
SELECT * FROM Tech_data where Call_assign = @Call_assign and MONTH(date_time)=MONTH(@date_time) and YEAR(date_time)=YEAR(@date_time)





经过测试:SQL Server 2012


这篇关于如何搜索记录数月的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆