oracle to_date with format doesn't show time - oracle

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).

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;

TO_DATE ISSUE IN CONVERSION

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

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;

Alter a datetime in Oracle and set time to 9 am

i have 28-APR-2016 10:05:07 date as parameter in stored procedure. This may be the current time also as string date.
i need to set the time to 9 am to check the shift start timing.
SELECT TO_DATE('28-APR-2016 10:05:07', 'DD-MON-YYYY HH24:MI:SS') FROM dual;
I am new to oracle. Help is appreciated.
If you want the date with 9:00 a.m., then you can do:
SELECT TRUNC(TO_DATE('28-APR-2016 10:05:07', 'DD-MON-YYYY HH24:MI:SS')) + 9/24.0
FROM dual;
You can also use:
SELECT TRUNC(TO_DATE('28-APR-2016 10:05:07', 'DD-MON-YYYY HH24:MI:SS')) + INTERVAL '9' HOUR
FROM dual;
I'm just old-fashioned so I tend to use the first method.

Oracle Time Zones Conversion (using from_tz)

I'm trying to convert a time (date + time) from one time zone to another. In the query below, I'm trying to convert a time from EST ("America/New_York") to PST ("America/Los_Angeles"). The query is partially working; the results:
DATABASE_DATE = 2012-02-13 1:00:00 PM
LOCALTIME (what I get): 2012-02-12 10:00:00 AM.
So the time is good but the date is wrong. It should be 2012-02-13 instead of 2012-02-12.
Am I doing something wrong? Here's my query:
select to_date( to_char( ( from_tz( to_timestamp( DATABASE_DATE
, 'YYYY-MM-DD HH:MI:SS')
,'America/New_York')
at time zone 'America/Los_Angeles')
,'YYYY-MM-DD HH:MI:SS')
,'YYYY-MM-DD HH:MI:SS') as localtime
from table
Thanks
to_timestamp() gets a string (VARCHAR2, CHAR ...) if you try to give it a date, then oracle will convert it to a string according to NLS_DATE_FORMAT which might vary in different environments and return unexpected results (as in this case).
What you should do is use to_char first, so your query can look like this:
select to_date(to_char((from_tz(to_timestamp(to_char(DATABASE_DATE, 'YYYY-MM-DD HH:MI:SS PM'), 'YYYY-MM-DD HH:MI:SS PM') ,'America/New_York')
at time zone 'America/Los_Angeles'),'YYYY-MM-DD HH:MI:SS PM'),'YYYY-MM-DD HH:MI:SS PM') as localtime
from table
UPDATE: if I understand you right then you want something like this:
select to_char((from_tz(to_timestamp(to_char(DATABASE_DATE, 'YYYY-MM-DD HH:MI:SS PM'), 'YYYY-MM-DD HH:MI:SS PM') ,'America/New_York')
at time zone 'America/Los_Angeles'),'YYYY-MM-DD HH:MI:SS PM TZD') as localtime
from table
SELECT TO_CHAR(NEW_TIME(systimestamp,'EST','PST'), 'DD-MON-YY HH24:MI:SS') AS converted_timestamp_column FROM DUAL;
Please try this as well .There is a function for this purpose in oracle
https://docs.oracle.com/cd/B28359_01/olap.111/b28126/dml_functions_2036.htm
select NEW_TIME (TO_DATE ('2011/11/11 01:45', 'yyyy/mm/dd HH24:MI'), 'AST', 'MST') from dual;

Resources