在sqlserver中求一SQL语句

快跑蜗牛哥 2008-01-03 08:11:33
已知表 work
id topdate name
1 2007/12/29 上午 10:26:35 q
2 2007/12/29 上午 10:32:47 a
3 2007/12/29 上午 10:32:48 c
4 2008/1/2 上午 10:21:31 s
5 2008/1/2 上午 10:24:36 d
6 2008/1/2 上午 10:25:31 f
求在某个时间段内的值(其中这个时间段要包含 年月日时分秒)
其中topdate 类型为datetime
...全文
74 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
JL99000 2008-01-04
  • 打赏
  • 举报
回复
很迷茫
dawugui 2008-01-03
  • 打赏
  • 举报
回复
create table tb(id int , topdate datetime , name varchar(10))
insert into tb values(1, '2007/12/29 10:26:35', 'q')
insert into tb values(2, '2007/12/29 10:32:47', 'a')
insert into tb values(3, '2007/12/29 10:32:48', 'c')
insert into tb values(4, '2008/1/2 10:21:31', 's')
insert into tb values(5, '2008/1/2 10:24:36', 'd')
insert into tb values(6, '2008/1/2 10:25:31', 'f')
go

select * from tb where convert(varchar(19) , topdate , 120) >= '2008-01-02 00:00:00' and convert(varchar(19) , topdate , 120) <= '2008-01-02 23:59:59'
/*
id topdate name
----------- ----------------------- ----------
4 2008-01-02 10:21:31.000 s
5 2008-01-02 10:24:36.000 d
6 2008-01-02 10:25:31.000 f

(3 行受影响)
*/

select * from tb where convert(varchar(19) , topdate , 120) >= '2008-01-02 00:00:00' and convert(varchar(19) , topdate , 120) <= '2008-01-02 10:24:36'
/*
id topdate name
----------- ----------------------- ----------
4 2008-01-02 10:21:31.000 s
5 2008-01-02 10:24:36.000 d

(2 行受影响)
*/

drop table tb

dawugui 2008-01-03
  • 打赏
  • 举报
回复
select * from work where convert(varchar(19) , topdate , 120) >= '2008-01-03 00:00:00' and convert(varchar(19) , topdate , 120) <= '2008-01-03 23:59:59'
中国风 2008-01-03
  • 打赏
  • 举报
回复
select * from work where topdate> ='startdate' and topdate <='enddate' --
---

pt1314917 2008-01-03
  • 打赏
  • 举报
回复

declare @stardate datetime
declare @enddate datetime
set @stardate=''
set @enddate=''
select * from work where topdate between @stardate and @enddate
中国风 2008-01-03
  • 打赏
  • 举报
回复
select * from work where topdate between 'startdate' and 'enddate'
--
select * from work where topdate>='startdate' and 'startdate'<='enddate'

34,576

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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