oracle数据库:在一天的特定时间之间选择

时间:2016-05-02 18:21:44

标签: sql oracle

column name    data type
wp_stime         DATE

QUERY:

select wp_stime from workpaths;

输出:

29-FEB-12
29-FEB-12
24-FEB-12
24-FEB-12
31-OCT-11
12-DEC-11
12-JAN-11
19-OCT-11
19-OCT-11
11-AUG-11
19-OCT-11
21-NOV-11
28-JUL-11
02-AUG-11
01-MAR-12
01-MAR-12
01-MAR-12
01-MAR-12
01-MAR-12
01-MAR-12
01-MAR-12
01-MAR-12
01-MAR-12
01-MAR-12
01-MAR-12
01-MAR-12
01-MAR-12
01-MAR-12
01-MAR-12

问题是如何根据wp_stime的上午9点到下午5点进行选择?

QUERY 2:

select to_char(wp_stime, 'HH24:MI:SS') from workpaths;

输出:

00:09:20
00:10:17
00:11:26
00:42:50
00:44:32
00:45:35
00:48:47
00:51:45
00:53:01
00:56:43
01:31:39
01:34:01
14:49:30
01:53:32
02:06:37
02:36:54
02:51:12

3 个答案:

答案 0 :(得分:3)

trunc(wp_stime)将返回相同的日期,时间部分被截断为00:00:00。然后wp_stime - trunc(wp_stime)将返回"小数部分" (wp_stime的时间部分)。这表示为以天为单位的数字。您希望此小数部分为9到17小时,或9/24和17/24天。

这是查询,在WITH子句中以测试数据开头。对于您,已经存在的表,完整的查询只是我的代码中的最后一行:

 with workpaths(wp_stime) as (
      select to_date('02-MAY-2016 13:30:44', 'dd-mon-yyyy hh24:mi:ss') from dual union all
      select to_date('15-FEB-2013 17:43:00', 'dd-mon-yyyy hh24:mi:ss') from dual union all
      select to_date('21-DEC-2015  2:15:27', 'dd-mon-yyyy hh24:mi:ss') from dual union all
      select to_date('02-JAN-2016 10:22:09', 'dd-mon-yyyy hh24:mi:ss') from dual
      )
select wp_stime from workpaths where wp_stime - trunc(wp_stime) between 9/24 and 17/24;

<强>输出

WP_STIME
------------------
02-MAY-16 13:30:44
02-JAN-16 10:22:09

答案 1 :(得分:1)

select * from workpaths where to_char(wp_stime,'hh24') between 9 and 16; 

应该有所帮助。 Oracle将从日期字段中提取小时部分作为字符串,并且在看到您与数字进行比较时会隐式地将其转换为数字。因此,您可以比较小时数。实际上,此查询会给出时间大于上午9点且小于下午5点的日期。

编辑:

17替换为16,因此将考虑到16:59:59之前的值。

编辑2:

显式执行字符串到数字转换:

select * from workpaths where to_number(to_char(wp_stime,'hh24')) between 9 and 16; 

答案 2 :(得分:0)

<input type="hidden" id="selectoption" name="selectoption" value="" />

<script>
function myFunction() {
    var x = document.getElementById("mySelect").text;
    document.getElementById("selectoption").value = x;
}
</script>

应该有效。在这种情况下,您使用的是日期数据类型而不是char数据类型。