前言
在项目中,需要使用模糊查询来查询当天或某个时间段的信息记录,已知在数据库中使用的是datetime时间类型。
方法总结:
方法一(用于查询当天的某项记录,专为date类型定制的快捷的方法): string sql = "SELECT count(*) from StudentBindPaperTypeEntity where IsUse ='1' and
DateDiff(day,TimeTamp,getdate())=0 and ExamModel='练习模式' ";
方法二(注意,最好在参数中设置%,否则容易报错):
SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@ip","%" + StudentBindPaperType.IP + "%"),
new SqlParameter("@date","%" + date+ "%") };
string sql = "SELECT count(*) from StudentBindPaperTypeEntity where IsUse ='1' and
TimeTamp like @date and ExamModel='练习模式' and IP like @ip";
方法三(适用于任何数据类型):
string sql = "select * from ScoreEntity WHERE score>@score and convert(varchar,TimeTamp,120)
like '@date%' and ExamModel='练习模式'
方法四(适用于除string外的类型):
select * from table1 whereTimeTamp between '2018-12-29 0:00:00' and '2018-12-29 24:59:59'";