TO_DATE ISSUE IN CONVERSION - oracle

I tried to execute the below query but it's throwing me error :
SELECT TO_DATE(
TIMESTAMP '1970-01-01 00:00:00' + numtodsinterval(1511421211, 'second')
,'DD-MM-YYYY HH24:MI:SS')
FROM dual
Error : ORA-01830: date format picture ends before converting entire input string

The TO_DATE( datestring, format_model ) function takes strings as arguments.
Your query:
SELECT TO_DATE(
TIMESTAMP '1970-01-01 00:00:00' + numtodsinterval(1511421211, 'second')
,'DD-MM-YYYY HH24:MI:SS'
)
FROM dual
Is passing a TIMESTAMP and a string so Oracle has to perform an implicit conversion from TIMESTAMP to a string so your function is effectively:
SELECT TO_DATE(
TO_CHAR(
TIMESTAMP '1970-01-01 00:00:00' + numtodsinterval(1511421211, 'second'),
(
SELECT value
FROM NLS_SESSION_PARAMETERS
WHERE parameter = 'NLS_TIMESTAMP_FORMAT'
)
),
'DD-MM-YYYY HH24:MI:SS'
)
FROM dual
If the NLS_TIMESTAMP_FORMAT session paramter does not match your format model 'DD-MM-YYYY HH24:MI:SS' then an exception will be raised.
You could change the NLS_TIMESTAMP_FORMAT parameter - but this is a session parameter that is set per user and each user can change it at any time during their session so this should NOT be the solution.
Instead, you can just use a DATE literal instead of a TIMESTAMP literal:
SELECT DATE '1970-01-01' + NUMTODSINTERVAL (1511421211, 'second')
FROM DUAL
Or, if you want to use a timestamp then you can use the CAST function:
SELECT CAST(
TIMESTAMP '1970-01-01 00:00:00' + NUMTODSINTERVAL (1511421211, 'second')
AS DATE
)
FROM DUAL

Is this what you are expecting?
select to_char(DATE '1970-01-01' + NUMTODSINTERVAL (1511421211, 'second'), 'yyyy/mm/dd hh24:mi:ss') from dual;

