oracle 英文时间转换 Tue Sep 08 14:52:07 CST 2015

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了oracle 英文时间转换 Tue Sep 08 14:52:07 CST 2015相关的知识,希望对你有一定的参考价值。

怎么转换成 2015/9/8 4:21:47 年月日时分秒格式

参考技术A SELECT TO_CHAR(SYSDATE,'YYYY/MM/DD HH24:MI:SS') FROM DUAL;

Oracle 11g 将结果表旋转 90 度

【中文标题】Oracle 11g 将结果表旋转 90 度【英文标题】:Oracle 11g Rotate Result Table 90 Degrees 【发布时间】:2018-07-03 11:47:40 【问题描述】:

我需要将结果集旋转 90 度。我一直在玩 pivot 和 unpivot,但到目前为止还没有成功。结果集如下所示:

2018-07-03  TUE 20:00   12:15   465 (null)
2018-07-02  MON 20:00   20:05   -5  FAIL
2018-06-29  FRI 19:30   19:04   26  (null)
2018-06-28  THU 20:00   19:07   53  (null)
2018-06-27  WED 20:00   19:37   23  (null)

我需要能够将整个结果集旋转 90 度,以便第 1 列(日期)成为列标题,并且数据的重置位于这些列下方,如下所示:

2018-06-27  2018-06-28  2018-06-29  2018-07-02  2018-07-03
WED         THU         FRI         MON         TUE
20:00       20:00       19:30       20:00       20:00
19:37       19:07       19:04       20:05       12:15
23          53          56          -5          465
(null)      (null)      (null)      FAIL        (null)

是否有其他功能可以实现这一点,或者可能是我还没想出来的枢轴/取消枢轴方法?

【问题讨论】:

现在每一列都包含不同类型的数据,结果集中的每一行都需要一列。以这种方式消费数据对哪个系统有利? 【参考方案1】:

你可以生成这样的逻辑,比如使用这个 SQL

select vl as date1, myCol as myCol1
    from 
       (
        select to_char(myDate,'yyyy-mm-dd') as mD, 
               to_char(myDate,'DY') as Dy, 
               to_char(myVal) as mVl, t.* from tab t
         where to_char(mydate,'yyyy-mm-dd') = <a_date_parameter>
       )
        unpivot (vl for myCol in (mD,Dy,myHourStart, myHourEnd,mVl,status))

unpivot概念的贡献为:

create table tab( myDate date, myHourStart varchar2(5), myHourEnd varchar2(5), myVal int, status varchar2(15) );
insert all 
   into tab values( date'2018-07-03','20:00','12:15', 465, null )
   into tab values( date'2018-07-02','20:00','20:05', -5, 'FAIL' )
   into tab values( date'2018-06-29','19:30','19:04', 26,  null )
   into tab values( date'2018-06-28','20:00','19:07', 53, null )
   into tab values( date'2018-06-27','20:00','19:37', 23,  null ) 
select * from dual;

select date1, date2, date3, date4, date5 from
(  select vl as date1, myCol as myCol1
    from 
       (
        select to_char(myDate,'yyyy-mm-dd') as mD, 
               to_char(myDate,'DY') as Dy, 
               to_char(myVal) as mVl, t.* from tab t
         where to_char(mydate,'yyyy-mm-dd') = '2018-06-27'
       )
        unpivot (vl for myCol in (mD,Dy,myHourStart, myHourEnd,mVl,status)) 
         ) q1 join
  (  select vl as date2, myCol as myCol2
      from 
         (
          select to_char(myDate,'yyyy-mm-dd') as mD, 
                 to_char(myDate,'DY') as Dy,
                 to_char(myVal) as mVl, t.* from tab t
           where to_char(mydate,'yyyy-mm-dd') = '2018-06-28'
         )
          unpivot (vl for myCol in (mD,Dy,myHourStart, myHourEnd,mVl,status)) 
           ) q2 on ( q1.myCol1 = q2.myCol2  )  
join
  (  select vl as date3, myCol as myCol3
      from 
         (
          select to_char(myDate,'yyyy-mm-dd') as mD, 
                 to_char(myDate,'DY') as Dy,
                 to_char(myVal) as mVl, t.* from tab t
           where to_char(mydate,'yyyy-mm-dd') = '2018-06-29'
         )
          unpivot (vl for myCol in (mD,Dy,myHourStart, myHourEnd,mVl,status)) 
           ) q3 on ( q1.myCol1 = q3.myCol3  ) 
join
  (  select vl as date4, myCol as myCol4
      from 
         (
          select to_char(myDate,'yyyy-mm-dd') as mD, 
                 to_char(myDate,'DY') as Dy,
                 to_char(myVal) as mVl, t.* from tab t
           where to_char(mydate,'yyyy-mm-dd') = '2018-07-02'
         )
          unpivot (vl for myCol in (mD,Dy,myHourStart, myHourEnd,mVl,status)) 
           ) q4 on ( q1.myCol1 = q4.myCol4  )            
join
  (  select vl as date5, myCol as myCol5
      from 
         (
          select to_char(myDate,'yyyy-mm-dd') as mD, 
                 to_char(myDate,'DY') as Dy,
                 to_char(myVal) as mVl, t.* from tab t
           where to_char(mydate,'yyyy-mm-dd') = '2018-07-03'
         )
          unpivot (vl for myCol in (mD,Dy,myHourStart, myHourEnd,mVl,status)) 
           ) q5 on ( q1.myCol1 = q5.myCol5  );

SQL Fiddle Demo

【讨论】:

以上是关于oracle 英文时间转换 Tue Sep 08 14:52:07 CST 2015的主要内容,如果未能解决你的问题,请参考以下文章

Oracle SQL 将行转换为列

我们如何仅从 Oracle 中具有各种日期和字符串格式的列中提取日期?

从 'Tue May 20 00:00:00 PKT 2014' 到 20-05-2014 的日期格式转换 [关闭]

Java转换Json日期/Date(1487053489965+0800)/格式以及js时间格式 Tue Feb 14 2017 14:06:32 GMT+0800

CORS

java中请问如何把 Tue Jan 02 00:05:52 CST 1900 转成oracle中number(例如1317362502)类型的 。