我有一张表格如下:
Filename - varchar
Creation Date - Date format dd/mm/yyyy hh24:mi:ss
Oldest cdr date - Date format dd/mm/yyyy hh24:mi:ss
如何计算Oracle SQL中两个日期之间的小时分钟和秒(以及可能的天数)的差异?
由于
答案 0 :(得分:108)
您可以在Oracle中减去日期。这将给你几天的差异。乘以24得到小时,依此类推。
SQL> select oldest - creation from my_table;
如果您的日期存储为字符数据,则必须先将其转换为日期类型。
SQL> select 24 * (to_date('2009-07-07 22:00', 'YYYY-MM-DD hh24:mi')
- to_date('2009-07-07 19:30', 'YYYY-MM-DD hh24:mi')) diff_hours
from dual;
DIFF_HOURS
----------
2.5
注意:
此答案适用于Oracle数据类型DATE
表示的日期。
Oracle还有一个数据类型TIMESTAMP
,它也可以表示日期(随时间)。如果您减去TIMESTAMP
个值,则得到INTERVAL
;要提取数值,请使用EXTRACT
函数。
答案 1 :(得分:14)
要在几秒钟内获得结果:
app.import('path/to/jquery/plugin.css)
答案 2 :(得分:13)
declare
strTime1 varchar2(50) := '02/08/2013 01:09:42 PM';
strTime2 varchar2(50) := '02/08/2013 11:09:00 PM';
v_date1 date := to_date(strTime1,'DD/MM/YYYY HH:MI:SS PM');
v_date2 date := to_date(strTime2,'DD/MM/YYYY HH:MI:SS PM');
difrence_In_Hours number;
difrence_In_minutes number;
difrence_In_seconds number;
begin
difrence_In_Hours := (v_date2 - v_date1) * 24;
difrence_In_minutes := difrence_In_Hours * 60;
difrence_In_seconds := difrence_In_minutes * 60;
dbms_output.put_line(strTime1);
dbms_output.put_line(strTime2);
dbms_output.put_line('*******');
dbms_output.put_line('difrence_In_Hours : ' || difrence_In_Hours);
dbms_output.put_line('difrence_In_minutes: ' || difrence_In_minutes);
dbms_output.put_line('difrence_In_seconds: ' || difrence_In_seconds);
end ;
希望这有帮助。
答案 3 :(得分:13)
select
extract( day from diff ) Days,
extract( hour from diff ) Hours,
extract( minute from diff ) Minutes
from (
select (CAST(creationdate as timestamp) - CAST(oldcreationdate as timestamp)) diff
from [TableName]
);
这将为您提供三列,分别为天,小时和分钟。
答案 4 :(得分:6)
您可以使用to_timestamp函数将日期转换为时间戳并执行子操作。
类似的东西:
SELECT
TO_TIMESTAMP ('13.10.1990 00:00:00','DD.MM.YYYY HH24:MI:SS') -
TO_TIMESTAMP ('01.01.1990:00:10:00','DD.MM.YYYY:HH24:MI:SS')
FROM DUAL
答案 5 :(得分:5)
你也可以试试这个:
select to_char(to_date('1970-01-01 00:00:00', 'yyyy-mm-dd hh24:mi:ss')+(end_date - start_date),'hh24:mi:ss')
as run_time from some_table;
它以更易读的形式显示时间,如:00:01:34。 如果您还需要几天,您可以简单地将DD添加到最后的格式化字符串。
答案 6 :(得分:4)
计算从计算机的HIREDATE到系统日期的年龄
SELECT HIREDATE||' '||SYSDATE||' ' ||
TRUNC(MONTHS_BETWEEN(SYSDATE,HIREDATE)/12) ||' YEARS '||
TRUNC((MONTHS_BETWEEN(SYSDATE,HIREDATE))-(TRUNC(MONTHS_BETWEEN(SYSDATE,HIREDATE)/12)*12))||
'MONTHS' AS "AGE " FROM EMP;
答案 7 :(得分:3)
select days||' '|| time from (
SELECT to_number( to_char(to_date('1','J') +
(CLOSED_DATE - CREATED_DATE), 'J') - 1) days,
to_char(to_date('00:00:00','HH24:MI:SS') +
(CLOSED_DATE - CREATED_DATE), 'HH24:MI:SS') time
FROM request where REQUEST_ID=158761088 );
答案 8 :(得分:1)
select (floor(((DATE2-DATE1)*24*60*60)/3600)|| ' : ' ||floor((((DATE2-DATE1)*24*60*60) -floor(((DATE2-DATE1)*24*60*60)/3600)*3600)/60)|| ' ' ) as time_difference from TABLE1
答案 9 :(得分:1)
在oracle 11g中
##############################################################
# Phusion Passenger Standalone uses a template file to
# generate an Nginx configuration file. The original template
# file can be found by running the following command:
#
# ls $(passenger-config about resourcesdir)/templates/standalone/config.erb
#
# You can create a copy of this template file and customize it
# to your liking. Just make sure you tell Phusion Passenger Standalone
# to use your template file by passing the --nginx-config-template
# parameter.
#
# *** NOTE ***
# If you customize the template file, make sure you keep an eye
# on the original template file and merge any changes.
# New Phusion Passenger features may require changes to the template
# file.
##############################################################
master_process on;
worker_processes 1;
daemon on;
error_log '<%= @options[:log_file] %>' <% if @options[:log_level] >= LVL_DEBUG %>info<% end %>;
pid '<%= @options[:pid_file] %>';
<% if Process.euid == 0 %>
<% if @options[:user] %>
<%# Run workers as the given user. The master process will always run as root and will be able to bind to any port. %>
user <%= @options[:user] %> <%= default_group_for(@options[:user]) %>;
<% else %>
<%# Prevent running Nginx workers as nobody. %>
user <%= current_user %> <%= default_group_for(current_user) %>;
<% end %>
<% end %>
events {
worker_connections 1024;
}
http {
log_format debug '[$time_local] $msec "$request" $status conn=$connection sent=$bytes_sent body_sent=$body_bytes_sent';
include '<%= PhusionPassenger.resources_dir %>/mime.types';
<% if @options[:ruby] %>
passenger_ruby <%= @options[:ruby] %>;
<% else %>
passenger_ruby <%= PlatformInfo.ruby_command %>;
<% end %>
<% if @options[:nodejs] %>
passenger_nodejs <%= @options[:nodejs] %>;
<% end %>
<% if @options[:python] %>
passenger_python <%= @options[:python] %>;
<% end %>
passenger_root '<%= PhusionPassenger.install_spec %>';
passenger_abort_on_startup_error on;
passenger_ctl cleanup_pidfiles <%= serialize_strset("#{@working_dir}/temp_dir_toucher.pid") %>;
passenger_ctl integration_mode standalone;
passenger_ctl standalone_engine nginx;
passenger_user_switching off;
<%= nginx_option :passenger_log_level, :log_level %>
<%= nginx_option :passenger_max_pool_size, :max_pool_size %>
<%= nginx_option :passenger_min_instances, :min_instances %>
<%= nginx_option :passenger_pool_idle_time, :pool_idle_time %>
<%= nginx_option :passenger_max_preloader_idle_time, :max_preloader_idle_time %>
<%= nginx_option :passenger_turbocaching, :turbocaching %>
<% if @options[:user] %>
passenger_user <%= @options[:user] %>;
passenger_default_user <%= @options[:user] %>;
passenger_analytics_log_user <%= @options[:user] %>;
<% else %>
passenger_user <%= current_user %>;
passenger_default_user <%= current_user %>;
passenger_analytics_log_user <%= current_user %>;
<% end %>
<% if @options[:instance_registry_dir] %>passenger_instance_registry_dir '<%= @options[:instance_registry_dir] %>';<% end %>
<% if @options[:data_buffer_dir] %>passenger_data_buffer_dir '<%= @options[:data_buffer_dir] %>';<% end %>
<% if @options[:rolling_restarts] %>passenger_rolling_restarts on;<% end %>
<% if @options[:resist_deployment_errors] %>passenger_resist_deployment_errors on;<% end %>
<% if !@options[:load_shell_envvars] %>passenger_load_shell_envvars off;<% end %>
<% if !@options[:friendly_error_pages].nil? -%>
passenger_friendly_error_pages <%= boolean_config_value(@options[:friendly_error_pages]) %>;
<% end %>
<% if @options[:union_station_gateway_address] %>
union_station_gateway_address <%= @options[:union_station_gateway_address] %>;
<% end %>
<% if @options[:union_station_gateway_port] %>
union_station_gateway_port <%= @options[:union_station_gateway_port] %>;
<% end %>
<% if @options[:union_station_gateway_cert] %>
union_station_gateway_cert -;
<% end %>
<% @options[:ctls].each do |ctl| %>
passenger_ctl '<%= ctl.split("=", 2)[0] %>' '<%= ctl.split("=", 2)[1] %>';
<% end %>
default_type application/octet-stream;
types_hash_max_size 2048;
server_names_hash_bucket_size 64;
client_max_body_size 1024m;
access_log off;
keepalive_timeout 60;
underscores_in_headers on;
gzip on;
gzip_comp_level 3;
gzip_min_length 150;
gzip_proxied any;
gzip_types text/plain text/css text/json text/javascript
application/javascript application/x-javascript application/json
application/rss+xml application/vnd.ms-fontobject application/x-font-ttf
application/xml font/opentype image/svg+xml text/xml;
<% if @app_finder.multi_mode? %>
# Default server entry for mass deployment mode.
server {
<% if @options[:ssl] %>
<% if @options[:ssl_port] %>
listen <%= nginx_listen_address %>;
listen <%= nginx_listen_address_with_ssl_port %> ssl;
<% else %>
listen <%= nginx_listen_address %> ssl;
<% end %>
<% else %>
listen <%= nginx_listen_address %>;
<% end %>
root '<%= PhusionPassenger.resources_dir %>/standalone_default_root';
}
<% end %>
<% for app in @apps %>
server {
<% if app[:ssl] %>
<% if app[:ssl_port] %>
listen <%= nginx_listen_address(app) %>;
listen <%= nginx_listen_address_with_ssl_port(app) %> ssl;
<% else %>
listen <%= nginx_listen_address(app) %> ssl;
<% end %>
<% else %>
listen <%= nginx_listen_address(app) %>;
<% end %>
server_name <%= app[:server_names].join(' ') %>;
<% if app[:static_files_dir] %>
root '<%= app[:static_files_dir] %>';
<% else %>
root '<%= app[:root] %>/public';
<% end %>
passenger_app_root '<%= app[:root] %>';
passenger_enabled on;
passenger_app_env <%= app[:environment] %>;
passenger_spawn_method <%= app[:spawn_method] %>;
<% if app[:app_type] %>passenger_app_type <%= app[:app_type] %>;<% end %>
<% if app[:startup_file] %>passenger_startup_file <%= app[:startup_file] %>;<% end %>
<% if app[:concurrency_model] && app[:concurrency_model] != DEFAULT_CONCURRENCY_MODEL %>passenger_concurrency_model <%= app[:concurrency_model] %>;<% end %>
<% if app[:thread_count] && app[:thread_count] != DEFAULT_APP_THREAD_COUNT %>passenger_thread_count <%= app[:thread_count] %>;<% end %>
<% if app[:min_instances] %>passenger_min_instances <%= app[:min_instances] %>;<% end %>
<% if app[:restart_dir] %>passenger_restart_dir '<%= app[:restart_dir] %>';<% end %>
<% if app[:sticky_sessions] %>passenger_sticky_sessions on;<% end %>
<% if app[:sticky_sessions_cookie_name] %>passenger_sticky_sessions_cookie_name '<%= app[:sticky_sessions_cookie_name] %>';<% end %>
<% if app[:vary_turbocache_by_cookie] %>passenger_vary_turbocache_by_cookie '<%= app[:vary_turbocache_by_cookie] %>';<% end %>
<% if app[:union_station_key] %>
union_station_support on;
union_station_key <%= app[:union_station_key] %>;
<% end %>
<% if app[:ssl] %>
ssl_certificate <%= app[:ssl_certificate] %>;
ssl_certificate_key <%= app[:ssl_certificate_key] %>;
<% end %>
<% if @options[:meteor_app_settings] %>
passenger_meteor_app_settings <%= @options[:meteor_app_settings] %>;
<% end %>
<% app[:envvars].each_pair do |name, value| %>
passenger_env_var '<%= name %>' '<%= value %>';
<% end %>
# Rails asset pipeline support.
location ~ "^/assets/.+-([0-9a-f]{32}|[0-9a-f]{64})\..+" {
error_page 490 = @static_asset;
error_page 491 = @dynamic_request;
recursive_error_pages on;
if (-f $request_filename) {
return 490;
}
if (!-f $request_filename) {
return 491;
}
}
location @static_asset {
gzip_static on;
expires max;
add_header Cache-Control public;
add_header ETag "";
}
location @dynamic_request {
passenger_enabled on;
}
}
passenger_pre_start <%= listen_url(app) %>;
<% end %>
答案 10 :(得分:1)
如果您想要看起来更简单的东西,请尝试使用此方法查找过去1分钟内发生的表格中的事件:
使用此条目,您可以调整小数值,直到获得所需的分钟值。就sysdate有效数字而言,值.0007恰好是1分钟。您可以使用它的倍数来获得您想要的任何其他值:
select (sysdate - (sysdate - .0007)) * 1440 from dual;
结果为1(分钟)
然后检查
是一件简单的事情select * from my_table where (sysdate - transdate) < .00071;
答案 11 :(得分:1)
(TO_DATE(:P_comapre_date_1, 'dd-mm-yyyy hh24:mi') - TO_DATE(:P_comapre_date_2, 'dd-mm-yyyy hh24:mi'))*60*60*24 sum_seconds,
(TO_DATE(:P_comapre_date_1, 'dd-mm-yyyy hh24:mi') - TO_DATE(:P_comapre_date_2, 'dd-mm-yyyy hh24:mi'))*60*24 sum_minutes,
(TO_DATE(:P_comapre_date_1, 'dd-mm-yyyy hh24:mi') - TO_DATE(:P_comapre_date_2, 'dd-mm-yyyy hh24:mi'))*24 sum_hours,
(TO_DATE(:P_comapre_date_1, 'dd-mm-yyyy hh24:mi') - TO_DATE(:P_comapre_date_2, 'dd-mm-yyyy hh24:mi')) sum_days
答案 12 :(得分:1)
如果您从“ your_table ”中选择两个日期,并希望将结果视为单列输出(例如'天 - hh:mm: ss ')你可以使用这样的东西。 首先,您可以计算这两个日期之间的间隔,然后从该区间导出所需的所有数据:
select extract (day from numtodsinterval (second_date
- add_months (created_date,
floor (months_between (second_date,created_date))),
'day'))
|| ' days - '
|| extract (hour from numtodsinterval (second_date
- add_months (created_date,
floor (months_between (second_date,created_date))),
'day'))
|| ':'
|| extract (minute from numtodsinterval (second_date
- add_months (created_date,
floor (months_between (second_date, created_date))),
'day'))
|| ':'
|| extract (second from numtodsinterval (second_date
- add_months (created_date,
floor (months_between (second_date, created_date))),
'day'))
from your_table
这应该给你这样的结果: 0天 - 1:14:55
答案 13 :(得分:1)
如果您希望使用表和列来获得日期差异。
SELECT TO_DATE( TO_CHAR(COLUMN_NAME_1, 'YYYY-MM-DD'), 'YYYY-MM-DD') -
TO_DATE(TO_CHAR(COLUMN_NAME_2, 'YYYY-MM-DD') , 'YYYY-MM-DD') AS DATEDIFF
FROM TABLE_NAME;
答案 14 :(得分:1)
select to_char(actual_start_date,'DD-MON-YYYY hh24:mi:ss') start_time,
to_char(actual_completion_date,'DD-MON-YYYY hh24:mi:ss') end_time,
floor((actual_completion_date-actual_start_date)*24*60)||'.'||round(mod((actual_completion_date-actual_start_date)*24*60*60,60)) diff_time
from fnd_concurrent_requests
order by request_id desc;
答案 15 :(得分:0)
这是另一种选择:
with tbl_demo AS
(SELECT TO_DATE('11/26/2013 13:18:50', 'MM/DD/YYYY HH24:MI:SS') dt1
, TO_DATE('11/28/2013 21:59:12', 'MM/DD/YYYY HH24:MI:SS') dt2
FROM dual)
SELECT dt1
, dt2
, round(dt2 - dt1,2) diff_days
, round(dt2 - dt1,2)*24 diff_hrs
, numtodsinterval((dt2 - dt1),'day') diff_dd_hh_mm_ss
from tbl_demo;
答案 16 :(得分:0)
这将计算日期之间的时间:
SELECT
(TO_CHAR( TRUNC (ROUND(((sysdate+1) - sysdate)*24,2))*60,'999999')
+
TO_CHAR(((((sysdate+1)-sysdate)*24)- TRUNC(ROUND(((sysdate+1) - sysdate)*24,2)))/100*60 *100, '09'))/60
FROM dual
答案 17 :(得分:0)
$sql="select bsp_bp,user_name,status,
to_char(ins_date,'dd/mm/yyyy hh12:mi:ss AM'),
to_char(pickup_date,'dd/mm/yyyy hh12:mi:ss AM'),
trunc((pickup_date-ins_date)*24*60*60,2),message,status_message
from valid_bsp_req where id >= '$id'";
答案 18 :(得分:0)
选择轮((tbl.Todate - tbl.fromDate)* 24 * 60 * 60) 从表 tbl
答案 19 :(得分:-1)
单个查询将返回两个时间戳列的时差:
选择INS_TS,MAIL_SENT_TS,从MAIL_NOTIFICATIONS中提取(从(INS_TS-MAIL_SENT_TS)的小时)timeDiff;