如何从sql server数据库中查询datetime类型的数据?详细些!!!

我想查询出表中的一段时间内的数据 按时间查询 时间的数据类型为datetime
求各位大虾指点!!!

这是查询与当前时间在同一周内的数据,sql语句是:
select * from 表 where datediff(week,时间字段,getdate())=0

也可以稍作修改用来查询与指定日期在同一周内的数据:
select * from 表 where datediff(week,时间字段,'2002-01-01')=0
像你的这个表,查询一段时间(需要指定前后日期的)
select * from 表 where startdate between 指定日期 and 指定日期
select * from 表 where leavedate between 指定日期 and 指定日期
查询一段时间( 不需要固定时间的,只需要前后间隔时间的)
select * from 表 where datediff(day,startdate,leavedate)<7 and startdate = 指定日期select * from 表 where datediff(day,startdate,leavedate)<7 and leavedate = 指定日期
这是查询此日期开始时间到离开时间在七天之间的所有数据,后面的and部分按需要加。追问

<% Connection con;
Statement sql;
ResultSet rs;

String strId=request.getParameter("id");

String strStartTime=request.getParameter("startTime");
String strLeaveTime=request.getParameter("leaveTime");

if(strId==null){
strId="";
}

if(strStartTime==null){
strStartTime="";
}
if(strLeaveTime==null){
strLeaveTime="";
}

追答

你要问的是什么问题?是你的代码出什么问题了吗?还是你的查询语句不行?

温馨提示:答案为网友推荐,仅供参考
第1个回答  推荐于2018-03-20
select * from 表名 where 字段名 between '2012-6-7' and '2012-6-27'
比如查询7号到27号的数据,注意是引号。表名你自己知道,字段名在这张表里是startTime或LeaveTime本回答被网友采纳
第2个回答  2020-06-02
where CAST(时间字段 as date)=xxx
或者
where CAST(时间字段 as date) between '2020-05-01'and'2020-06-01'
相似回答