Date formats are displaying differently in Oracle - 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';

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;

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.

Azure Data Factory. Lookup date in Azure SQL DWH, use in Oracle Query

I have an Oracle database and I have to load dat from this database to Azure SQL DWH. This is done once every day. At the beginning of the pipeline I first do a lookup on SQL DWH to look for the latest date. The result for that is something like '2015-10-25'.
I want to use this date to query the Oracle database. But I allready found out, by trying the query on Oracle that the following code does not work:
Select * from Table 1 where day = '2015-10-25'
The date in the day column looks like 25-OCT-15 (DD-MON-YY).
I treid the following where clause:
where day = TO_DATE('2015-10-25','DD-MON-YY')
But then I get the error: "literal does not match format string"
I realy don't know how to make Oracle understand this T-SQL date format.
Your Oracle column is of date datatype. When you connect to an Oracle database and write a query against that date column, you will see its default format DD-MON-YY as per this reference.
You can override this setting by running an ALTER SESSION command, eg
ALTER SESSION SET NLS_DATE_FORMAT = 'YYYY MM DD';
but this is just in that local session. The data is still stored in Oracle in the same manner and it's simply the way you view it that is changing.
In Azure Data Factory (ADF v2) and your example, you are dealing with strings. So you must make sure any parameters you pass in are in the correct format or set to the correct datatype. The Oracle function TO_DATE converts strings to the date datatype. Therefore when passing in a string of format YYYY-MM-DD then that is the format you must use, to let the TO_DATE function know what you are passing in:
TO_DATE('2015-10-25','YYYY-MM-DD')
The function then successfully converts your parameter to a date datetype for correct comparison against the main date column.
You can try this query:
Select * from Table 1 where day = to_char (to_date('2015-10-25','YYYY-MM-DD'), 'DD-Mon-YY')
Reference this blog: how to convert YYYYMMDD to DD-Mon-YYYY in oracle?
Hope this helps.

How to format date fields using select query with Oracle

I am new to node red.
I am storing into the Oracle db in this date format dd-mm-yy hh:mi:ss.ff PM but I am getting a payload using a select query in this format yyyy-mm-ddThh:mi:ss.000Z but while retrieving data from Oracle db I want to print this format dd-mm-yyyy hh:mm:ss in my payload, how to write select query so that I can print the same date format,whatever stored in Oracle db
When you fetch a date value, it is stringified according to the NLS settings of your client. Looking at the same data with two different clients, you might see two different representations... of the same date.
If you want to choose the display format of a date, use Oracle function TO_CHAR in your query. It accepts a date and a format spec, and returns a string :
TO_CHAR(my_date_column, 'dd-mm-yyyy hh24:mi:ss')
It is also possible to change the default date format for the life time of your session, like :
ALTER SESSION SET NLS_DATE_FORMAT = 'dd-mm-yyyy hh24:mi:ss';
NB : if you are dealing with timestamps or timestamps with time zone, you need NLS_TIMESTAMP_FORMAT or NLS_TIMESTAMP_TZ_FORMAT.

Format of Date Parameter in Oracle BI Reports

In Oracle BI Publisher, I can't seem to find in what format the date parameters are passed in to the query. For example
select * from myTable where the_date = :the_date
Any ideas?
This would typically be via the NLS_DATE_FORMAT parameter, either set in your session or for the whole database. The default in oracle is 'DD-MON-YY'. Of course you may change your query to take it in any format you wish with the to_date conversion function.

Resources