Oracle date format issue - oracle

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.

Related

Converting date from DD-MON-YY to DD-MM-YYYY with NLS_DATE_FORMAT

I'm trying to store date type data from Oracle FORMS with format mask as like DD-MM-YYYY but every time it store as like DD/MON/YY.
I already alter session with NLS_DATE_FORMAT, but result is as same as before.
Oracle internal date format that is written in the table is something you can't change in any way, but, in the same time, it is irrelevant. If you are dealing with DATE type column then you should know that it containes both the date and the time. How, where and when you will show it or use it is on you. Here is a sample of a few formats derived from that original Oracle DATE format...
WITH
t AS
(
Select SYSDATE "MY_DATE_COLUMN" From Dual
)
Select
MY_DATE_COLUMN "DATE_DEFAULT_FORMAT",
To_Char(MY_DATE_COLUMN, 'mm-dd-yyyy') "DATE_1",
To_Char(MY_DATE_COLUMN, 'yyyy/mm/dd') "DATE_2",
To_Char(MY_DATE_COLUMN, 'dd.mm.yyyy') "DATE_3",
To_Char(MY_DATE_COLUMN, 'dd.mm.yyyy hh24:mi:ss') "DATE_4"
From t
DATE_DEFAULT_FORMAT
DATE_1
DATE_2
DATE_3
DATE_4
22-OCT-22
10-22-2022
2022/10/22
22.10.2022
22.10.2022 10:59:44
You can find a lot more about the theme at https://www.oracletutorial.com/oracle-basics/oracle-date/
Regards...
In Oracle, a DATE is a binary data-type consisting of 7-bytes (representing century, year-of-century, month, day, hour, minute and second). It ALWAYS has those 7 components and it is NEVER stored in any particular human-readable format.
every time it store as like DD/MON/YY.
As already mentioned, no, it does not store a date like that; the database stores dates as 7 bytes.
What you are seeing is that the client application, that you are using to connect to the database, is receiving the 7-byte binary date value and is choosing to convert it to something that is more easily comprehensible to you, the user, and is defaulting to converting the date to a string with the format DD/MON/RR.
What you should be doing is changing how the dates are displayed by the client application by either:
Change the settings in the Toad (View > Toad Options > Data Grids > Data and set the Date Format option) and allow Toad to implicitly format the string; or
Use TO_CHAR to explicitly format the date (TO_CHAR(column_name, 'DD-MM-YYYY')).
I'm trying to store data as like DD-MM-YYYY.
If you want to store a date then STORE it as a date (which has no format) and format it when you DISPLAY it.
If you have a valid business case to store it with a format then you will need to store it as a string, rather than as a date, because you can format strings; however, this is generally considered bad practice and should be avoided.
Sadman, to add to what others have posted I suggest you do not write your applications with reliance on the NLS_DATE_FORMAT parameter but rather you screens and application should specify the expected DATE entry format and the code should use the TO_DATE function to store the data into the database. All application SQL should use the TO_CHAR function to format date output for display.

timestamp conversion issue in oracle

I am trying to convert data from oracle date field which is in local eastern time to UTC format
and I'm using below function to achieve it
CAST("date field" AS TIMESTAMP) AT TIME ZONE 'UTC'
and my output looks like below
06-NOV-19 09.55.21.000000 AM
However i need to format the output to below format
11/6/2019 9:55:21.000000000 AM
Is there an oracle function which i directly use to do above format conversion?
Formatting and the actual value are separate things. The value, you say is correct, however you want it to look in a different style than the default.
Look into TO_CHAR if you want a specifically formatted string output. eg:
SELECT TO_char(cast(sysdate as timestamp) at time zone 'UTC','mm/dd/yyyy hh:mi:ssxFF am') from dual;
11/18/2019 07:39:42.000000 pm
I tried on my database and the same consult turn the expected result. Try to confer this web site that will show the config of date in oracle:
http://blog.marvinsiq.com/2018/08/23/formatar-data-e-hora-no-oracle-sql-developer/

Storing timestamp values stored in a varchar2 column into a date column in oracle

I have a column in a table that stores timestamp values as
"2018-01-12 16:13:51.107000000", i need to insert this column into a date column in another table, what format mask do i have to use here..
I have used the mask 'YYYY-MM-DD HH24:MI:SS.FF' but shows 'date format not recognized'.
I am assuming that you were trying to use TO_DATE on your text timestamp data. This won't work, because Oracle dates do not store anything more precise than seconds. Since your timestamps have fractional seconds, you may use TO_TIMESTAMP here, then cast that actual timestamp to a date:
SELECT
CAST(TO_TIMESTAMP('2018-01-12 16:13:51.100000',
'YYYY-MM-DD HH24:MI:SS.FF') AS DATE)
FROM dual;
12.01.2018 16:13:51
Demo
You can do this with a single call to TO_DATE(), but you must give the correct format model. Note that this solution is simpler (and possibly faster - if that matters) than converting to a timestamp and then casting to date.
If you want TO_DATE() to ignore part of the input string, you can use the "boilerplate text" syntax in the format model. That is enclosed in double quotes. For example, if your string included the letter T somewhere and it had to be ignored, you would include "T" in the same position in the format model.
This has some flexibility. In your case, you must ignore the decimal point, and up to nine decimal digits (the maximum for timestamp in Oracle). The format model will allow you to use ".999999999" (or any other digits, but 9999... is used by most programmers) to ignore a decimal point and UP TO nine digits after that.
Demo: (notice the double-quoted "boilerplate text" in the format model)
select to_date('2018-01-12 16:13:51.100000',
'YYYY-MM-DD HH24:MI:SS".999999999"') as dt
from dual;
DT
-------------------
2018-01-12 16:13:51

How to load files using SQLLDR with date format as yyyymmddhhmmss?

I need to load a table with a .csv file which contains date "20140825145416".
I have tried using (DT date "yyyymmdd hh24:mm:ss") in my control file.
It throws an error as ORA-01821: date format not recognized
I require the data in table as "MM/DD/YYYY HH:MM:SS".
Sample data : 20140825145416
thanks in advance.
Well, I would be remiss if I did not point out that the correct answer is to never store dates as VARCHAR2 data, but make it a proper DATE column and load it like this:
DT DATE "YYYYMMDDHH24MISS"
Formatting is done when selecting. It will make your life so much easier if you ever need to use that date in a calculation.
That out of the way, If you have no control over the database and have to store it as a VARCHAR2, first convert to a date, then use to_char to format it before inserting:
DT CHAR "to_char(to_date(:DT, 'YYYYMMDDHH24MISS'), 'MM/DD/YYYY HH24:MI:SS')"
Note 'MI' is used for minutes. You had a typo where you used 'MM' (months) again for minutes.
I know it's already been said in the previous answer, but it's so important, it's worth repeating. Do not store dates as varchars !!
If your DT column is timestamp then this might work
DT CHAR(25) date_format TIMESTAMP mask "yyyymmddhhmiss"
I used something like this in external tables. Maybe this might help
https://docs.oracle.com/cd/B19306_01/server.102/b14215/et_concepts.htm
and
https://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:8128892010789

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