to_timestamp encounter the fractional seconds must be between 0 and 999999999 - oracle

enter code herethis is the code
select to_timestamp('03-APR-13 01.15.31.6754542 PM','DD-MON-RR HH.MI.SS.FF4 AM') from dual;
i originally intended to accurate millisecond to 4 digits,but the error display.and i find if the digits of .ff is equal or greater than 7 which is the digits of 6754542,
for example:
select to_timestamp('03-APR-13 01.15.31.6754542 PM','DD-MON-RR HH.MI.SS.FF8 AM') from dual;
select to_timestamp('03-APR-13 01.15.31.6754542 PM','DD-MON-RR HH.MI.SS.FF9 AM') from dual;
and so on. they always disply 03-APR-13 01.15.31.675454200 PM,9 digits...
how can i accurate millisecond to 4 digits,or forever not?
please help me,thanks.

SELECT
TO_CHAR(
TO_TIMESTAMP('03-APR-13 01.15.31.6754542 PM',
'DD-MON-RR HH.MI.SS.FF AM'),
'DD-MON-RR HH.MI.SS.FF4 AM')
FROM DUAL;

Related

Trying to covert timestamp to date causes error

select to_date('25-MAY-20 12.10.12.320000 PM', 'dd-mon-yy hh:mi:ss pm') from dual;
getting the error
"am or pm required"
Oracle only stores precision up to seconds in a date type. If you want fractional seconds (e.g. milliseconds) you will need to use a timestamp column. But in any case, your current format mask is wrong. Try this instead:
SELECT TO_TIMESTAMP('25-Feb-20 12.10.12.320000 PM', 'dd-mon-yy hh.mi.ss.ff6 pm')
FROM dual
Demo
You can convert your timestamp into char and then convert to date.
Example with systimestamp:
select to_date(
to_char(systimestamp,'dd-mon-yy hh:mi:ss PM')
, 'dd-mon-yy hh:mi:ss PM')
from dual;

How to get a date at midnight

I would like to know how to get a date with 00 hour, 00 minutes and 00 seconds.
This
select to_char(sysdate, 'dd/MM/YYYY HH:mm:ss') from dual;
gives the date at the time I asked it
If I don't give any hour :
select to_char(to_date('03/05/2017', 'DD/MM/YYYY'), 'DD/MM/YYYY HH:mm:ss') from dual;
I have a date at noon.
How can I get a date (with sysdate or giving my own date with to_date) at 00:00:00
Thank you
Use TRUNC( date_value, format_model ) and either omit the format model (the default is to truncate to midnight) or use one of the format models 'ddd', 'dd' or 'j':
SELECT TRUNC( SYSDATE ) FROM DUAL;
I dont give any hour :
select to_char(to_date('03/05/2017', 'DD/MM/YYYY'), 'DD/MM/YYYY HH:MI:SS') from dual;
I have a date at noon.
No, you have the date at midnight formatted with a 12-hour clock.
select to_char( to_date('03/05/2017', 'DD/MM/YYYY'), 'DD/MM/YYYY HH:MI:SS PM')
Outputs 03/05/2017 12:00:00 AM
To get a 24-hour clock you need to use HH24 in the format model (rather than HH or HH12 which is a 12-hour clock):
select to_char( to_date('03/05/2017', 'DD/MM/YYYY'), 'DD/MM/YYYY HH24:MI:SS')
Outputs 03/05/2017 00:00:00
mm should be replaced with mi for minutes:
select to_char(to_date('03/05/2017', 'DD/MM/YYYY'), 'DD/MM/YYYY HH:mm:ss') from dual;

How to convert timestamp to yyyy-mm-dd hh24:mi:ss format in oracle

