+++求一个SQL语句:查询time介于time1和time2之间的记录~!+++

andy2001p 2003-08-11 01:50:22
+++求一个SQL语句:查询time介于time1和time2之间的记录~!+++
...全文
55 9 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
dearwader 2003-08-13
  • 打赏
  • 举报
回复
写个例子启发,这种方法在程序中使用更见优势

1。创建一个自定义函数(因为经常会用到)
CREATE FUNCTION [dbo].[UF_DatetimeToInt] (@dtDateTime datetime)
RETURNS int
AS--*****--
BEGIN
declare @iDateInt int

set @iDateInt = year(@dtDateTime)*10000 + month(@dtDateTime)*100 + day(@dtDateTime)
return @iDateInt

END


2。写SQL语句
select * from tab
where dbo.UF_DatetimeToInt(time) > dbo.UF_DatetimeToInt(time1)
and dbo.UF_DatetimeToInt(time) < dbo.UF_DatetimeToInt(time2)
dafu71 2003-08-13
  • 打赏
  • 举报
回复
Select * from 表 where Time between time1 and time2
pengdali 2003-08-11
  • 打赏
  • 举报
回复
Select * from 表 where 时间列 between @time1 and @time2


Select * from 表 where 时间列 between '1999-1-1 10:10:10' and '1999-1-31 10:10:10'
andy2001p 2003-08-11
  • 打赏
  • 举报
回复
time,time1,time2 均是DateTime 类型 , 我要比较的是全部!
pengdali 2003-08-11
  • 打赏
  • 举报
回复
Select * from 表 where 时间列 between @time1 and @time2


Select * from 表 where 时间列 between '1999-1-1' and '1999-1-31'
edisonwong 2003-08-11
  • 打赏
  • 举报
回复
假设 time,time1,time2 均是DateTime 类型,只比较时间部分
select * from Tab where right(convert(varchar(20),time,120),8)
between right(convert(varchar(20),time1,120),8)
and right(convert(varchar(20),time2,120),8)
愉快的登山者 2003-08-11
  • 打赏
  • 举报
回复
select * from yourtable where [time] between @time1 and @time2
select * from yourtable where [time]>=@time1 and [time]<=@time2

txlicenhe 2003-08-11
  • 打赏
  • 举报
回复
Select * from yourTable where Time between time1 and time2
97866 2003-08-11
  • 打赏
  • 举报
回复
Select * from 表 where Time between time1 and time2

or

Select * from 表 where Time>=time1 and Time<=time2

22,302

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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