简体   繁体   English

在Oracle中转换为DateTime

[英]Convert to DateTime in Oracle

I have a strange format that I need to convert to an Oracle Timestamp (no fractal seconds needed). 我有一种奇怪的格式,需要转换为Oracle时间戳(不需要分形秒)。 I have two columns, date_entered, and time_entered, that have this format from our AS/400: 我有两列date_entered和time_entered,它们来自我们的AS / 400:

1110729 954
1110811 1216
1110815 1526
1110815 1659
1110817 1007
1110818 1000
1110821 1715
1110822 1320
1110823 1852
1110825 1743
1110826 1100
1110826 1559
1110826 1711
1110826 1906

The Date Column is an AS/400 date, with the 1st character as a y2k mark YYYMMDD. 日期列是一个AS / 400日期,第一个字符是y2k标记YYYMMDD。 All dates are actually above year 2000, so that 1st character can be disregarded if needed. 所有日期实际上都在2000年以上,因此如果需要,可以忽略第一个字符。

Also, as you can see my time_entered field is a 24-hour timstamp, however it is a "number" field, so 3-digit rows are possible. 另外,如您所见,我的time_entered字段是一个24小时计时,但是它是一个“数字”字段,因此3位数字行是可能的。 I would like to write a SQL statement to convert this to an Oracle Timestamp. 我想编写一条SQL语句将其转换为Oracle时间戳。 Not a function or anything, but just select statement to select the datetime of these two columns combined. 不是函数或任何东西,而只是select语句来选择这两列组合的日期时间。

Did you try: 你试过了吗:

select To_TimeStamp(substr(datefield,2)||lpad(timefield,4,'0'),'YYMMDDHH24MI') 
From table

I don't have oracle on this machine, but the above should work for you. 我在这台机器上没有oracle,但是上面的命令对您有用。

I have an Oracle instance and Sparky is correct with just a few minor modifications. 我有一个Oracle实例,Sparky进行了一些小的修改就正确了。 Need to change the '+' to '||' 需要将“ +”更改为“ ||” and also need to change the 'hh' to 'HH24': 并且还需要将“ hh”更改为“ HH24”:

TO_TIMESTAMP(SUBSTR(datefield,2)||LPAD(timefield,4,'0'),'YYMMDDHH24MI')

Sample shown below: 示例如下所示:

SQL> describe oldvalues2
 Name                                     Null?    Type
 ---------------------------------------- -------- ----------------------------
 DATEVAL                                           VARCHAR2(7)
 TIMEVAL                                           VARCHAR2(4)

SQL> select * from oldvalues2
  2  /

DATEVAL TIME
------- ----
1110729 954
1110826 1906

SQL> select to_timestamp(substr(DATEVAL,2)||lpad(TIMEVAL,4,'0'),'YYMMDDHH24MI')
  2  from oldvalues2
  3  /

TO_TIMESTAMP(SUBSTR(DATEVAL,2)||LPAD(TIMEVAL,4,'0'),'YYMMDDHH24MI')
---------------------------------------------------------------------------
29-JUL-11 09.54.00.000000000 AM
26-AUG-11 07.06.00.000000000 PM

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

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