Cast NUMBER to DATE. Error ORA-00932: inconsistent datatypes: expected DATE got NUMBER - sql

I have a column with a datatype number but I want to convert the column into date. I tried using CAST function but it gives error
ORA-00932: inconsistent datatypes: expected DATE got NUMBER.
For example, 20221203 to 2022-12-03.
Any suggestions?
col_date is the column name
select cast(col_date as date)
from school

Try converting int to varchar and then varchar to date
select cast(cast(col_date as varchar(10)) as date)

Use the to_date() function:
select to_date(col_date, 'YYYYMMDD')
from school
That does an implicit conversion from number to string, but you can make it explicit:
select to_date(to_char(col_date), 'YYYYMMDD')
from school
Of course, it would be better to store your values as proper dates. You may have numbers which don't correspond to actual dates, and will need to decide how to handle those if you do.
Oracle's date datatype always has a time component, which will be set to midnight with this conversion. They have no intrinsic human-readable format - your client decides how to display, usually using your session NLS_DATE_FORMAT setting. You can change that with alter session, which will affect the display of all date values.
If you want to display the date as a string with a particularly format then you can reverse the process with the to_char() function:
select to_char(to_date(to_char(col_date), 'YYYYMMDD'), 'YYYY-MM-DD')
from school
If you only want it reformatted as a string, and don't need it as a real date at all, you could just format the number directly:
select to_char(col_date, 'FM0000G00G00', 'nls_numeric_characters='' -''')
from school
db<>fiddle
But either way, only do that for final display - leave it as an actual date (not string) for any processing, joins, storage etc.

Related

I have an oracle table which has date in dd-mm-yyyy and dd/mm/yyyy format in same field. Now i have to convert into one common format

I have an oracle table which has date in dd-mm-yyyy and dd/mm/yyyy format in same field. Now i have to convert into one common format.
Please suggest how to approach this?
I did tried but it is failing as it is failing due to invalid month.
Is there a way i can first identify what format the date is and then based on case statement i might convert.
or something easy way? Please
I trust you've learnt your lesson and you're now going to store these dates in the date data type.
Your two different date formats actually aren't important, Oracle already is a little over accepting when it comes to separating characters.
e.g
to_date('01/01/1900','dd-mm-yyyy')
Does not error
I did tried but it is failing as it is failing due to invalid month.
Your error is coming because you've allowed a value that doesn't match either of those formats into your string column.
If you are on version 12.2 at least (which you should be in 2020) then you can use the validate_conversion function to identify rows that don't convert to a date with your format (https://docs.oracle.com/en/database/oracle/oracle-database/12.2/sqlrf/VALIDATE_CONVERSION.html#GUID-DC485EEB-CB6D-42EF-97AA-4487884CB2CD)
select string_column
from my_table
where validate_conversion(string_column AS DATE,'dd/mm/yyyy') = 0
The other additional helper we got in 12.2 was the on conversion error clause of to_date. So you can do.
alter table my_table add my_date date;
update my_table set my_date = to_date(my_string default null on conversion error,'dd/mm/yyyy');
If you are confident that there is no other format than those two, a simple approach is replace():
update mytable set mystring = replace(mystring, '/', '-');
This turns all dates to format dd-mm-yyyy.
I would suggest taking a step forward and convert these strings to a date column.
alter table mytable add mydate date;
update mytable set mydate = to_date(replace(mystring, '/', '-'), 'dd-mm-yyyy');
This will fail if invalid date strings are met. I tend to consider that a good thing, since it clearly signals that this a problem with the data. If you want to avoid that, you can use on conversion error, available starting Oracle 12:
to_date(
replace(mystring, '/', '-') default null on conversion error,
'dd-mm-yyyy'
)
Then you can remove the string column, which is no longer needed.

How does ORACLE database convert DATE to NUMBER implicitly?