The syntax appear somewhat incorrect to me. Try this:
SELECT TO_DATE ('1970-01-01 00:00:00', 'YYYY-MM-DD HH24:MI:SS')
+ NUMTODSINTERVAL (1511421211, 'second')
FROM DUAL;
Edit:
As #a_horse.. said "Oracle DATE always contains a time",so if timestamp is not visible then you just need to see your NLS_DATE_FORMAT in table V$NLS_PARAMETERS. In your case its simply set to
NLS_DATE_FORMAT = 'DD-MM-YYYY';
So you need to alter the session first to get the timestamp in SQLPLUS. See below:
SQL> alter session set NLS_DATE_FORMAT = 'mm-dd-yyyy HH24:mi:ss';
Session altered.
SQL> SELECT TO_DATE ('1970-01-01 00:00:00', 'YYYY-MM-DD HH24:MI:SS') + NUMTODSINTERVAL (15114212
11, 'second') FROM DUAL;
TO_DATE('1970-01-01
-------------------
11-23-2017 07:13:31

Related

Convert the string having millseconds/microsecond to a 'yyyy-MM-dd HH24:mi:ss' in oracle

I have string = '07-Jun-2021 23:30:43.758' .
how can i convert it into '2020-06-07 23:30:43'
i tried casting or converting using to_date /to_timestamp. its not giving desired result.
You can convert it to a timestamp, then back to a string.
SELECT TO_CHAR (TO_TIMESTAMP ('07-Jun-2021 23:30:43.758', 'DD-Mon-YYYY HH24:MI:SS.FF'),
'YYYY-MM-DD HH24:MI:SS')
FROM DUAL;

how to truncate yyyy/mm/dd hh:mm:ss.SSS to mm/dd/yyyy in oracle

When I ran a query to get date it is retrieved in this format 'yyyy/mm/dd hh:mm:ss.SSS' but I need to convert it to mm/dd/yyyy.
I'm using this query for conversion
select
to_char(
add_months (
to_date(
to_char(
trunc(
TO_DATE('2016/01/01 00:00:00.0', 'YYYY/MM/DD HH24:MI:SS.SSS')
), 'MM/DD/YYYY' -- to char
),'MM/DD/YYYY' -- to date
), -2*1 -- add months
), 'MM/DD/YYYY' -- to char
) START_DATE,
to_char(
add_months (
to_date(
to_char(
trunc(
TO_DATE('2017/01/01 00:00:00.0', 'YYYY/MM/DD HH24:MI:SS.SSS')
), 'MM/DD/YYYY' -- to char
), 'MM/DD/YYYY' -- to date
), 3 -- add months
), 'MM/DD/YYYY' -- to char
) END_DATE
from dual;
Output is
ORA-01810: format code appears twice
01810. 00000 - "format code appears twice"
The problem is in the conversion of to_date itself. The below conversion itself is throwing the error you mentioned
select
TO_DATE('2017/01/01 00:00:00.0', 'YYYY/MM/DD HH24:MI:SS.SSS') END_DATE
from dual;
You need to use it like below if you want to convert the string with timestamp to timestamp
select TO_TIMESTAMP('2017/01/01 00:00:00.0', 'YYYY/MM/DD HH24:MI:SS.FF') from dual
This will simply satisfy your need rather than making so many conversions.
select to_char(
add_months(
TO_TIMESTAMP('2017/01/01 00:00:00.0', 'YYYY/MM/DDHH24:MI:SS.FF'),
-2),
'mm/dd/yyyy') from dual
ORA-01810: format code appears twice
That's because of SS.SSS. SSS is not a valid date format. You are trying to handle fractional seconds but:
the correct format mask for that is FF
DATE doesn't support fractional seconds, only TIMESTAMP
Really date format is a display issue and should be handled by the client's NLS settings. But if you really must do it in SQL this is all you need:
select
to_char(DATE '2015-11-01', 'MM/DD/YYYY') START_DATE
, to_char(DATE '2017-04-01', 'MM/DD/YYYY') END_DATE
from dual;
You don't need trunc() because the date literals are already set to midnight. You don't need add_months() because you can just change the value of the date literal. You don't need to cast the date to a string back to a date because you just don't.

oracle to_date with format doesn't show time

I have simple calculation, I subtract interval from date with time:
select TO_DATE('2016-12-05 23:04:59', 'YYYY-MM-DD HH24:MI:SS') - to_dsinterval('00 0:05:00') from dual;
It works fine, the result: 2016-12-05 22:59:59
but it doesn't work correctly with timezones, so the next approach solves the problem with timezone. I just wrap expression with to_date() one more time
select TO_DATE(
TO_DATE('2016-12-05 23:04:59', 'YYYY-MM-DD HH24:MI:SS') - to_dsinterval('00 0:05:00')) from dual;
but now it turns time to zeros. Result should be: 2016-12-05 22:59:59 but actual: 2016-12-05 00:00:00
If I add format to the outer to_date as this:
select to_date( TO_DATE('2016-12-05 23:04:59', 'YYYY-MM-DD HH24:MI:SS') - to_dsinterval('00 0:05:00'), 'YYYY-MM-DD HH24:MI:SS') from dual;
The result become very strange: 0005-12-16 00:00:00
What I'm doing wrong?
DATE data type does not support any time zone functions, you must use TIMESTAMP WITH TIME ZONE for that.
Your query
SELECT TO_DATE( TO_DATE('2016-12-05 23:04:59', 'YYYY-MM-DD HH24:MI:SS') - TO_DSINTERVAL('00 0:05:00'), 'YYYY-MM-DD HH24:MI:SS')
FROM dual;
does following:
Create a DATE '2016-12-05 23:04:59'
Subtract interval '00 0:05:00'
Cast to a VARCHAR2 (using NLS_DATE_FORMAT format)
Cast to a DATE using YYYY-MM-DD HH24:MI:SS format
In case your NLS_DATE_FORMAT would be equal to YYYY-MM-DD HH24:MI:SS this query returns correct output.
Use this one:
SELECT TO_TIMESTAMP('2016-12-05 23:04:59', 'YYYY-MM-DD HH24:MI:SS') - TO_DSINTERVAL('00 0:05:00')
FROM dual;
TO_DATE(... works as well. If you need time zone support you must do:
SELECT TO_TIMESTAMP_TZ('2016-12-05 23:04:59 Europe/Berlin', 'YYYY-MM-DD HH24:MI:SS TZR') - TO_DSINTERVAL('00 0:05:00')
FROM dual;
TO_DATE( char, fmt, nls ) takes VARCHAR2 arguments.
Performing TO_DATE('2016-12-05 23:04:59', 'YYYY-MM-DD HH24:MI:SS') - to_dsinterval('00 0:05:00') returns a DATE datatype which when you pass it to TO_DATE() oracle will cast it to a VARCHAR2 datatype so it matches the expected datatype of the argument (implicitly calling TO_CHAR( value, NLS_DATE_FORMAT ) to perform this cast) and then convert this back to a DATE datatype.
You just need to do:
SELECT TO_DATE('2016-12-05 23:04:59', 'YYYY-MM-DD HH24:MI:SS')
- to_dsinterval('00 0:05:00')
FROM DUAL;
If you want to handle time zones then use a TIMESTAMP AT TIME ZONE and just convert it to whatever timezone you want to store the date at:
SELECT TIMESTAMP '2016-12-05 23:04:59 Europe/Paris' AT TIME ZONE 'UTC'
FROM DUAL;
(Will create your timestamp in Paris' time zone and convert it to the correct time in the UTC time zone).

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

Oracle - How to check my date field value has time portion or not

I have a table containing a date type field and it holds few records with date value with time portion and few records only have date value without time portion. How to check which record has time portion and what not without navigating record by record?
This is what I'm using at the moment, but I want something faster:
SELECT MY_TAB.SEQ, MY_TAB.CRTE_DT,
CASE WHEN TRUNC (MY_TAB.CRTE_DT) = MY_TAB.CRTE_DT
THEN 'False' ELSE 'True' END AS "Has Time Portion"
FROM (SELECT 1 AS SEQ,
TO_DATE ('7/28/2013', 'MM/DD/YYYY') AS CRTE_DT
FROM DUAL
UNION
SELECT 2 AS SEQ,
TO_DATE ('11/07/2013 12:27:54', 'MM/DD/YYYY HH24:MI:SS') AS CRTE_DT
FROM DUAL) MY_TAB;
Nice Solution. Just one catch. If the timestamp is of 12:00 am midnight, then the result will be false.
TO_DATE ('11/07/2013 00:00:00', 'MM/DD/YYYY HH24:MI:SS') will yield to false
CREATE TABLE MY_TAB
AS
SELECT SEQ, CRTE_DT
FROM (SELECT 1 AS SEQ, TO_DATE ('7/28/2013', 'MM/DD/YYYY') AS CRTE_DT FROM DUAL
UNION
SELECT 2 AS SEQ,
TO_DATE ('11/07/2013 12:27:54', 'MM/DD/YYYY HH24:MI:SS') AS CRTE_DT FROM DUAL
UNION
SELECT 3 AS SEQ,
TO_DATE ('09/5/2013 00:00:00', 'MM/DD/YYYY HH24:MI:SS') AS CRTE_DT FROM DUAL
UNION
SELECT 4 AS SEQ, TO_DATE ('4/15/2013 00:00:01', 'MM/DD/YYYY HH24:MI:SS') AS CRTE_DT FROM DUAL
UNION
SELECT 5 AS SEQ, TO_DATE ('12/12/2012 1:01:01', 'MM/DD/YYYY HH24:MI:SS') AS CRTE_DT FROM DUAL);
SELECT MY_TAB.SEQ, MY_TAB.CRTE_DT,
CASE WHEN TRUNC (MY_TAB.CRTE_DT) = MY_TAB.CRTE_DT
THEN 'False' ELSE 'True' END AS "Has Time Portion"
FROM MY_TAB;
If you just want to check the data field contains time portion or not, you can do it by altering the session as,
SQL> ALTER SESSION SET NLS_DATE_FORMAT = 'dd/mm/yyyy';
Session altered.
SQL> SELECT SYSDATE FROM dual;
SYSDATE
----------
08/11/2013
SQL> ALTER SESSION SET NLS_DATE_FORMAT = 'dd/mm/yyyy hh24:mi:ss';
Session altered.
SQL> SELECT SYSDATE FROM dual;
SYSDATE
-------------------
08/11/2013 08:30:49

Resources