Oracle timestamp string value conversion - sql

I'm Trying to fetch values between two timestamps, however the conversion timestamp failing with formatting error.
SELECT
*
FROM
PKV
WHERE
extended_timestamp BETWEEN TO_TIMESTAMP('28-OCT-22 01.10.37.153016000 PM ASIA/CALCUTTA,DD-MON-YY HH24:MI:SS') AND TO_TIMESTAMP(
'28-OCT-22 10.10.37.153016000 PM ASIA/CALCUTTA,DD-MON-YY HH24:MI:SS')

You put the 2 arguments of TO_TIMESTAMP in only 1 string.
Note also that your date format is NLS dependent.
TO_TIMESTAMP_TZ('28-OCT-22 01.10.37.153016000 PM ASIA/CALCUTTA','DD-MON-YY HH12:MI:SS.FF9 PM TZR', 'NLS_DATE_LANGUAGE = American')

Use a TIMESTAMP literal:
SELECT *
FROM PKV
WHERE extended_timestamp
BETWEEN TIMESTAMP '2022-10-28 13:10:37.153016000 ASIA/CALCUTTA'
AND TIMESTAMP '2022-10-28 22:10:37.153016000 ASIA/CALCUTTA';

Related

Convert Varchar with timezone to Timestamp in Oracle

I have a problem converting varchar to DateTime/timestamp.
Here is the case
ID EVENT_TIME(Varchar)
1 2020-04-12T09:25:53+0800
2 2020-04-12T09:25:53+0700
3 2020-04-12T09:25:53+0900
return I want, all timestamp convert to +0700
ID EVENT_TIME(Datetime)
1 2020-04-12 10:25:53
2 2020-04-12 09:25:53
3 2020-04-12 11:25:53
is this possible? and how can I do it using oracle?
thanks
Please use below query to convert varchar to timestamp using to_timestamp_tz and again convert it to the required time format using to_char
select ID, to_char(to_timestamp_tz(EVENT_TIME, 'YYYY-MM-DD"T"HH24:MI:SS.FF TZH:TZM'), 'YYYY-MM-DD HH24:MI:SS') as event_date from table_name;
Example:
select to_char(to_timestamp_tz('2020-04-12T09:25:53+0800', 'YYYY-MM-DD"T"HH24:MI:SS.FF TZH:TZM'), 'YYYY-MM-DD HH24:MI:SS') as event_date from dual;
As #Jim said, you can use to_timestamp_tz() with a character literal to convert the string to a timestamp value:
to_timestamp_tz(event_time, 'SYYYY-MM-DD"T"HH24:MI:SSTZH:TZM')
The to normalise to the +07:00 offset you can use at time zone:
to_timestamp_tz(event_time, 'SYYYY-MM-DD"T"HH24:MI:SSTZH:TZM') at time zone '+07:00'
If you don't want to keep the time zone part you can cast to a plain timestamp:
cast(
to_timestamp_tz(event_time, 'SYYYY-MM-DD"T"HH24:MI:SSTZH:TZM') at time zone '+07:00'
as timestamp)
Or you can cast (... as date) as you don't have fractional seconds.
Or you can convert that back to a string for display:
to_char(
to_timestamp_tz(event_time, 'SYYYY-MM-DD"T"HH24:MI:SSTZH:TZM') at time zone '+07:00',
'SYYYY-MM-DD HH24:MI:SS')
db<>fiddle

01830. 00000 - "date format picture ends before converting entire input string"

select to_date('13/03/17 05:43:29,000000000 PM -05:00DD/MM/YY HH24:MI:SS') from
irregularities;
How to convert this date to 24-hour format?
You can convert a string to a timestamp with time zone using:
select to_timestamp_tz('13/03/17 05:43:29,000000000 PM -05:00',
'DD/MM/RR HH:MI:SS,FF9 AM TZH:TZM')
from dual;
If you only want a date data type then you can cast it:
select cast(
to_timestamp_tz('13/03/17 05:43:29,000000000 PM -05:00',
'DD/MM/RR HH:MI:SS,FF9 AM TZH:TZM')
as date)
from dual;
If you really only want the string version you can convert it back, which you would usually only do for display:
select to_date(
to_timestamp_tz('13/03/17 05:43:29,000000000 PM -05:00',
'DD/MM/RR HH:MI:SS,FF9 AM TZH:TZM'),
'DD/MM/YYYY HH24:MI:SS')
from dual;
If the original string is coming from a table then just replace the text literal with the column name, and dual with your table name. Of course, that assumes the column is actually a string. If it is actually already a timestamp and your client is just displaying it in a way you don't like, you only need theto_char() part.
Read more about these things in the documentation: to_timestamp_tz, format models, cast() and to_char().

oracle to_date returns some error in custom query

Oracle DB
START_DATE |END_DATE
--------------------|-------------------
2016-02-01 00:00:00 |2016-02-29 23:55:00
2016-02-01 00:00:00 |2016-02-29 23:55:00
2016-02-01 00:00:00 |2016-02-29 23:55:00
2016-02-01 00:00:00 |2016-02-29 23:55:00
2016-02-01 00:00:00 |2016-02-29 23:55:00
Query
`select * from VM_REPORT_TEMP_US where to_date(START_DATE,'YYYY-MM-DD HH24:MI:SS') >= '2016-02-01 00:00:00' AND to_date(END_DATE,'YYYY-MM-DD HH24:MI:SS') <= '2016-`02-24 24:59:00'`
Im trying to run this query but im getting some error. Can some one know where im going in this query ?
Im getting the below error
SQL Error [1861] [22008]: ORA-01861: literal does not match format string
java.sql.SQLDataException: ORA-01861: literal does not match format string
Do NOT use to_date() with a DATE column. That first converts the date value into a varchar value just to convert that back to a date which it was to begin with.
to_date() expects a varchar value, so Oracle first converts the DATE value into a varchar value using the current NLS settings. Then it tries to convert that varchar back into a date, using the format mask you supplied which most probably doesn't match the default NLS format you have and therefor you get an error.
You should also use proper date values in the condition rather then strings that are (again) implicitly converted to a DATE based on the current NLS settings:
select *
from VM_REPORT_TEMP_US
where START_DATE >= timestamp '2016-02-01 00:00:00'
AND END_DATE <= timestamp '2016-02-24 23:59:00'
Note that the hour 24 is invalid in an ISO timestamp literal.
If you want to provide the date/timestamp value in a format other then the ISO format, you need to use to_date() for those:
select *
from VM_REPORT_TEMP_US
where START_DATE >= to_date('01.02.2016 00:00:00', 'dd.mm.yyyy hh24:mi:ss')
AND END_DATE <= to_date('24.02.2016 23:59:59', 'dd.mm.yyyy hh24:mi:ss')
start_date and end_date are dates already, so you don't have to convert them to date. Convert the strings instead or even better use datetime literals.
select *
from VM_REPORT_TEMP_US
where START_DATE >= timestamp '2016-02-01 00:00:00'
and END_DATE <= timestamp '2016-02-24 00:59:00'
Bit different from both the above answers, but this is what I would use to avoid confusion between YYYY-MM-DD and YYYY-DD-MM.
select * from VM_REPORT_TEMP_US
where
START_DATE >= to_date('2016-02-01 00:00:00','YYYY-MM-DD HH24:MI:SS')
AND
END_DATE <= to_date('2016-02-24 00:59:00','YYYY-MM-DD HH24:MI:SS')

Oracle : Date time subtraction

I have to calculate time difference in minutes from current(sysdate) and modified time:-
to_date(to_char(sysdate, 'YYYY-MM-DD HH24:MI:SS'), 'YYYY-MM-DD HH24:MI:SS')
- to_date(to_char(modified, 'YYYY-MM-DD HH24:MI:SS'), 'YYYY-MM-DD HH24:MI:SS')
but problem is to_char returns proper time:-
to_char(whenmodified, 'YYYY-MM-DD HH24:MI:SS')
Outputs 2016-05-23 14:55:50
and to_date doesn’t show time:-
to_date(to_char(modified, 'YYYY-MM-DD HH24:MI:SS'), 'YYYY-MM-DD HH24:MI:SS')
Outputs: 2016-05-23
Please assist how I can get time difference by converting to_char to to_date.
NOTE:
I cant do sysdate-modified because both sysdate and modified gives date without time e.g 2016-05-23
Using to_char for sysdate or modified give date with time 2016-05-23 14:55:50
As we cant subtracts dates in to_char function I am again converting back them to to_date for getting time.
I am expecting:
2016-05-23 14:55:50 - 2016-05-23 14:53:50 = 2 min
I have to calculate time difference in minutes from current(sysdate) and modified time
Oracle Setup:
CREATE TABLE table_name ( modified DATE );
INSERT INTO table_name
SELECT TIMESTAMP '2016-05-23 14:20:00' FROM DUAL UNION ALL
SELECT TIMESTAMP '2016-05-23 00:00:00' FROM DUAL UNION ALL
SELECT TIMESTAMP '2016-05-01 00:00:00' FROM DUAL UNION ALL
SELECT TIMESTAMP '2016-01-01 00:00:00' FROM DUAL;
Query:
SELECT ( sysdate - modified ) * 24 * 60 AS minute_difference
FROM table_name;
Output:
MINUTE_DIFFERENCE
-----------------
3.66666667
863.666667
32543.6667
206783.667
And to address your comment that:
to_date doesn’t show time
A date always has a time component and never has a format internally to the database (it is represented by 7 or 8 bytes) - the formatting of a date is done by the client program that you use to access the database (and often the default is not to show the time component - however, the time component still exists).
You can change this either in the preferences of your client program or, if they don't use that to control it, by changing the NLS_DATE_FORMAT session parameter:
ALTER SESSION SET NLS_DATE_FORMAT = 'YYYY-MM-DD HH24:MI:SS';

