取决两个日期之间的SQL
介于两个日期之间的SQL
表里面字段:time (nvarchar(50))
得到2个日期 string t1="2012-12-5"
string t1="2012-12-10"
怎么得到这2个日期之间的数据?
select * from table where time ??
<> ?
between and?
------解决方案--------------------
where cast([time] as datetime) between @t1 and @t2
------解决方案--------------------
where [time] between @t1 and @t2
------解决方案--------------------
------解决方案--------------------
------解决方案--------------------
相等於
------解决方案--------------------
在数据库中可以这样做:
declare @t1 varchar(20),@t2 varchar(20)
set @t1 = '2012-12-5'
set @t2 = '2012-12-10'
select * from tb where convert(time,datetime) between convert(datetime,@t1) and convert(datetime,@t2)
表里面字段:time (nvarchar(50))
得到2个日期 string t1="2012-12-5"
string t1="2012-12-10"
怎么得到这2个日期之间的数据?
select * from table where time ??
<> ?
between and?
------解决方案--------------------
where cast([time] as datetime) between @t1 and @t2
------解决方案--------------------
where [time] between @t1 and @t2
------解决方案--------------------
select * from tb where convert(datetime,@str1,120) and convert(datetime,@str2,120)
------解决方案--------------------
USE test
GO
---->生成表tb
--
--if object_id('tb') is not null
-- drop table tb
--Go
--Create table tb([time] NVARCHAR(50))
--Insert into tb
--Select '2012-12-5 11:05:48'
--Union all Select '2012-12-5 11:08:29'
--Union all Select '2012-12-5 11:05:48'
DECLARE @t1 NVARCHAR(50) -- string
,@t2 NVARCHAR(50) -- string
SELECT @t1='2012-12-5'
,@t2='2012-12-10'
SELECT * FROM tb WHERE time BETWEEN CONVERT(DATETIME,@t1) AND CONVERT(DATETIME,@t2)
/*
time
-------------------
2012-12-5 11:05:48
2012-12-5 11:08:29
2012-12-5 11:05:48
*/
------解决方案--------------------
time BETWEEN CONVERT(DATETIME,@t1) AND CONVERT(DATETIME,@t2)
相等於
time >= CONVERT(DATETIME,@t1) AND time <= CONVERT(DATETIME,@t2)
------解决方案--------------------
在数据库中可以这样做:
declare @t1 varchar(20),@t2 varchar(20)
set @t1 = '2012-12-5'
set @t2 = '2012-12-10'
select * from tb where convert(time,datetime) between convert(datetime,@t1) and convert(datetime,@t2)