Proper DateTime Format on Oracle - oracle

I have problem with read a datetime format like this : 7/1/2014 6:02:09 AM , my goal is I want to compare some date in query oracle by to_date function but it seems different with the format, like there are no zero in the month, day, and hours.
my thought for the better query is like this to_date('7/1/2014 6:02:09 AM','MM/DD/YYYY HH:MI:SS AM')
but seems like I have wrong format for this.
Thanks in advance

Try this format instead:
to_date('07-01-2014 12:15','MM-DD-YYYY HH:MI'))

Finally I found the format,
I have to change first the AM PM format to 24 hours timestamp :
TO_CHAR(TO_DATE('01/01/1970 00:00:00', 'MM/DD/YYYY HH24:MI:SS')
thank you all audiences :)

Related

In Oracle, Covert Timestamp to Number

In oracle database, I have a table with one column for "Update date" having data type as Timestamp (MM/DD/YYYY HH:MI:SS AM).
I want to convert this timestamp into a number, including the Hour, Minute and Seconds part.
Anyone please suggest what should be my query to get the same.
Any help would be appreciated.
Thanks
Rajat Arora
Solution query---
Select
TO_NUMBER(TO_CHAR(Timestamp, 'YYYYMMDDHH24MISS'))
From
dual;

Covert into date format in Oracle

I have a character field in the database where Date value is stored.
Now I need to convert this character Date field value in below format
YYYY-MM-DDTHH24:MI:SS
I am using below conversion for it, but it's not working
to_char(lastupdate,'YYYY-MM-DD"T"HH24:MI:SS')
Could anyone please help out on this?
As you are stored your date in character literal like mm-dd-yyyy, you first need to convert it to date data_type using to_date and then to character using to_char as suggested by Nicholas Krasnov. Try like this,
SELECT to_char(to_date('01-01-2014', 'dd-mm-yyyy'),'YYYY-MM-DD"T"HH24:MI:SS')
FROM <table_name>;
If you want the date as 2014-01-17 00:00:00:
select TO_CHAR(TO_DATE(last_update, 'DD/MM/YYYY'), 'YYYY-MM-DD HH24:MI:SS') from <table>;

Months between sysdate to earlier date

I want to get the months between current date to earlier date.
SELECT MONTHS_BETWEEN(to_date(fld_valid_from,'yyyy-mm-dd hh24:mi:ss'),TO_DATE(sysdate, 'yyyy-mm-dd hh24:mi:ss')) num_months
FROM tbl_customer
But it's not working. I don't know if that's correct or not.
sysdate is already a date, and does not need to be converted to one using to_date(). I suspect that fld_valid_from is also a date.
What error message are you getting? Did you try comparing the two variables without the TO_DATE command?
SELECT MONTHS_BETWEEN (trunc(fld_valid_from),trunc(sysdate))
FROM tbl_customer
This would work if your column fld_valid_from is a date type. You're comparing a date with another date. You use TO_DATE to convert a string data type to a date data type.
With the TRUNC function, you remove the timestamp from the date, and get only...well, the date:
SELECT SYSDATE, TRUNC(SYSDATE) FROM DUAL;
SYSDATE TRUNC(SYSDATE)
---------------------------------------
16/07/2013 10:45:53 16/07/2013
Hope it helps.
Regards.

Oracle date format issue

I'm trying to export/import data in .csv format using SQLDeveloper. The source and destination databases are Oracle 11g. I'm having a hard time with the date formats. In the exported .csv, I see dates like:
31-AUG-09 11.54.00.000000000 AM
I'm trying to figure out the appropriate format string, but I don't know what the last element is before the meridian indicator (AM/PM). Here's the format string I have.
'DD-MON-YY HH.MI.SS.??????????? AM'
What should take the place of the question marks?
If these values are always 00000000000, then ??????????? could be just fine, in case you use DATE.
If you want to convert those 0s, you need to use a TIMESTAMP and FF9:
SELECT TO_TIMESTAMP( '31-AUG-09 11.54.00.000000000 AM',
'DD-MON-YY HH.MI.SS.FF9 AM' )
FROM dual
You have another problem though: Use MI instead of MM, since MM is month and can not be used twice.
You can use FF9 to represent the fractional seconds part.

Oracle Date formatting "2009-02-13T11:46:40+00:00"

I've had some brilliant help before and I'm hoping you can get me out of a hole again.
I've got a date coming in from a web service in this format:
2009-02-13T11:46:40+00:00
which to me looks like standard UTC format.
I need to insert it into an Oracle database, so I'm using to_date() on the insert. Problem is, I cant get a matching formatting string for it and keep getting "ORA-01861: literal does not match format string" errors.
I know its a fairly trivial problem but for some reason I cannot get it to accept the right format string. Any help appreciated.
Thanks :)
Gareth
You can directly convert it to a TIMESTAMP_WITH_TIME_ZONE datatype.
select
to_timestamp_tz('2009-02-13T11:46:40+00:00','YYYY-MM-DD"T"HH24:MI:SSTZH:TZM')
from
dual
TO_TIMESTAMP_TZ('2009-02-13T11:46:40+00:00','YYYY-MM-DD"T"HH24:MI:SSTZH:TZM
---------------------------------------------------------------------------
13-FEB-09 11.46.40.000000000 AM +00:00
(I'm assuming the input string is using a 24-hour clock since there is no AM/PM indicator.)
If you want to convert that to a simple DATE, you can, but it will lose the time zone information.
SELECT CAST(TO_TIMESTAMP_TZ(REPLACE('2009-02-13T11:46:40+00:00', 'T', ''), 'YYYY-MM-DD HH:MI:SS TZH:TZM') AS DATE)
FROM dual
To import date in specified format you can set nls_date_format.
Example:
alter session set nls_date_format='YYYY-MM-DD HH24:MI:SS'
This way your SQL statements can be shorter (no casts). For various mask look at Datetime Format Models

Resources