简体   繁体   English

如何在SQL中转换日期格式

[英]how to convert date format in sql

if i have dates/time like these 如果我有这样的日期/时间

8/5/2014 12:00:01 AM
8/5/2014 12:00:16 AM
8/5/2014 12:00:18 AM
8/5/2014 12:17:18 AM
8/5/2014 12:19:18 AM

i want these date/times 我想要这些日期/时间

  • if the minutes less than 15 and greater than 00 i want the time for minutes to be 00 如果分钟数少于15且大于00,我希望分钟数为00
  • if the minutes less than 30 and greater than 15 i want the minutes to be 15 如果分钟数少于30且大于15,我希望分钟数为15
  • if the minutes less than 45 and greater than 30 i want the minutes to be 30 如果分钟数少于45且大于30,我希望分钟数为30
  • if the minutes less than 00 and greater than 45 i want the minutes to be 45 如果分钟数小于00且大于45,我希望分钟数为45

     8/5/2014 12:00:00 AM ... ... 8/5/2014 12:15:00AM ... ... 8/5/2014 12:30:00AM ... ... 8/5/2014 12:45:00AM 

i need to do that for my report how can i apply these in oracle 我需要针对我的报告执行此操作,如何在Oracle中应用这些

Here is one method. 这是一种方法。 Extract the date and then add in what you want as hours and minutes: 提取日期,然后以小时和分钟的形式添加所需的内容:

select trunc(dt) + extract(hour from dt) / 24.0 +
       (trunc(extract(minute from dt) / 15) * 15) / (24.0 * 60);

This uses the fact that + for dates adds a number of days. 这利用了一个事实,即+表示日期会增加几天。 The three terms are the original date at midnight, the number of hours converted to days (hence the / 24 ) and the third is the number of minutes, suitably rounded. 这三个术语是原始日期为午夜,将小时数转换为天数(因此为/ 24 ),而第三个术语是分钟数(适当舍入)。

Here's another method, which is really a variation of Gordon Linoff's approach: 这是另一种方法,实际上是Gordon Linoff方法的一种变体:

trunc(dt, 'HH24') + ((15/1440) * (floor(to_number(to_char(dt, 'MI'))/15)))

The trunc(dt, 'HH24') gives you the value truncated to hour precision, so for your sample that's always midnight. trunc(dt, 'HH24')为您提供截断为小时精度的值,因此对于您的样本始终为午夜。 Then floor(to_number(to_char(dt, 'MI'))/15) gives you the number of complete 15-minute periods represented by the minute value; 然后floor(to_number(to_char(dt, 'MI'))/15)给出由分钟值表示的完整15分钟周期的数量; with your data that's either zero or 1. As Gordon mentioned when you add a numeric value to a date it's treated as fractions of a day, so that needs to be multiplied by '15 minutes' (15/1400). 数据为零或1。正如Gordon所提到的,当您向日期添加数字值时,它被视为一天的一部分,因此需要乘以“ 15分钟”(15/1400)。

with t as (
  select to_date('8/5/2014 12:00:01 AM') as dt from dual
  union all select to_date('8/5/2014 12:00:16 AM') as dt from dual
  union all select to_date('8/5/2014 12:00:18 AM') as dt from dual
  union all select to_date('8/5/2014 12:17:18 AM') as dt from dual
  union all select to_date('8/5/2014 12:19:18 AM') as dt from dual
  union all select to_date('8/5/2014 12:37:37 AM') as dt from dual
  union all select to_date('8/5/2014 12:51:51 AM') as dt from dual
)
select dt, trunc(dt, 'HH24')
    + ((15/1440) * (floor(to_number(to_char(dt, 'MI'))/15))) as new_dt
from t;

DT                     NEW_DT               
---------------------- ----------------------
08/05/2014 12:00:01 AM 08/05/2014 12:00:00 AM 
08/05/2014 12:00:16 AM 08/05/2014 12:00:00 AM 
08/05/2014 12:00:18 AM 08/05/2014 12:00:00 AM 
08/05/2014 12:17:18 AM 08/05/2014 12:15:00 AM 
08/05/2014 12:19:18 AM 08/05/2014 12:15:00 AM 
08/05/2014 12:37:37 AM 08/05/2014 12:30:00 AM 
08/05/2014 12:51:51 AM 08/05/2014 12:45:00 AM 

To add yet another method this looks like a situation where the function NUMTODSINTERVAL() could be useful - it makes it slightly more obvious what's happening: 要添加另一种方法,这看起来像函数NUMTODSINTERVAL()可能有用的情况-它使正在发生的事情更加明显:

select trunc(dt) 
       + numtodsinterval(trunc(to_char(dt, 'sssss') / 900) * 15, 'MINUTE')
  from ...

TRUNC() truncates the date to the beginning of that day. TRUNC()将日期截断到该天的开始。 The format model sssss calculates the number of seconds since midnight. 格式模型 sssss计算自午夜以来的秒数。 The number of complete quarter-hours since midnight is the number of seconds divided by 900 (as there are 900 quarter-hours in the day). 自午夜以来完整的四分之一小时数是秒数除以900(因为一天中有900个四分之一小时数)。 This is truncated again to remove any part-completed quarter-hours, multipled by 15 to give the number of minutes (there are 15 minutes in a quarter hour). 再次将其截断以删除部分完成的季度小时数,再乘以15即可得到分钟数(每25分钟中有15分钟)。 Lastly, convert this to an INTERVAL DAY TO SECOND and add to the original date. 最后,将其转换为INTERVAL DAY TO SECOND并添加到原始日期。

SQL> alter session set nls_date_format = 'dd/mm/yyyy hh24:mi:ss';

Session altered.

SQL> with t as (
  2   select to_date('05/08/2014 12:00:01') as dt from dual union all
  3   select to_date('05/08/2014 12:00:16') as dt from dual union all
  4   select to_date('05/08/2014 12:00:18') as dt from dual union all
  5   select to_date('05/08/2014 12:17:18') as dt from dual union all
  6   select to_date('05/08/2014 12:19:18') as dt from dual union all
  7   select to_date('05/08/2014 12:37:37') as dt from dual union all
  8   select to_date('05/08/2014 12:51:51') as dt from dual
  9  )
 10  select trunc(dt)
 11         + numtodsinterval(trunc(to_char(dt, 'sssss') / 900) * 15, 'MINUTE')
 12    from t
 13         ;

TRUNC(DT)+NUMTODSIN
-------------------
05/08/2014 12:00:00
05/08/2014 12:00:00
05/08/2014 12:00:00
05/08/2014 12:15:00
05/08/2014 12:15:00
05/08/2014 12:30:00
05/08/2014 12:45:00

7 rows selected.

I've explicitly set my NLS_DATE_FORMAT so I can rely on implicit conversion in TO_DATE() so that it fits on the page without scrolling. 我已经明确设置了我的NLS_DATE_FORMAT,因此我可以依靠TO_DATE()隐式转换,使其适合页面而无需滚动。 It is not recommended to use implicit conversion normally. 建议通常使用隐式转换。

Here is an example for ORACLE with actual date: 这是带有实际日期的ORACLE的示例:

select trunc(sysdate,'HH')+trunc(to_number(to_char(sysdate,'MI'))/15)*15/24/60
from dual;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM