If I have dates that are in this format: Sep-29-07 13:45:00 PDT
How would I convert this into a Date object in oracle 11g? I have tried this:
SELECT TO_DATE(PublishDate, 'MON-DD-YY HH24:MI:SS')
FROM Order;
But I am getting an error saying:
ORA-01830: date format picture ends before converting entire input string
If you have only one time zone's dates, you can just ignore the timezone part using:
select to_date(PublishDate, 'MON-DD-YY HH24:MI:SS "PDT"')
from Orders;
If you plan to support multiple timezones, then store them with timestamp with time zone format. use correct timezone abbreviation (PST instead of PDT - See this) and convert the string, use to_timestamp_tz.
select to_timestamp_tz(PublishDate, 'MON-DD-YY HH24:MI:SS TZR')
from Orders;
or perhaps, convert them in one local time zone. See this answer for that
You can ignore the timezone by using:
SELECT TO_DATE(SUBSTR(PublishDate, 1, length(PublishDate) - 4), 'MON-DD-YY HH24:MI:SS')
However, if you want to handle different timezones, you need to store the standard format, something like 'America/Los_Angeles'.
For example:
SELECT TO_TIMESTAMP_TZ(PublishDate, 'MON-DD-YY HH24:MI:SS TZR')
from (select 'Sep-29-07 13:45:00 America/Los_Angeles' as PublishDate from dual) x;
You can this modify this with 'PDT', but that is not acceptable by itself.
Related
I have a table name as business_details and column name business_date whose data type is varchar2.
Now i have to find out the data between two different dates and date format like : 12-JUN-18 21:15:13
Means, 12 Jun, 2018.
Kindly help me to write a query which can fetch the data between these two dates :12-JUN-18 21:15:13 and 25-JUN-18 18:15:32
I assume that in table business_details you have an column date or something like that.
Than use something like this:
select business_date from business_details
where date between TO_DATE ('12-JUN-18 21:15:13','dd-MM-yy hh:mi:ss')
AND TO_DATE ('25-JUN-18 18:15:32','dd-MM-yy hh:mi:ss');
Assuming your business_date is actually a string in the format you've shown (and it isn't really a date your client is just showing in that format), you need to convert that to a date type, as well as converting the string literals.
select *
from business_details
where to_date(business_date, 'DD-MON-RR HH24:MI:SS')
between to_date('12-JUN-18 21:15:13', 'DD-MON-RR HH24:MI:SS')
and to_date('25-JUN-18 18:15:32', 'DD-MON-RR HH24:MI:SS');
The format model you tried to use in a comment did this:
to_date('12-JUN-18 21:15:13', 'DD-MM-YYYY HH24:MI:SS')
is using MM rather than MON, which works anyway by default - although using month numbers is safer anyway as they aren't dependent on your session language. But more importantly it uses YYYY. If you pass a 2-digit value like 18 and try to convert with YYYY you get the wrong year:
select to_date('12-JUN-18 21:15:13', 'DD-MON-YYYY HH24:MI:SS') form dual;
TO_DATE('12-JUN-182
-------------------
0018-06-12 21:15:13
In your version your business_date was being converted implicitly so would use NLS settings, which are presumably using RR already. But that means you were comparing a date in 2018 with a range in 0018, which is why nothing matched.
You could also use timestamp literals for the fixed values (unless those strings are actually being passed in from somewhere else):
select *
from business_details
where to_date(business_date, 'DD-MON-RR HH24:MI:SS')
between cast(timestamp '2018-06-12 21:15:13' as date)
and cast(timestamp '2018-06-25 18:15:32' as date);
For suppose if I want sysdate,
SELECT SYSDATE as system_date FROM DUAL;
should output in the following format
14-Feb-2018 T19:50:02+00:00
i.e.,
DD-MMM-YYYY Thh:mm:ss+HH:MM
Assuming you know the date represents UTC and want the +00:00 part to be fixed:
select to_char(sysdate, 'DD-Mon-YYYY "T"HH24:MI:SS"+00:00"') from dual;
TO_CHAR(SYSDATE,'DD-MON-YYYY"T"HH24:
------------------------------------
14-Feb-2018 T20:13:08+00:00
The format model elements are in the documentation. That includes a section on character literals, which I've used for the fixed T and +00:00 parts.
As #mathguy said, this seems a bit unusual; and you might actually to leave the column as a native date and have your application or reporting tool or whatever format it for you. It depends what exactly you're doing, and whether you actually want a string value directly from the query.
As your updated question now doesn't have that pseudo-timezone, it's now even simpler, but the same idea:
select to_char(sysdate, 'DD-Mon-YYYY "T"HH24:MI:SS') from dual;
TO_CHAR(SYSDATE,'DD-MON-YYYY"T
------------------------------
14-Feb-2018 T20:17:50
If you're working with a data type that knows about time zones - i.e. not a plain DATE or TIMESTAMP - you can include those in the formatting using the appropriate model elements:
select to_char(systimestamp, 'DD-Mon-YYYY "T"HH24:MI:SSTZH:TZM') from dual;
TO_CHAR(SYSTIMESTAMP,'DD-MON-YYYY"T"
------------------------------------
14-Feb-2018 T20:24:58+00:00
which happens to still show +00:00 because my system is in the UK. With a different value it shows something appropriate:
alter session set time_zone = 'AMERICA/NEW_YORK';
select to_char(current_timestamp, 'DD-Mon-YYYY "T"HH24:MI:SSTZH:TZM') from dual;
TO_CHAR(CURRENT_TIMESTAMP,'DD-MON-YY
------------------------------------
14-Feb-2018 T15:28:57-05:00
Notice now I'm using systimestamp and current_timestamp, which are TZ-aware, and not sysdate or current_date which are not - you'l get an error if you try to get the TZH or TZM elements from those.
The format you are requesting doesn't make much sense. +00:00 is the time zone offset (otherwise what is it?) but in Oracle the DATE data type does not know about time zones. Only the Oracle data type TIMESTAMP WITH TIME ZONE should be formatted that way in Oracle.
Here is how this should be done with timestamps WITH TIME ZONE. Note that the standard SYSTIMESTAMP function is a timestamp WITH TIME ZONE. In the query below, you can see how the timestamp is formatted using my session's default, and then using an explicit format model.
SQL> select systimestamp,
2 to_char(systimestamp, 'dd-Mon-yyyy "T"hh24:mi:sstzh:tzm') as ts
3 from dual
4 ;
SYSTIMESTAMP TS
------------------------------------------- ---------------------------
14-FEB-18 12.14.18.537000 PM -08:00 14-Feb-2018 T12:14:18-08:00
I have to convert a SYSDATE date to a specific date format. That format must be something like this: '2016-11-23T15:12:48Z'. I think this is a weird date format but is the requirements that I have.
This must be a date to send in a Web Service message.
In Oracle (12c or 11g) I have some function to transform a date in this specific format? Thanks.
That will give you ISO-8601 mentioned in comment:
select to_char(systimestamp,'YYYY-MM-DD"T"hh24:mi:sstzh:tzm') isodt from dual;
If you really want Z instead of timezone you can use:
select to_char(cast(systimestamp as timestamp) at time zone 'UTC',
'yyyy-mm-dd"T"hh24:mi:ss"Z"')
from dual;
Since the "Z" in the timestamp format you're after means "This is in UTC", you should first make sure your sysdate is returned in UTC. You can do this by using systimestamp and sys_extract_utc() like so:
select to_char(sys_extract_utc(systimestamp), 'yyyy-mm-dd"T"hh24:mi:ss"Z"') dt_as_utc
from dual;
DT_AS_UTC
--------------------
2016-11-28T10:33:49Z
I have following 2015-06-17T00:00:00.000+05:00 string.
I want to convert this string to Date using oracle sql.
I tried lot of format mask but none works for me :
SELECT TO_DATE('2015-06-17T00:00:00.000+05:00','yyyy-mm-dd HH24:MI:SS TZR') FROM DUAL;
Any idea which format mask should i apply for above conversion.
Also please note that i only need date information i.e (mm-dd-yyyy). So its also ok if the conversion results in date information only (i.e skipping time information)
This should work:
SELECT TO_DATE(SUBSTR('2015-06-17T00:00:00.000+05:00',1,10),'yyyy-mm-dd') from dual
If you need to keep track of the time zone you should probably look at something like this:
SELECT CAST(TO_TIMESTAMP_TZ('2015-06-17T00:00:00.000+05:00','yyyy-mm-dd"T"HH24:MI:SS.FFTZH:TZM') AT TIME ZONE 'UTC' AS DATE) FROM DUAL;
We are using Oracle 11g and we have a table with a timestamp column which doesn't contain the timezone (simply defined as TIMESTAMP). The timezone is written in a different column.
I want to be able to use the 'max' function while taking the timezone into account.
There's probably a function to convert timezones easily and I just don't know it.
I want to be able to do something like this:
SELECT max(covert_to_timezone(my_timestamp, my_timezone, 'GMT')) FROM my_table
How can I do that?
==================
Solution: According to Ajith Sasidharan's answer:
SELECT max(from_tz(my_timestamp, my_timezone) at time zone '0:00') FROM my_table
Update: If you want to compare dates, simply use 'cast':
SELECT max(from_tz(cast(my_date as timestamp), my_timezone) at time zone '0:00') FROM my_table
Btw, now I got 'ORA 01878' errors, meaning our table contains invalid times (DST shit).
I guess you want a function 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