34,597
社区成员
发帖
与我相关
我的任务
分享
select * from ta where cast(convert(varchar,t1,108) as datetime) between '08:00' and '21:00'
declare @t table (t1 varchar(30),t2 varchar(30))
insert @t
select '2009-6-2 11:41:00','2009-6-20 21:41:00' union all
select '2001-3-4 09:22:12','2009-4-15 20:11:11' union all
select '2001-3-4 07:22:12','2009-4-15 17:11:11'
select *
from @t
where convert(char(10),cast(t1 as datetime),114) >= '08:00:00:0' and convert(char(10),cast(t1 as datetime),114) < '21:00:00:0'
(所影响的行数为 3 行)
/*
t1 t2
------------------------------ ------------------------------
2009-6-2 11:41:00 2009-6-20 21:41:00
2001-3-4 09:22:12 2009-4-15 20:11:11
(所影响的行数为 2 行)
*/
declare @str varchar(30)
set @str='2009-6-2 11:41:00'
select cast(@str as datetime)
select convert(char(10),cast(@str as datetime),120)
/*
------------------------------------------------------
2009-06-02 11:41:00.000
(所影响的行数为 1 行)
----------
2009-06-02
(所影响的行数为 1 行)
*/
select * from ta
where
convert(varchar(10) ,cast(t1 as datetime),108)
between '08:00' and '21:00'
If not object_id('[ta]') is null
Drop table [ta]
Go
Create table [ta]([t1] nvarchar(18),[t2] nvarchar(19))
Insert ta
Select '2009-6-2 11:41:00','2009-6-20 21:41:00' union all
Select '2009-6-3 11:41:00','2009-6-21 21:41:00' union all
Select '2009-6-4 09:41:00','2009-6-22 21:41:00' union all
Select '2009-6-5 10:41:00','2009-6-23 21:41:00' union all
Select '2009-6-6 22:41:00','2009-6-21 21:41:00'
Go
--Select * from ta
-->SQL查询如下:
select *
from ta
where cast(convert(varchar,cast(t1 as datetime),108) as datetime) between '08:00' and '21:00'
/*
t1 t2
------------------ -------------------
2009-6-2 11:41:00 2009-6-20 21:41:00
2009-6-3 11:41:00 2009-6-21 21:41:00
2009-6-4 09:41:00 2009-6-22 21:41:00
2009-6-5 10:41:00 2009-6-23 21:41:00
(4 行受影响)
*/
select * from tb where convert(varchar(5),cast(t1 as datetime),108) >= '08:00'
and convert(varchar(5),cast(t2 as datetime),108)<='21:00'
select * from tb where datepart(Hour, convert(varchar(20),cast(t1 as datetime),108)) between 8 and 21
select * from ta where cast(convert(varchar,t1,108) as datetime) between '08:00' and '21:00'
--*******************************************************************************
-- 日期转换参数大全
--********************************************************************************
select CONVERT(varchar,getdate(),120)
--2009-03-15 15:10:02
select CONVERT(varchar(10),getdate(),120)
--2009-03-15
select replace(replace(replace(CONVERT(varchar, getdate(), 120 ),'-',''),' ',''),':','')
--20090315151201
select CONVERT(varchar(12) , getdate(), 111)
--2009/03/15
select CONVERT(varchar(12) , getdate(), 112)
--20090315
select CONVERT(varchar(12) , getdate(), 102)
--2009.03.15
select CONVERT(varchar(12) , getdate(), 108)
--15:13:26
其它我不常用的日期格式转换方法:
select CONVERT(varchar(12) , getdate(), 101 )
--03/15/2009
select CONVERT(varchar(12) , getdate(), 103 )
--15/03/2009
select CONVERT(varchar(12) , getdate(), 104 )
--15.03.2009
select CONVERT(varchar(12) , getdate(), 105 )
--15-03-2009
select CONVERT(varchar(12) , getdate(), 106 )
--15 03 2009
select CONVERT(varchar(12) , getdate(), 107 )
-- 15, 2009
select CONVERT(varchar(12) , getdate(), 109 )
--03 15 2009
select CONVERT(varchar(12) , getdate(), 110 )
--03-15-2009
select CONVERT(varchar(11) , getdate(), 113 )
--15 03 2009
select CONVERT(varchar(12) , getdate(), 114)
--15:15:55:717