I'm trying to understand this function:
NVL2( NULL, ( SYSDATE - SYSDATE ), DATE '2020-05-24' ))
And its returned value:
NVL2(NULL,(SYSDATE-SYSDATE),DATE '2020-05-24'))
2458994
I'm having trouble understanding where that number, 2458994, comes from, as SYSDATE-SYSDATE is a NUMBER, and you cannot implicity convert a DATE to a NUMBER:
TO_NUMBER(DATE '2020-05-24')
ORA-01722: invalid number
The ORACLE SQL Language Reference NVL2 states:
If expr2 is numeric data, then Oracle Database determines which argument has the highest numeric precedence, implicitly converts the other argument to that data type, and returns that data type.
So my question is, what form of conversion is ORACLE SQL using on the DATE datatype to make it a NUMBER datatype?
It's effectively doing:
to_number(to_char(DATE '2020-05-24','J'))
The 'J' is (from the docs):
Julian day; the number of days since January 1, 4712 BC. Number specified with J must be integers.
If you run that manually it gets the same value you see:
select to_number(to_char(DATE '2020-05-24','J')) from dual;
2458994
It isn't obvious that it should be doing that, but it is. If the second argument is a plain (type-2) number then you get an error:
select NVL2(NULL,42,to_date('2020-05-24','YYYY-MM-DD')) from dual;
ORA-00932: inconsistent datatypes: expected NUMBER got DATE
If you dump the date subtraction result it comes back as a different (internal, undocumented as far as I can see) data type:
select dump(SYSDATE-SYSDATE) from dual;
Typ=14 Len=8: 114,133,37,0,0,0,0,0
That seems to cause the third argument to be converted to that same type; it's almost equivalent to:
select DATE '2020-05-24' - DATE '-4712-01-01' from dual;
2458993
So it looks like it's either doing something similar to that but adjusting it, or doing an internal version of the 'J' conversion, or... something else vaguely similar. It doesn't seem to be documented behaviour.

Difference in dates in Oracle

I am writing the following query
SELECT
TO_CHAR(TO_DATE(data_elaborazione, 'YYYYMMDD') -
TO_DATE(DATA_INS_AGG_SDS, 'YYYYMMDD') )AS DateDiff
FROM
dual
I want to calculate the difference between the two dates, in days, but I get an error:
ORA-00904: "DATA_INS_AGG_SDS": invalid identifier
The same goes for data_elaborazione too. Data_elaborazione, DATA_INS_AGG_SDS are both varchar types that contain dates as varchar
The error you are getting is cause by you referencing columns on dual that do not exist. You'll need to select these columns from the tables they actually exist in. As for your date arithmetic, it should return a number, which does not require TO_CHAR to display, unless you have some specific formatting concerns. Here is an example of date arithmetic. The second date value has a time component. So, the two columns due the date arithmetic and display the result, the second essentially rounds down to just get the number of days to an even integer value.
Please read the comments about data types. You should always work with values in their correct data types. Avoid storing either dates or numbers as strings in the DB.
-- start test_data
with some_data(begin_date, end_date) as
(select to_date('02/15/2017','MM/DD/YYYY'), to_date('04/03/2017 09:34:12','MM/DD/YYYY HH24:MI:SS') from dual)
-- end test data
select end_date - begin_date as num_days_diff_w_time,
FLOOR(end_date - begin_date) as num_days_diff_wo_time
from some_data;

Converting from VARCHAR to DATE

