Convert to DateTime in Oracle
I have a strange format that I need to convert to an Oracle Timestamp (no fractal seconds needed). I have two columns, date_entered, and time_entered, that have this format from our 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. All dates are actually above year 2000, so that 1st character can be disregarded if needed.
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. I would like to write a SQL statement to convert this to an Oracle Timestamp. Not a function or anything, but just select statement to select the datetime of these two columns combined.
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.
I have an Oracle instance and Sparky is correct with just a few minor modifications. Need to change the '+' to '||' and also need to change the 'hh' to '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
精彩评论