核心提示:在C#.NET +SQL SERVER程序开发中,使用时间段查询注意:1、第一种时间段查询情况jldate字段在数据库中为datetime(日期+时间展现形式,如2021-2-28 12:14:16)...
在C#.NET +SQL SERVER程序开发中,使用时间段查询注意:
1、第一种时间段查询情况
jldate字段在数据库中为datetime(日期+时间展现形式,如2021-2-28 12:14:16),bdate和edate为DatePicker日期选择文本框名
strsql = "select * from 表名 where (CONVERT(CHAR(10),jldate,23)>='" + bdate.Text + "' or '" + bdate.Text + "'='' ) and (CONVERT(CHAR(10),jldate,23)<='" + edate.Text + "' or '" + edate.Text + "'='' ) ";
2、第二种时间段查询情况
cjdatep字段在数据库中为char字符形式
strsql = "select * from 表名 where CONVERT(datetime, cjdate) >= '" + bdate.Text + "' and CONVERT(datetime, cjdate) <= '" + edate.Text + "' ";
使用下面语法也行:
strsql = "select * from yj where CONVERT(datetime, cjdate) between '" + bdate.Text + "' and '" + edate.Text + "' ";