I have a db where I have converted 2 date columns to varchar for the purpose of getting 1 column substringed into another. However, now I don't seem to be able to convert the datatype when I try to use:
ALTER TABLE datacomplete
ALTER COLUMN yearmonth TYPE DATE; /*Can't find a way to specify a format*/
It throws this error:
ERROR: column "yearmonth" cannot be cast automatically to type date
Hint: You might need to specify "USING yearmonth::date".
I'm not sure how to use that command at all, could anyone potentially assist?
My first column is in the format of yyyy-mm-dd, however I'd like it to be yyyymm only, but I'm guessing this is easier once I convert the datatype to date and I can somehow switch formats.
The second column only shows the year so I need to convert it to date as format 'yyyy'.
UPDATE: The first one was solved, now I need to convert the second to 'yyyy'
ALTER TABLE pscomplete_1 ALTER COLUMN "year" TYPE DATE USING "year"::date;
It throws this error
15:12:51 [ALTER - 0 rows, 1.062 secs] [Code: 0, SQL State: 22007] ERROR: >invalid input syntax for type date: "2016"
... 1 statement(s) executed, 0 rows affected, exec/fetch time: 1.062/0.000 sec >[0 successful, 1 errors]
The USING keyword allows you to give the translation function to PostgreSQL.
For your first column it is easy for you already have a correct DATE format:
ALTER TABLE datacomplete ALTER COLUMN yearmonth TYPE DATE USING yearmonth::DATE;
For your second column it is unclear for PostgreSQL which exact date you want. Let's say we want the first of January of the given year:
ALTER TABLE datacomplete ALTER COLUMN year TYPE DATE USING (year || '-01-01')::DATE;
I understood you were starting from a VARCHAR column in the format 'YYYY-MM-DD'.
So I'll do the same.
And you want a column in the date format, and you want a yearmonth in all-digits format.
If I'm not forced to use leading zeroes in an all-digits column, I prefer INT to string columns.
And I prefer to make the same derivation only once if I can do that.
This is why I use a WITH clause (global table expression) to cast the varchar to date, and then I use the resulting date for the DATE_PART() function I use to create the yearmonth column. I have seen very often that date arithmetics are safer and often faster than subtring-ing the date literal (remember, the Americans format dates differently from the Europeans, there are different formats within Europe, and also in Asia, and not all like the ISO date format). So I derive yearmonth as the year multiplied by 100, plus the month, and as DATE_PART() returns a float, I cast the whole expression to INT.
So here goes:
WITH foo(varchar_dt) AS (
SELECT '2017-01-11'
UNION ALL SELECT '2016-12-11'
UNION ALL SELECT '2016-11-11'
UNION ALL SELECT '2016-10-11'
)
, foo_with_date AS (
SELECT
varchar_dt
, CAST(varchar_dt AS DATE) AS the_date
FROM foo
)
SELECT
varchar_dt
, the_date
, CAST(DATE_PART('year',the_date)*100 + DATE_PART('month',the_date) AS INT) AS yearmonth
FROM foo_with_date
;
varchar_dt|the_date |yearmonth
2017-01-11|2017-01-11| 201,701
2016-12-11|2016-12-11| 201,612
2016-11-11|2016-11-11| 201,611
2016-10-11|2016-10-11| 201,610
I can't help myself - I find this much cleaner, and filtering by yearmonth would become filtering by an integer, which is always at least a little bit faster than strings.
Happy playing
Marco the Sane

In Oracle, convert number(5,10) to date

