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

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.

Related

How to get future and past dates in mm/dd/yyyy and dd/mm/yyyy format in Oracle SQL 18C?

I want to get display of future and past dates in mm/dd/yyyy and dd/mm/yyyy format in Oracle SQL 18C using SQL functions, so I want the code for it. I tried code select sysdate from dual and I get the output 21-JAN-23, but I want output of future and past dates like 23/11/2033 and 16/12/2009 in mm/dd/yyyy and dd/mm/yyyy format.
Format date using TO_CHAR() function
SELECT
TO_CHAR( SYSDATE, 'FMMonth DD, YYYY' )
FROM
dual;
The output would be:
August 1, 2017
Creating a Future or Past Date
In Oracle, a DATE is a binary data type that ALWAYS consists of 7 bytes representing century, year-of-century, month, day, hour, minute and second and is NEVER stored in any particular human-readable format.
Therefore, if you want to get a DATE data type in a particular format then it is impossible as dates never have any format when they are stored.
If you want to get a date you can use:
A date literal:
SELECT DATE '2023-12-31' FROM DUAL;
or, the TO_DATE function:
SELECT TO_DATE('31/12/2023', 'MM/DD/YYYY') FROM DUAL;
Displaying Dates in a Client Application
However, if the problem is how to display a date in a particular format then you need to convert the binary DATE value to a string.
Most client applications (SQL*Plus, SQL Developer, TOAD, C#, Java, etc.) will implicitly convert a binary date to something that is human-readable when they display it and will have settings in the application that determine the default format that it applies to dates.
For SQL*Plus and SQL Developer, you can modify the NLS_DATE_FORMAT session parameter to change how that client application displays dates (note: this does not change how Oracle stores the dates internally, only how it is displayed by the client).
For example:
ALTER SESSION SET NLS_DATE_FORMAT = 'YYYY-MM-DD HH24:MI:SS';
or:
ALTER SESSION SET NLS_DATE_FORMAT = 'MM/DD/YYYY';
And then the client application will display dates in that format when you use a SELECT statement.
For other client applications you will need to check the documentation for that application.
Explicitly Formatting Dates as Strings
If you want to display a DATE in a particular format independent of any settings in the client application then you will need to convert the date to a string.
Using TO_CHAR:
SELECT TO_CHAR(DATE '2023-12-31', 'MM/DD/YYYY') AS formatted_date FROM DUAL;
Or, if you are generating the date and formatting it (rather than taking an existing date and formatting it) then you could just use a string literal:
SELECT '31/12/2023' AS formatted_date FROM DUAL;

Oracle TRUNC function doesn't work in perl

I try to insert the created field in my_table. The created field has a datetime type. In my_table the field my_created has a date format. So I try to TRUNC the created field. However I'm getting the error ORA-01830: date format picture ends before converting entire input stringwhile inserting the truncated value. It seems, that the time is still there but is reset to 00:00. how can I get only the date without time? It happens only in perl. I'm getting only date in toad.
Very simplified code looks like:
my $SQL="SELECT
TRUNC(CREATED),
FROM
DBA_OBJECTS";
my $sth = $db->prepare($SQL);
$sth->execute();
my $date = $sth->fetchrow();
$SQL = "INSERT INTO MY_TABLE
(MY_CREATED)
VALUES (?)";
my $stmt = $dbh_master->prepare($SQL);
$stmt->execute($date);
EDIT:
I found an ugly workaround and I'm executing it like this:
$stmt->execute(substr($date, 0, 10));
However maybe someone has a nicer solution.
How can I get only the date without time?
In Oracle, a DATE is a binary data type that is composed of 7 bytes representing: century, year-of-century, month, day, hour, minute and second. It ALWAYS has those binary components so if you want an Oracle DATE data type then you cannot get it without a time.
The Oracle DATE data type was released with Oracle version 2 in 1979 and predates the ANSI/ISO standard (of 1986, where a DATE does not have a time component) and Oracle has maintained backwards compatibility with their previous data types rather than adopting the ANSI standard.
If you use the TRUNC(date_value, format_model) function then it will set the binary components of the DATE, up to the specified format model, to the minimum (0 for hours, minutes and seconds, 1 for days and months) but it will NOT give you a data type that does not have a time component.
It happens only in perl. I'm getting only date in toad.
No, you are getting the entire 7 byte binary value in Toad; however, the user interface is only choosing to show you the date component. There should be a setting in the preferences that can set the date format in Toad which will let you see the entire date-time components.
Oracle SQL/Plus and SQL Developer use the NLS_DATE_FORMAT session parameter and Toad may also be able to use that.
If you want to get the value as a DATE then it will always have a time component (even if you set that time component to zeros using TRUNC).
If you want to get the date so that it is formatted in a way without a time component then you need to convert it to another data type and can use TO_CHAR to format it as a string:
SELECT TO_CHAR(CREATED, 'YYYY-MM-DD')
FROM DBA_OBJECTS
But then you will be returning a (formatted) string and not a DATE data type.

Change timestamp format in oracle

I am getting timestamp format as '01-APR-21 12.02.00.496677000 AM' from oracle, I want to change the format to load data into sql server column with datatype as datetime.
Current Input: '01-APR-21 12.02.00.496677000 AM'
Expected Output : 2021-04-01 12:02:00.496
I need to write the code in oracle to change timestamp format
Thanks
Neha
A TIMESTAMP is a binary data format consisting of 7-20 bytes (century, year-of-century, month, day, hour, minute, second, up to 6 bytes for fractional seconds and up to 7 bytes for time zone information); it does NOT have a format.
Why am I seeing the TIMESTAMP with a format?
You are seeing it with a format because whatever user interface you are using to access the database has decided that it is more useful to display the binary information as a formatted string rather than returning the raw byte values to you.
Typically, for SQL/Plus and SQL Developer, this is managed by the NLS_TIMESTAMP_FORMAT session parameter. Other user interfaces will have different mechanisms by which they manage the default format of dates and timestamps.
If you want to change the default for SQL/Plus (and SQl Developer) then you can use:
ALTER SESSION SET NLS_TIMESTAMP_FORMAT = 'YYYY-MM-DD HH24:MI:SS.FF9';
(Or whatever format you would like.)
How can I format the TIMESTAMP value?
If you then want to display the timestamp with a format (remember, a TIMESTAMP is not stored with any format) then you want to use TO_CHAR to convert it to a string where it can have a format.
If you want to format the TIMESTAMP as a YYYY-MM-DD HH24:MI:SS.FF3 string then use:
SELECT TO_CHAR( your_timestamp_column, 'YYYY-MM-DD HH24:MI:SS.FF3' )
FROM your_table
How can I convert a formatted string back to a TIMESTAMP?
From your comment:
I loaded the oracle data into a different SQL table where date format was varchar and data got loaded as '01-APR-21 12.02.00.496677000 AM'
Use TO_TIMESTAMP:
SELECT TO_TIMESTAMP( your_string_column, 'DD-MON-RR HH12:MI:SS.FF9 AM' )
FROM your_table
(Note: that this will convert your string to a binary TIMESTAMP and then whatever user interface you are using will use its rules on how to display it; and if the default format it is using is DD-MON-RR HH12:MI:SS.FF9 AM then the query above will look like it has done nothing; it has, the UI is just implicitly converting it back to a string to display it.)

Oracle: Store date in a BLOB field and retrieve as date when needed

I am using this expression to store a date directly in a BLOB field:
utl_raw.cast_to_raw(sysdate)
This works fine, but when I need to retrieve this date, I can't manage to retrieve the time as well, only the date, with the function below.
utl_raw.cast_to_varchar2(utl_raw.cast_to_raw(sysdate))
Is there a way to retrieve the date I stored with time?
PS: I know I can just convert the date to a varchar with TO_CHAR and datetime format before storing it, but let's assume I can't do it right now.
utl_raw.cast_to_raw accepts a string, not a date, therefore what you are capturing is effectively to_char(sysdate). This will use the current nls_date_format which is typically something like DD-MON-RRRR (and nls_date_language which might be English but could vary depending on user desktop settings). You'll need to either change the default or specify a different format, e.g.
utl_raw.cast_to_raw(to_char(sysdate,'YYYY-MM-DD HH24:MI:SS'))

How to Insert a Timestamp in Oracle in a Specific Format

I am at a loss as how to insert the current time in a different format than the default. Can somebody help explain?
Here is how my table was created:
CREATE TABLE ACTIVITY_LOG
(
TIME TIMESTAMP NOT NULL
, ACTIVITY VARCHAR2(200) NOT NULL
);
My insert command works:
insert into activity_log
values (localtimestamp,'blah');
But how do i insert the localtimestamp value into my table in a different format using the various MM DD YY HH MM SS tags? I've tried the following, but it gives me the ORA-1830: date format picture ends before converting entire input string error.
insert into activity_log
values (to_timestamp(localtimestamp,'YYYY/MM/DD'),'blah');
You don't insert a timestamp in a particular format. Timestamps (and dates) are stored in the database using an internal representation, which is betwen 7 and 11 bytes depending on the type and precision. There is more about that in this question, among others.
Your client or application decides how to display the value in a human-readable string form.
When you do:
to_timestamp(localtimestamp,'YYYY/MM/DD')
you are implicitly converting the localtimestamp to a string, using your session's NLS settings, and then converting it back to a timestamp. That may incidentally change the value - losing precision - but won't change how the value is stored internally. In your case the mismatch between the NLS setting and the format you are supplying is leading to an ORA-01830 error.
So your first insert is correct (assuming you really want the session time, not the server time). If you want to see the stored values in a particular format then either change your client session's NLS settings, or preferably format it explicitly when you query it, e.g.:
select to_char(time, 'YYYY-MM-DD HH24:MI:SS.FF3') from activity_log
You don't seem to provide any indication of what your 'localtimestamp' is - is that pseudocode? A variable name? A column you haven't shown the definition for?
What data type is 'localtimestamp'? What data does it contain? Pertinent questions as other answers point out, because if it truly is a time stamp then oracle will be converting it to a string for you, before passing that string to to_timestamp() in your final query. Your initial stab at it should just work if the variable is a timestamp, containing a timestamp
Ultimately "date format picture ends" means "you passed me a string looking like '2017-05-17 12:45:59', but claimed it was only 'yyyy-mm-dd'. What was I expected to do with the rest of it?"
Your current final comment on your question "I was hoping to look in the table and see a useful looking time" - that's your query tool's problem. Have a look in the setting of your query tool and change the date format it displays. As has been noted, dates in oracle are stored as a decimal number days since a certain moment in time. If 0 represents 01 Jan 1970, then 1.75 represents 6pm on the 2 Jan 1970. It is up to the end program the user is using, to format the date into something you like.. you cannot "insert a timestamp with a different format" because time stamps don't have a format any more than a number like 1.75 has a format. It is what your query does with it when it gets it out, that gives it the format:
To_char(timestampcol, 'yyyy mm did')
To-char(tomestampcol, 'mon dd yyyy')
These use oracles built in date formatter, that turns that decimal number of the date into a string in the given format; you will see a string.. or you can just write "select * from table" and run it in TOAD and toad will show you the dates according to the format in settings, or you can write a c# program and get a load of date objects out and call my date.ToString("yyyy-MM-dd") on them to format them. The idea I'm trying to get across is that you don't pick the date format on the way in, you pick it on the way out, if you don't like what you're looking at, you have to change it on the way out, not the way in

Resources