Getting Oracle Error While trying to convert varchar2 column and retrieve data using BIP report - oracle

We have column (field) attribute14 in the oracle table which is of data type VARCHAR2 and is storing date in format '20-MAY-22' (DD-MON-YY).
We have a requirement to develop a BIP report in oracle fusion and get output based on the parameter date passed & if no parameter is based then all the records.
But as we all know any date parameter in the oracle Data set or BIP report will be in the below format.
Date Parameter: '2021-12-05T19:00:00.000-05:00'
And upon trying the below query when the parameter is passed the following error.
select *
from ab cet
where 1=1
AND TO_DATE(cet. attribute14, DD-MON-YY NLS_date_language=AMERICAN")
IN ( nvl(:void_date.TO_DATE(cet.attribute14, DD-MON-YY.NLS_date_language=AMERICAN")))
The below error comes when the parameter value is passed.
Error: ORA-01847: day of the month must be between 1 and the last day of the month.

You need to use single quotes around the date format. Seems your language is also incorrect. Details can be read in the Oracle documentation. Here is a simple example.
SELECT TO_DATE(
'20-MAY-22',
'DD-MON-YY',
'NLS_DATE_LANGUAGE = American')
FROM DUAL;
https://docs.oracle.com/cd/B19306_01/server.102/b14200/functions183.htm

Related

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.

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;

Decode function not working in SQL*Loader

I am using SQL*Loader to import data from CSV to an Oracle table. My data has multiple date fields and i need to replace null values with '0001-01-01-00-00-00' and other values need to follow date format.
I used decode option like
decode(:QUOTE_CREATE_DT,NULL,'0001-01-01-00.00.00',
TO_DATE(:QUOTE_CREATE_DT,'MM/DD/YYYY HH24:MI:SS'))
which is not working when null values occur.
SQL*Loader error:
Record 2: Rejected - Error on table EX_QUOTE_MO_SAMPLE, column QUOTE_CREATE_DT.
ORA-01847: day of month must be between 1 and last day of month
Your version is relying on implicit conversion using the SQL*Loader session's NLS settings.
You could use decode or coalesce or nvl to provide a date string in the format you're converting, but inside the to_date() call:
QUOTE_CREATE_DT "TO_DATE(NVL(:QUOTE_CREATE_DT, '01/01/0001'), 'MM/DD/YYYY')"

Oracle - Date Format

I need your assistance with converting Oracle dates.
I have a column that stores dates like this 20150731 00:00:34.220. However, I would like to show the column like this 20150731 but when I run a simple select statement to test output I get the following error.
select TO_DATE('20150731 00:00:34.550','YYYYMMDD')
from dual
Error
ORA-01830: date format picture ends before converting entire input string
This query
select TO_DATE('20150731 00:00:34.550','YYYYMMDD')
from dual
leads to error
ORA-01830: date format picture ends before converting entire input string
because you pass string with length 22 characters, but at the same time you pass date format with 8 characters, which obviously doesn't correspond to string. You should write the query as
select to_timestamp('20150731 00:00:34.550','yyyymmdd hh24:mi:ss.ff3')
from dual
As for your table, since you have varchar2 column with dates, you have to take care about table content. Query requires exect matching of the source string and date format.
If you want to show only date without time and you don't need to process this string as date, you can make just
select substr('20150731 00:00:34.550', 1, 8)
from dual
What is the data type of the column? If it is DATE (as it should be) then not it is not stored in the format you say. It is stored in an internal binary format. You would/should use the to_char function to DISPLAY it in whatever format you choose. If you do not use the to_char function, it will be displayed in the format specified by NLS_DATE_FORMAT, which can be specified at several locations.
As for your example, you passed a string format of yyyymmd hh:mi:ss.fff', but you provided a description mask of only YYYYMMDD. It doesn't know what to do with time component. In addition to that when you SELECT TO_DATE, oracle also has to do an implied TO_CHAR to convert it back to a string for display purposes.
In addition, you provided your to_date with a character string that included fractions of seconds. A DATE data type only resolves to seconds. If you need fractional seconds, you need to use TIMESTAMP, not DATE.
If your column is a varchar and you need a date output:
select TO_DATE(substr('20150731 00:00:34.550', 1, 8),'YYYYMMDD') from dual
If it's in a date format and you need a string output:
select to_char(your_column, 'YYYYMMDD') from your_table
Is that being stored in an Oracle datetime column? If not, you may have to do some manipulation to get it into a DD-MON-YYYY format. If it is being stored as a text string you could use SUBSTR( Date_field, Start_Position, Length) to get the first 8 characters. check out this link SUBSTR
Working on the assumption that you're not trying to change the value in the column, and are just trying to show it in the YYYYMMDD format -
As mentioned by a_horse_with_no_name, you'll just need to convert it to a character string. In this example I used systimestamp as my date:
SELECT TO_CHAR(systimestamp,'YYYYMMDD') FROM DUAL
Result:
20160121
That should give you the YYYYMMDD format you want to display.

Oracle - How to Convert VARCHAR to DATETIME format

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.

Resources