change date format 'yyyy/mm/dd' to 'mm-dd-yyyy' in Oracle - oracle

I have inserted into a table in Oracle. My implementation without PLSQL would be:
SELECT to_date('1900-01-01','YYYY-MM-DD') + (rownum - 1) AS DT_CAL,
rownum AS NUM_JOUR
FROM dual
CONNECT BY to_date('1900-01-01','YYYY-MM-DD') + (rownum - 1) <=
to_date('2000-12-31','YYYY-MM-DD')
result is: 05/28/1900, not 1900-05-28. Can you help me understand what the problem is?

The DATE data type does not have a format; Oracle stores it as either 7- or 8-bytes and it is not until it is passed to a client program (i.e. SQL/Plus, SQL Developer, Toad, Java, Python, etc) and that client program formats it according to whatever rules it has that the date gets a format.
If you are using SQL/Plus or SQL Developer then it will use the NLS_DATE_FORMAT session parameter to format the date. You can change this using:
ALTER SESSION SET NLS_DATE_FORMAT = 'YYYY-MM-DD HH24:MI:SS';
(Be aware that this will only change the format in the current session and will not change it for any other sessions/users.)
If you want to give the date a particular format then you will need to convert it to a string.

to_date() takes your string parameter, matches it to the format you provide in the second parameter, and constructs a date field from it. The date field isn't using the format you provided in the second parameter - in fact it'll be stored using some internal data representation that has no format at all (a number, in all likelihood).
To present a format back out in the results from a date field, you can either:
Have the client executing the query set the NLS parameters (at session level) to provide a localized format, with an ALTER SESSION SET NLS_DATE_FORMAT='YYYY-MM-DD'; statement), or
Use to_char(..., 'YYYY-MM-DD') around your existing field to turn the date back into a string formatted the way you want to have it. Where you replace ... with your current column definition in the select.
Approach #1 is already happening, as there'll already be an NLS_DATE_FORMAT set that is producing the current format, but it's with a format you don't want, so if you can control it and change it there, you can do it that way. If you can't and you must have the format a single consistent other way, then #2 could be the way to go.

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.

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.

Date formats are displaying differently in Oracle

I am exporting the reports data from DB to excel. I am having 2 DB(Local & QA) schemas and having same queries for reports. Here I am facing an issue with date formats. we are using Oracle 12c.
Date Format should be 'DD-MON-YY' like '01-AUG-18'
While exporting data to excel,
from local db, it exporting date like '12-09-18'.
from QA db, it exporting date like '12-AUG-18'.
Query is :
select trim(to_date('01-01-1970 00','DD-MM-YYYY hh24') + (createdtime)/1000/60/60/24)
from TBL_RESPONSESUMMARY;
Can any one guide me please.
Use TO_CHAR to explicitly specify the format model you want to use to output the date:
SELECT TO_CHAR(DATE '1970-01-01' + createdtime/1000/60/60/24, 'DD-MON-YYYY')
FROM TBL_RESPONSESUMMARY;
What your query does is:
to_date('01-01-1970 00','DD-MM-YYYY hh24') generates your epoch value as a DATE data type;
+ (createdtime)/1000/60/60/24 adds your milliseconds offset value to it (and the data type will remain as a DATE);
trim( string_value ) takes a string value and removes leading and trailing white space; however, your input value is a DATE not a string so Oracle must perform an implicit cast using TO_CHAR and it uses the NLS_DATE_FORMAT session parameter. On your local database this is probably DD-MM-RR but on the QA database it is DD-MON-RR so you get different values.
You could change the NLS_DATE_FORMAT so that it is consistent on both databases - however, it is better to use an explicit call to TO_CHAR so you are not relying on session variables and implicit casts.
You have different date format between two databases. Run this command on local database to change date format
ALTER SESSION SET NLS_DATE_FORMAT = 'DD-Mon-YY';

Date Conversion in Oracle (YYYYMMDD format back to YYYYMMDD)

Hi I am pretty much new to Oracle and need an idea on how to do it.
Consideer I have a Text file and the value in the file is
'20180924'
'20180923'
I read these value from the Pro*c and then I add 29 days to it, so the value now changes from 20180924 to 20180953. I don't want that to happen i want the value to be added as
TO_DATE('20180924','yyyymmdd')+29 which returns the value as "23-OCT-18"
The answer is correct but i want the result to also be in the same YYYYMMDD format like 20181023.
How do i achieve this ?
Just format it back.
to_char(TO_DATE('20180924','yyyymmdd')+29, 'yyyymmdd')
Dates do not have a format - they are represented internally by 7 or 8 bytes.
If you want a date to have a format then you will need to convert it to a data type that can be formatted - i.e. a string:
TO_CHAR( TO_DATE( '20180924', 'yyyymmdd' ) + 29, 'YYYYMMDD' )
When SQL/Plus (or SQL Developer) displays date data types they implicitly convert them to strings (since this is more meaningful to you, the user, than displaying the raw bytes) and uses the NLS_DATE_FORMAT session parameter as the format model in this implicit conversion. If you want to change this then you can use:
ALTER SESSION SET NLS_DATE_FORMAT = 'YYYYMMDD';
and then your query:
TO_DATE( '20180924', 'yyyymmdd' ) + 29
should (provided your SQL client uses this setting and does not have their own internal format preferences) give the output you expect. (Note: this will change the default format for the client to display all dates in this session and not just this one statement.)
select TO_NUMBER((TO_CHAR(TO_DATE(HIREDATE,'DD-MON-YY'),'YYYYMMDD')) FROM EMP;
select TO_NUMBER((TO_CHAR(TO_DATE(HIREDATE,'DD-MON-YY'),'YYYYMMDD'))FROM EMP
20201101
select TO_NUMBER((TO_CHAR(TO_DATE(SYSDATE,'DD-MON-YY'),'YYYYMMDD')) FROM DUAL;

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