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

andy2001p 2003-08-11 01:50:22
+++求一个SQL语句:查询time介于time1和time2之间的记录~!+++
...全文
10 点赞 收藏 9
写回复
9 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
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)
回复
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

回复
发帖
疑难问题
创建于2007-09-28

2.1w+

社区成员

MS-SQL Server 疑难问题
申请成为版主
帖子事件
创建了帖子
2003-08-11 01:50
社区公告
暂无公告