select * from tab1 where convert(varchar(10),date_time,108) between '09:00:00' and '10:30:00'
select * from tab1 where convert(nvarchar(10),date_time,108) between '09:00:00' and '10:30:00'
--理解错了:
select * from tab1 where convert(varchar(10),date_time,108)='09:00:00' or convert(varchar(10),date_time,108)='10:30:00'
select * from tab1 where convert(varchar(10),date_time,108) in ('09:00:00','10:30:00')
这样还行啊?
select * from tab1
where substring(date_time,12,8) between '09:00:00' and '10:30:00'
select * from tab1
where substring(date_time,12,8) = '09:00:00' or substring(date_time,12,8)='10:30:00'
select convert(varchar(10),getdate(),108)
--10:06:46 当前时间,这个是转换datetime为时间字符串的函数
select * from tab1
where substring(date_time,12,8) between '09:00:00' and '10:30:00'
---------------------------------
-- Author: htl258(Tony)
-- Date : 2009-07-29 09:35:13
---------------------------------
--> 生成测试数据表:tab1
If not object_id('[tab1]') is null
Drop table [tab1]
Go
Create table [tab1]([date_time] datetime,[action] nvarchar(4))
Insert tab1
Select '2009-6-20 09:00:00','坐车出去' union all
Select '2009-6-20 10:30:00','唱歌' union all
Select '2009-6-20 12:00:00','吃饭' union all
Select '2009-6-20 12:30:00','看报纸' union all
Select '2009-6-20 14:00:00','回家' union all
Select '2009-6-21 09:00:00','上班' union all
Select '2009-6-21 10:00:00','写报告' union all
Select '2009-6-21 10:30:00','开会'
Go
--Select * from tab1
-->SQL查询如下:
select *
from tab1
where cast(convert(varchar,[date_time],8) as datetime) between '09:00' and '10:30'
/*
date_time action
----------------------- ------
2009-06-20 09:00:00.000 坐车出去
2009-06-20 10:30:00.000 唱歌
2009-06-21 09:00:00.000 上班
2009-06-21 10:00:00.000 写报告
2009-06-21 10:30:00.000 开会
(5 行受影响)
*/
select * from tab1
where substring(date_time,12,8) = '09:00:00' or substring(date_time,12,8)='10:30:00'
select * from tab1
where convert(varchar(10),date_time,108)='09:00:00' or convert(varchar(10),date_time,108)='10:30:00'
select * from tab1 where convert(varchar(10),date_time,108)='09:00:00' or convert(varchar(10),date_time,108)='10:30:00'
select * from tab1
where substring(date_time,12,8) ='09:00:00' or substring(date_time,12,8) ='10:30:00'
如果只取9:00与10:30两个时间,这样.
---------------------------------
-- Author: htl258(Tony)
-- Date : 2009-07-29 09:35:13
---------------------------------
--> 生成测试数据表:tab1
If not object_id('[tab1]') is null
Drop table [tab1]
Go
Create table [tab1]([date_time] datetime,[action] nvarchar(4))
Insert tab1
Select '2009-6-20 09:00:00','坐车出去' union all
Select '2009-6-20 10:30:00','唱歌' union all
Select '2009-6-20 12:00:00','吃饭' union all
Select '2009-6-20 12:30:00','看报纸' union all
Select '2009-6-20 14:00:00','回家' union all
Select '2009-6-21 09:00:00','上班' union all
Select '2009-6-21 10:00:00','写报告' union all
Select '2009-6-21 10:30:00','开会'
Go
--Select * from tab1
-->SQL查询如下:
select *
from tab1
where cast(convert(varchar,[date_time],8) as datetime) in('09:00','10:30')
/*
date_time action
----------------------- ------
2009-06-20 09:00:00.000 坐车出去
2009-06-20 10:30:00.000 唱歌
2009-06-21 09:00:00.000 上班
2009-06-21 10:30:00.000 开会
(4 行受影响)
*/
If not object_id('[tab1]') is null
Drop table [tab1]
Go
Create table [tab1]([date_time] datetime,[action] nvarchar(4))
Insert tab1
Select '2009-6-20 09:00:00','坐车出去' union all
Select '2009-6-20 10:30:00','唱歌' union all
Select '2009-6-20 12:00:00','吃饭' union all
Select '2009-6-20 12:30:00','看报纸' union all
Select '2009-6-20 14:00:00','回家' union all
Select '2009-6-21 09:00:00','上班' union all
Select '2009-6-21 10:00:00','写报告' union all
Select '2009-6-21 10:30:00','开会'
Go
select * from tab1
where convert(varchar(10),date_time,108)='09:00:00' or convert(varchar(10),date_time,108)='10:30:00'
date_time action
----------------------- ------
2009-06-20 09:00:00.000 坐车出去
2009-06-20 10:30:00.000 唱歌
2009-06-21 09:00:00.000 上班
2009-06-21 10:30:00.000 开会
select * from tab1 where date_time like'%09:00:00%' or date_time like'%10:30:00'
declare @tabl table (date_time datetime,action nvarchar(20))
insert into @tabl select '2009-6-20 09:00:00','坐车出去'
union all select '2009-6-20 10:30:00','唱歌'
union all select '2009-6-20 12:00:00','吃饭'
union all select '2009-6-20 12:30:00','看报纸'
union all select '2009-6-20 14:00:00','回家'
union all select '2009-6-21 09:00:00','上班'
union all select '2009-6-21 10:00:00','写报告'
union all select '2009-6-21 10:30:00','开会'
select * from @tabl where CONVERT(nvarchar(10),date_time ,108) between '09:00:00' and '10:30:00'
date_time action
----------------------- --------------------
2009-06-20 09:00:00.000 坐车出去
2009-06-20 10:30:00.000 唱歌
2009-06-21 09:00:00.000 上班
2009-06-21 10:00:00.000 写报告
2009-06-21 10:30:00.000 开会
(5 行受影响)