sql,时间段between and 时间段查询

jxftp 2014-01-27 02:49:50
不知道为什么查询不出来,
表中有一两个字段,开始时间(Fstartdate),结束时间(Fenddate)
如2013-1-27 ,2014-1-27
今天是1月27日,我希望能查到今天的记录,我查询的条件是:
select * from tablename where 1=1 and getdate() between fstartdate and Fenddate

结果没有显示


我换成:
select * from tablename
where 1=1 and
getdate() between cast(convert(varchar(10),Fstartdate,120) as datetime)+''
and cast(convert(varchar(10),Fenddate,120)+'23:59:59' as datetime)

也没有显示,不知道为什么?
...全文
939 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
發糞塗牆 2014-01-27
  • 打赏
  • 举报
回复
select * from tablename 
where 1=1 and
getdate() between cast(convert(varchar(30),Fstartdate,120) as datetime)+' 00:00:00.000' 
and cast(convert(varchar(30),Fenddate,120)+' 23:59:59.997' as datetime)
jxftp 2014-01-27
  • 打赏
  • 举报
回复
datetime
發糞塗牆 2014-01-27
  • 打赏
  • 举报
回复
select * from tablename where 1=1 and getdate() between cast(convert(varchar(10),Fstartdate,120) as datetime)+'' and cast(convert(varchar(10),Fenddate,120)+'23:59:59' as datetime) 如果用这个,需要换成这样: select * from tablename where 1=1 and getdate() between cast(convert(varchar(10),Fstartdate,120) as datetime)+' 00:00:00.000' and cast(convert(varchar(10),Fenddate,120)+' 23:59:59.997' as datetime)
  • 打赏
  • 举报
回复

create table test(fstartdate datetime,Fenddate datetime)
insert into test values('2013-1-27','2014-1-27')
select * from test where 1=1 and getdate() between fstartdate and dateadd(day,1,Fenddate)
/*
2013-01-27 00:00:00.000	2014-01-27 00:00:00.000
*/

fstartdate='2013-01-27 00:00:00.000'
Fenddate='2014-01-27 00:00:00.000'
getdate()='2014-01-27 15:02:21.950'
LongRui888 2014-01-27
  • 打赏
  • 举报
回复
改成这样试试,加一个空格: select * from tablename where 1=1 and getdate() between cast(convert(varchar(10),Fstartdate,120) as datetime)+'' and cast(convert(varchar(10),Fenddate,120)+' 23:59:59' as datetime)
  • 打赏
  • 举报
回复
select * from tablename where 1=1 and getdate() between fstartdate and dateadd(day,1,Fenddate)
發糞塗牆 2014-01-27
  • 打赏
  • 举报
回复
select * from tablename where 1=1 and fstartdate<= getdate() AND  Fenddate>=GETDATE()
这样呢?你的fstartdate是什么数据类型?

27,579

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