date range in oracle apps report

The following sql when run with these parameters,
:P_COMP_DATE_FROM = '15-NOV-2015'
:P_COMP_DATE_TO = '15-NOV-2015'
compares as between '15-NOV-2015 00:00:00' and '15-NOV-2015 00:00:00'
Select Ordered_date
From xxcost_rep
Where DATE_COMPLETED BETWEEN NVL(fnd_date.canonical_to_date(:P_COMP_DATE_FROM), DATE_COMPLETED) AND NVL(fnd_date.canonical_to_date(:P_COMP_DATE_TO)), DATE_COMPLETED);
how can I compare this as start of the day and end of the day, so can display the correct result in the range.
I am trying the following to add 86399 seconds to make it the end of the day, but receiving error:
WHERE DATE_COMPLETED BETWEEN NVL(fnd_date.canonical_to_date(:P_COMP_DATE_FROM), DATE_COMPLETED) AND NVL(fnd_date.canonical_to_date(to_date(:P_COMP_DATE_TO,'DD-MON-YYYY')+interval '86399' second), DATE_COMPLETED)
{P_TO_CUSTOMER=, P_COMP_DATE_FROM=2015/11/15 00:00:00, P_COMP_DATE_TO=2015/11/15 00:00:00, P_TO_ORDER_NUMBER=, P_CUST_REGION=, P_TO_DATE=, P_JOB_STATUS=, P_FROM_DATE=, P_FROM_ORDER_NUMBER=, P_FROM_CUSTOMER=}
Calling XDO Data Engine...
--SQLException
java.sql.SQLDataException: ORA-01861: literal does not match format string
ORA-01861: literal does not match format string
The above error is because the date literal doesn't match with the format mask.
For example,
SQL> SELECT TO_DATE('2015118','yyyy/mm/dd') FROM dual;
SELECT TO_DATE('2015118','yyyy/mm/dd') FROM dual
*
ERROR at line 1:
ORA-01861: literal does not match format string
You might be storing dates as string, and there might be strings with different date formats. Therefore, your function fnd_date.canonical_to_date might be failing for such date literals while converting into DATE using TO_DATE.
Also, you should not depend on your client's NLS date format. Remember, TO_DATE is NLS dependent. You should explicitly mention the format mask.
For example,
SQL> SELECT to_date('11/18/2015 00:00:00', 'mm/dd/yyyy hh24:mi:ss') date_from,
2 to_date('11/18/2015 23:59:59', 'mm/dd/yyyy hh24:mi:ss') date_to
3 FROM dual;
DATE_FROM DATE_TO
------------------- -------------------
11/18/2015 00:00:00 11/18/2015 23:59:59
In your case, you need to compare the dates. You could do it like the below example,
SQL> WITH DATA AS(
2 SELECT DATE '2015-11-18' dt FROM dual
3 )
4 SELECT * FROM DATA
5 WHERE dt
6 BETWEEN to_date(
7 to_char(dt, 'mm/dd/yyyy')||' 00:00:00',
8 'mm/dd/yyyy hh24:mi:ss'
9 )
10 AND to_date(
11 to_char(dt, 'mm/dd/yyyy')||' 23:59:59',
12 'mm/dd/yyyy hh24:mi:ss'
13 );
DT
-------------------
11/18/2015 00:00:00
UPDATE
For the first part where you just need the start time, you don't have to add the time portion as 00:00:00since DATE has both date and time elements. When you do not mention the time portion, it defaults to midnight i.e. 00:00:00.
For example, add INTERVAL '86399' SECOND:
SQL> SELECT DATE '2015-11-18' from_date,
2 DATE '2015-11-18' + INTERVAL '86399' SECOND to_date
3 FROM dual;
FROM_DATE TO_DATE
------------------- -------------------
11/18/2015 00:00:00 11/18/2015 23:59:59