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.
Related
If I try a query as
select * from hr.employees
by sqldeveloper I have an output where the field HIRE_DATE is display so:
21-GIU-07
The data format is in according of the land (Italy)
If I use Tora or Toad (an old version) for the query the same row and the same field is display as
HIRE_DATE 2007-06-21 00:00:00
I am not undestand why there is time value in the field HIRE_DATE. In the example is 0 but I have found table where the time is set.
Why Tora/Toad show the time too and not in sqldeveloper ?
Thanks in advance anyone wants to answer
There is a parameter called NLS_DATE_FORMAT which specifies the default format for date data type.
The NLS_DATE_FORMAT has an order of overriding precedence, and tool specific NLS paramter settings will override it. That is the reason why two different tools has different outputs.
This is the most usual order of overriding precendence :
NLS_DATE_FORMAT is in the database initialization parameters, will be overriden by,
Settings of OS environment variable on the client machine, will be overriden by,
NLS parameter setting at session level with ALTER SESSION statements, will be overriden by,
to_date and to_char functions at the sql statement level.
Having said all that, in your situation, you need to check the NLS_DATE_FORMAT in both the tools.
Last but most important, check this link and learn more about the NLS_DATE_FORMAT.
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.
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';
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.
I am using Oracle10g database in which a table contains a Column with Date DataType. I am using the following query to get the record:
select to_char(START_TIME, 'YYMMDD HH24:MI:SS') from table;
So from above query, the result will be of type VARCHAR. I have tried to_Date() method but resulted in displaying only DATE. Can i convert VARCHAR to DATETIME format? The result should be of type DATETIME. Please help me how to resolve this problem.
an Oracle date contains both date and time so you can consider it a datetime (there is no datatype of datetime in Oracle). how is DISPLAYS when you select it is entirely up to your client. the default display setting is controlled by the NLS_DATE_FORMAT parameter. If you're just using the date in your pl/sql block then just assign it into a date datatype and select into that variable without to_char and it will work just fine and contain whatever time component is present in your table.
to control the display, for example using nls_date_format:
SQL> select a from datetest;
A
---------
19-FEB-13
SQL> alter session set nls_date_format='YYMMDD HH24:MI:SS';
Session altered.
SQL> select a from datetest;
A
---------------
130219 07:59:38
but again, this is only for display.
Oracle's Date type fields contain date/time values, therefore converting it to Datetime does not make any sense (it's already datetime)
Read more about oracle date types here
Yeah the Date datatype will meet your needs but you will have to jump through some hoops every time to get the exact time out of it. Definitely use the Timestamp datatype.