How to convert 26-Mar-15 03.42.43.601000000 pm to yyyy-mm-dd hh24:mi:ss. Can anyone help me on this ?
First convert your string/varchar2 into a timestamp, and then format it back to a string with your format:
SQL> select to_char
2 ( to_timestamp('26-Mar-15 03.42.43.601000000 pm','dd-Mon-rr hh.mi.ss.ff9 am')
3 , 'yyyy-mm-dd hh24:mi:ss'
4 )
5 from dual
6 /
TO_CHAR(TO_TIMESTAM
-------------------
2015-03-26 15:42:43
1 row selected.
Yo can convert the date string to timestamp by;
select to_timestamp('26-Mar-15 03.42.43.601000000 pm', 'dd-mon-yy hh.mi.ss.FF9 AM')
from DUAL
If you want to get the data in yyyy-mm-dd hh24:mi:ss then use to_char function
select
to_char(
to_timestamp('26-Mar-15 03.42.43.601000000 pm', 'dd-mon-yy hh.mi.ss.FF9 AM'),
'yyyy-mm-dd hh24:mi:ss'
)
from DUAL

Convert elapsed time a decimal in Oracle

When performing the below calculation (end_time minus start_time) in Oracle you get a result of +00 01:30:00.000000. How can that result be converted to 1.50?
to_timestamp(to_char(end_time,'HH:MI AM'), 'HH:MI AM') -
to_timestamp(to_char(start_time,'HH:MI AM'), 'HH:MI AM')
Try something like this:
select dat,extract(hour from dat) ||'.'||trunc(100*extract(minute from dat)/60,0) result from
(select to_timestamp('04:30 AM', 'HH:MI AM') - to_timestamp('03:10 AM', 'HH:MI AM') as dat from dual union all
select to_timestamp('02:30 AM', 'HH:MI AM') - to_timestamp('02:00 AM', 'HH:MI AM') as dat from dual)
Results:
DAT RESULT
--------------------------------------------------------------------------------
+00 01:20:00.000000 1.33
+00 00:30:00.000000 0.50
The difference between two TIMESTAMPs is an INTERVAL. You can easily extract the various components and obtain the desired result with some arithmetics. In the general case (for a result in fractional days!):
with testdata as (
select NUMTODSINTERVAL(1.55,'DAY') i from dual
)
select EXTRACT(DAY FROM i)
+ EXTRACT(HOUR FROM i)/24
+ EXTRACT(MINUTE FROM i)/(24*60)
+ EXTRACT(SECOND FROM i)/(24*60*60)
FROM testdata
In your particular case, assuming (1) "timestamp is the data type" (2) you're "ignoring the day, second and millisecond components" (3) you want fractional hours :
select ...
EXTRACT(HOUR FROM (end_time-start_time))
+ EXTRACT(MINUTE FROM (end_time-start_time))/(60)
FROM ...
Alternative method: since your times are only accurate to the minute you may as well just use DATEs, subtraction of which yields a decimal number, e.g.:
to_date(to_char(end_time,'HH24:MI'), 'HH24:MI') -
to_date(to_char(start_time,'HH24:MI'), 'HH24:MI')
If the date portions are guaranteed to be on the same date, you can use an even simpler expression for the same result:
end_time - start_time

How to strip Timestamp up to just seconds?

Given a timestamp value, i.e. "04-SEP-14 03.09.09.272949000 PM", how do I get the date time up to just the seconds part and zeroing out the micro-seconds part?
given: 04-SEP-14 03.09.09.272949000 PM
wish: 04-SEP-14 03.09.09.00 PM
thanks in advance.
You can use the cast function to cast it to a zero-millisecond TIMESTAMP with the construct:
cast(<your column>, timestamp(0))
Using a CTE to pass in your value:
with t as (
select to_timestamp('04-SEP-14 03.09.09.272949000 PM',
'DD-MON-RR HH:MI:SS.FF AM') as tstamp
from dual
)
select to_char(cast(tstamp as timestamp(0)), 'DD-MON-RR HH:MI:SS.FF2 AM')
from t;
TO_CHAR(CAST(TSTAMPASTIMESTAMP(0)),'DD-M
----------------------------------------
04-SEP-14 03:09:09.00 PM
You could also cast it twice, once to DATE to strip the milliseconds, and then back to TIMESTAMP again:
with t as (
select to_timestamp('04-SEP-14 03.09.09.272949000 PM',
'DD-MON-RR HH:MI:SS.FF AM') as tstamp
from dual
)
select to_char(cast(cast(tstamp as date) as timestamp),
'DD-MON-RR HH:MI:SS.FF2 AM')
from t;
TO_CHAR(CAST(CAST(TSTAMPASDATE)ASTIMESTA
----------------------------------------
04-SEP-14 03:09:09.00 PM
... which is doing more work as well as being more typing.
You can't use trunc(date) because that implicitly converts the TIMESTAMP to a DATE, and since a DATE is already at that precision you can't pass SS as the fmt anyway.

Resources