选择时间戳之间的数据

时间:2015-06-15 20:36:58

标签: sql-server

我在sql名称[time]中有一个字段,数据类似于

Checkperson           time
...........................

divye                9:30 AM
nitesh               9:30 AM
abhishek             10:30 AM
 Rajendra            9:30 AM
 ravindra            12:30 PM
 Balwant              11:30 AM

我需要时间戳上午9:30到11:30之间的数据

result like 

divye                9:30 AM
nitesh               9:30 AM
abhishek             10:30 AM
Rajendra              9:30 AM
Balwant              11:30 AM

2 个答案:

答案 0 :(得分:1)

假设您的时间字段为varchar,您可以将varchar字段转换为时间格式(样式114)并使用BETWEEN运算符,如下所示:

SELECT  t1.CheckPerson ,
        t1.new_time AS [time]
FROM    ( SELECT    t.* ,
                    CONVERT(VARCHAR(20), CAST(LTRIM(RTRIM(LEFT([time], 5))) AS TIME), 114) 
                    AS new_time
          FROM      timetable t
        ) t1
WHERE   t1.new_time BETWEEN '09:30:00.0000000'
                    AND     '11:30:00.0000000'

这是一个SQL小提琴演示:

SQL Fiddle

<强>更新

根据您在评论中提供的查询,您可以在查询中使用此代码,如下所示:

SELECT  city ,
        place ,
        [open]
FROM    ( SELECT    city ,
                    place ,
                    CONVERT(VARCHAR(20), CAST(LTRIM(RTRIM(LEFT([open], 5))) AS TIME), 114) 
                    AS [open]
          FROM      staff
        ) q1
WHERE   q1.city LIKE '%%'
        AND q1.place LIKE '%park%'
        AND q1.[open] BETWEEN '10:30:00.0000000'
                      AND     '11:45:00.0000000';

答案 1 :(得分:0)

select *
from tablename
where CAST(Created as time) >= '09:30:00' or CAST(Created as time) < '11:30:00'

select * 
from tablename 
WHERE CONVERT(VARCHAR(20),Date,108) between '09:30:00' and '11:30:00'