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

andy2001p 2003-08-11 01:50:22
+++求一个SQL语句：查询time介于time1和time2之间的记录~!+++
...全文
10 点赞 收藏 9

9 条回复

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

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

2.1w+

MS-SQL Server 疑难问题

2003-08-11 01:50