When ececute the following SQL syntax in Oracle, always not success, please help.
40284.3878935185 represents '2010-04-16 09:18:34', with microsecond.
an epoch date of 01 January 1900 (like Excel).
create table temp1 (date1 number2(5,10));
insert into temp1(date1) values('40284.3878935185');
select to_date(date1, 'yyyy-mm-dd hh24:mi:ssxff') from temp1
Error report: SQL Error: ORA-01861: literal does not match format
string
01861. 00000 - "literal does not match format string"
*Cause: Literals in the input must be the same length as literals in
the format string (with the exception of leading whitespace). If the
"FX" modifier has been toggled on, the literal must match exactly,
with no extra whitespace.
*Action: Correct the format string to match the literal.
Thanks to Mark Bannister
Now the SQL syntax is:
select to_char(to_date('1899-12-30','yyyy-mm-dd') +
date1,'yyyy-mm-dd hh24:mi:ss') from temp1
but can't fetch the date format like 'yyyy-mm-dd hh24:mi:ss.ff'. Continue look for help.
Using an epoch date of 30 December 1899, try:
select to_date('1899-12-30','yyyy-mm-dd') + date1
Simple date addition doesn't work with timestamps, at least if you need to preserve the fractional seconds. When you do to_timestamp('1899-12-30','yyyy-mm-dd')+ date1 (in a comment on Mark's answer) the TIMESTAMP is implicitly converted to a DATE before the addition, to the overall answer is a DATE, and so doesn't have any fractional seconds; then you use to_char(..., '... .FF') it complains with ORA-01821.
You need to convert the number of days held by your date1 column into an interval. Fortunately Oracle provides a function to do exactly that, NUMTODSINTERVAL:
select to_timestamp('1899-12-30','YYYY-MM-DD')
+ numtodsinterval(date1, 'DAY') from temp3;
16-APR-10 09.18.33.999998400
You can then display that in your desired format, e.g. (using a CTE to provide your date1 value):
with temp3 as ( select 40284.3878935185 as date1 from dual)
select to_char(to_timestamp('1899-12-30','YYYY-MM-DD')
+ numtodsinterval(date1, 'DAY'), 'YYYY-MM-DD HH24:MI:SSXFF') from temp3;
2010-04-16 09:18:33.999998400
Or to restrict to thousandths of a second:
with temp3 as ( select 40284.3878935185 as date1 from dual)
select to_char(to_timestamp('1899-12-30','YYYY-MM-DD')+
+ numtodsinterval(date1, 'DAY'), 'YYYY-MM-DD HH24:MI:SS.FF3') from temp3;
2010-04-16 09:18:33.999
An epoch of 1899-12-30 sounds odd though, and doesn't correspond to Excel as you stated. It seems more likely that your expected result is wrong and it should be 2010-04-18, so I'd check your assumptions. Andrew also makes some good points, and you should be storing your value in the table in a TIMESTAMP column. If you receive data like this though, you still need something along these lines to convert it for storage at some point.
Don't know the epoch date exactly, but try something like:
select to_date('19700101','YYYYMMDD')+ :secs_since_epoch/86400 from dual;
Or, cast to timestamp like:
select cast(to_date('19700101', 'YYYYMMDD') + :secs_since_epoch/86400 as timestamp with local time zone) from dual;
I hope this doesn't come across too harshly, but you've got to totally rethink your approach here.
You're not keeping data types straight at all. Each line of your example misuses a data type.
TEMP1.DATE1 is not a date or a varchar2, but a NUMBER
you insert not the number 40284.3878935185, but the STRING >> '40284.3878935185' <<
your SELECT TO_DATE(...) uses the NUMBER Temp1.Date1 value, but treats it as a VARCHAR2 using the format block
I'm about 95% certain that you think Oracle transfers this data using simple block data copies. "Since each Oracle date is stored as a number anyway, why not just insert that number into the table?" Well, because when you're defining a column as a NUMBER you're telling Oracle "this is not a date." Oracle therefore does not manage it as a date.
Each of these type conversions is calculated by Oracle based on your current session variables. If you were in France, where the '.' is a thousands separator rather than a radix, the INSERT would completely fail.
All of these conversions with strings are modified by the locale in which Oracle thinks your running. Check dictionary view V$NLS_PARAMETERS.
This gets worse with date/time values. Date/time values can go all over the map - mostly because of time zone. What time zone is your database server in? What time zone does it think you're running from? And if that doesn't spin your head quite enough, check out what happens if you change Oracle's default calendar from Gregorian to Thai Buddha.
I strongly suggest you get rid of the numbers ENTIRELY.
To create date or date time values, use strings with completely invariant and unambiguous formats. Then assign, compare and calculate date values exclusively, e.g.:
GOODFMT constant VARCHAR2 = 'YYYY-MM-DD HH24:MI:SS.FFF ZZZ'
Good_Time DATE = TO_DATE ('2012-02-17 08:07:55.000 EST', GOODFMT);