做机房重构时,需要查询某天的账单,用“日期”为条件查询该天的所有数据,那么应该如何获取当天的时间段呢?
方法一:Between...and...
BETWEEN 操作符在 WHERE 子句中使用,作用是选取介于两个值之间的数据范围。
首先获取两个时间节点:
dtp1.Value = DateTime.Parse(DateTime.Now.Date.ToString() + " 00:00:00");
dtp2.Value = DateTime.Parse(DateTime.Now.Date.ToString() + " 23:59:59");
SQL语句:
select * from table1 where Date between 'dtp1' and 'dtp2'";
方法二:时间类型的模糊查询,可以任意查询某天
@date1 = date.ToString("yyyy-MM-dd")
select * from table1 where convert(varchar,Date,120) LIKE @date1+'%';
注:在使用时间类型的模糊查询时,需要把时间的类型转换为字符串类型