我如何根据日计算数据?

时间:2012-03-07 07:02:46

标签: c# asp.net sql

我喜欢count job_id

  • js_id int
  • job_id int
  • apply_date smalldatetime

代码:

string cmdstr3 = "SELECT COUNT(job_id) FROM applied_jobs 
                  WHERE js_id='" + jsId + "' AND apply_date='" + DateTime.Now.Date + "'";`

string cmdstr4 = "SELECT COUNT(job_id) FROM applied_jobs 
                  WHERE js_id='" + jsId + "' AND (apply_date >='" + start_date.Date + "' AND apply_date <'" + end_date.Date+ "')";

但它计为0

我有这样的数据

3/6/2012 12:00:00 AM
3/6/2012 12:00:00 AM
3/5/2012 12:00:00 AM

1 个答案:

答案 0 :(得分:1)

您似乎想要获取某个js_id的当前日期的条目数。这就是你应该做的事情:

var cmd = new SqlCommand("SELECT COUNT([job_id]) FROM [applied_jobs] WHERE [js_id] = @jsid AND datediff(day,[apply_date], @applydate) = 0",cn);
cmd.Parameters.Add("@jsid", SqlDbType.Int).Value = jsId;
cmd.Parameters.Add("@applydate", SqlDbType.SmallDateTime).Value = DateTime.Now.Date;