I would like to know how to get the difference between two date times. The issue I am facing is that I have to convert the date from the table, to a usable date. i.e. 7841433540 converts to 09/10/14 09:19:00. My sql to return those values is:
SELECT ddt.tochar(ENC_START_DDT,'MM/DD/YY HH24:MI:SS') "Admit",
TO_CHAR(SYSTIMESTAMP,'MM/DD/YY HH24:MI:SS') "Today"
from CCDBA.PATIENT where CCDBA.PATIENT.PAT_SEQ = '101067048';
Now I thought I could...
SELECT ddt.tochar(ENC_START_DDT,'MM/DD/YY HH24:MI:SS')-TO_CHAR(SYSTIMESTAMP,'MM/DD/YY HH24:MI:SS')
from CCDBA.PATIENT where CCDBA.PATIENT.PAT_SEQ = '101067048';
But that returns:
ORA-01722: invalid number
01722. 00000 - "invalid number"
*Cause:
*Action:
Any help is greatly appreciated
You cannot subtract strings and expect the database to understand them as dates. So, convert them to dates:
select (to_date(ddt.tochar(ENC_START_DDT,'MM/DD/YY HH24:MI:SS'), 'MM/DD/YY HH24:MI:SS') -
sysdate) as diff
from CCDBA.PATIENT
where CCDBA.PATIENT.PAT_SEQ = '101067048';
Related
sql request is:
select DRCR_CR_DT
from PS_DRCR
where
TRUNC(DRCR_CR_DT)= TO_DATE('1/4/2022','dd/mon/yyyy');
the type of DRCR_CR_DT is: DATE
DRCR_CR_DT contain something like this : 1/4/2022 2:02:54 PM
thank you for the answer
The error means that DRCR_CR_DT is a string, not a real date - so its data type is VARCHAR2 not DATE as you said.
create table PS_DRCR (DRCR_CR_DT) AS
select '1/4/2022 2:02:54 PM' from dual;
select DRCR_CR_DT
from PS_DRCR
where
TRUNC(DRCR_CR_DT)= TO_DATE('1/4/2022','dd/mon/yyyy');
ORA-00932: inconsistent datatypes: expected NUMBER got DATE
If it was really a date it would work - at least, with the format model fixed as #Littlefoot pointed out.
If you are stuck with it as a string (which is a bad data model) then you need to convert it to a date before truncating:
select DRCR_CR_DT
from PS_DRCR
where
TRUNC(TO_DATE(DRCR_CR_DT, 'DD/MM/YYYY HH:MI:SS AM')) = TO_DATE('1/4/2022','dd/mm/yyyy');
DRCR_CR_DT
-------------------
1/4/2022 2:02:54 PM
You can also compare with a date literal:
select DRCR_CR_DT
from PS_DRCR
where
TRUNC(TO_DATE(DRCR_CR_DT, 'DD/MM/YYYY HH:MI:SS AM')) = DATE '2022-04-01';
db<>fiddle
The reason you get ORA-00932 is that before it looks at the actual values involved the parser sees something like:
where TRUNC(<some string>) = TO_DATE(<some string>)
The numeric form of TRUNC "takes as an argument any numeric data type or any nonnumeric data type that can be implicitly converted to a numeric data type", while the date form only takes a "datetime data type". That means TRUNC(<some string>) has to be interpreted as a number.
So without looking at either string to be converted, it knows that translates to:
where <some number> = <some date>
It's doing that analysis before it does any actual conversions, so it doesn't get as far as trying to convert the date string; when it does get that far that will throw ORA-01843.
I'd expect something different; if date you provide is 1/4/2022, then there's no mon format model in it:
SQL> select to_date('1/4/2022', 'dd/mon/yyyy') from dual;
select to_date('1/4/2022', 'dd/mon/yyyy') from dual
*
ERROR at line 1:
ORA-01843: not a valid month
When fixed:
SQL> select to_date('1/4/2022', 'dd/mm/yyyy') from dual;
TO_DATE('1
----------
01/04/2022
SQL>
If that's not it, please, provide sample data (CREATE TABLE and INSERT INTO statements).
[EDIT]
This is what you were supposed to provide; now I had to do it, based on your comment:
DRCR_CR_DT is a varchar2 the containt of DRCR_CR_DT is 01/04/2022 00:00:00
Therefore:
SQL> create table ps_drcr (drcr_cr_dt varchar2(20));
Table created.
SQL> insert into ps_drcr values ('01/04/2022 00:00:00');
1 row created.
Query: TO_DATE's format model must match data format. If table contains 00:00:00, then format model is hh24:mi:ss, not hh:mi:ss am you used.
In that case, query works for this particular value.
SQL> select *
2 from ps_drcr
3 where TRUNC(TO_DATE(DRCR_CR_DT, 'DD/MM/YYYY HH24:MI:SS')) =
4 TO_DATE('1/4/2022','dd/mm/yyyy');
DRCR_CR_DT
--------------------
01/04/2022 00:00:00
SQL>
Note that it is always a really bad idea to store date datatype values as strings (into varchar2 columns) because nothing prevents you from storing e.g. ab/&4/20xz 0a:f8:fi into a varchar2 column, and that certainly isn't valid date value. If there is at least one row in the table whose drcr_cr_dt doesn't represent valid date, query will fail.
I have a table containing a column in date format named "date_started". I want to substract all the dates from this column from a fixed date, for example 31.03.2022, resulting in a new column "absolut days" showing difference between two dates in days.
I tried with the following statement:
SELECT ('31.03.2022'- date_started) AS absolut days
FROM ....
Unfortunately i am not able to find a workaround for the resulting Error message:
ORA-00932: Inkonsistente Datentypen: CHAR erwartet, DATE erhalten
00932. 00000 - "inconsistent datatypes: expected %s got %s"
I am beginner in SQL, exspecially in Oracle and looking forward for some help, thx!
'31.03.2022' may look like a date but it is not a DATE data type; it is a string literal.
If you want a DATE data type then you can use a date literal:
SELECT DATE '2022-03-31' - date_started AS absolut_days
FROM your_table;
Or convert your string to a date:
SELECT TO_DATE('31.03.2022', 'DD.MM.YYYY') - date_started AS absolut_days
FROM your_table;
Then, for the sample data:
CREATE TABLE your_table (date_started) AS
SELECT DATE '2022-01-01' FROM DUAL;
Both output:
ABSOLUT_DAYS
89
db<>fiddle here
I have a table where the date_one field is of type VARCHAR2 (20 BYTE) and the date_two field is of type DATE.
Output for date_one is 29-01-2019 20:22:08 and output for date_two is 25-JAN-19.
I need to calculate records based on an interval of days between these two fields but I'm having a hard time with this error:
select field1,field2,field3
from mytable
where
trunc(to_date(DATE_ONE, 'DD-MM-YYYY HH24:MI:SS')) - trunc(to_date(DATE_TWO 'DD-MM-YYYY')) > interval '15' day;
ORA-00932: inconsistent datatypes: expected NUMBER, got INTERVAL DAY
TO SECOND
00932. 00000 - "inconsistent datatypes: expected %s got %s"
How can I fix this error?
The result of subtracting two dates is a number of days, unlike the result of subtracting two timestamps, which is an interval. You are currently trying to compare the number you get from that subtraction with a fixed interval - which is what the error is saying. The data types don't match and can't be implicitly converted for comparison.
You can either convert that number to an interval:
select field1,field2,field3
from mytable
where (trunc(to_date(DATE_ONE, 'DD-MM-YYYY HH24:MI:SS')) - trunc(DATE_TWO)) * interval '1' day > interval '15' day;
or more simply just compare with the number 15:
select field1,field2,field3
from mytable
where trunc(to_date(DATE_ONE, 'DD-MM-YYYY HH24:MI:SS')) - trunc(DATE_TWO) > 15;
db<>fiddle
I've changed trunc(to_date(DATE_TWO, 'DD-MM-YYYY')) to simply trunc(DATE_TWO). Because DATE_TWO is already a date, when you do to_date(DATE_TWO, 'DD-MM-YYYY') you are implicitly converting the date to a string using your session's NLS_DATE_FORMAT setting (which seems to be DD-MON-RR), and then converting that string back to a date. As well as being a waste of effort, that may break if this is run in a session with different settings. You just need to truncate the date you already have.
I have an error while running this query :
select a.date, e.date, x.rlv, e.rlv
from data_1 a
inner join data_2 e on e.date = (select to_char(to_date(x.date,'yyyymmdd','yyyymmdd')
- INTERVAL '3' month,'yyyymmdd') from data_2 x where x.rlv=e.rlv)
;
I got this error :
12702. 00000 - "invalid NLS parameter string used in SQL function"
*Cause: An unknown parameter name or invalid value is specified in a NLS parameter string
Do you have any ideas ? thanks in advance
Main problem with it is - as #sticky bit commented - that you're dealing with strings that represent date values. Wrong choice, switch to DATE datatype if possible.
Other than that, your code doesn't work because you misused syntax. Here's how (I modified column name from date - which is reserved for datatype - to col):
Your: to_char(to_date(col, 'yyyymmdd', 'yyyymmdd') - interval '3' month, 'yyyymmdd')
Mine: to_char(to_date(col, 'yyyymmdd') - interval '3' month, 'yyyymmdd')
Compare and see the difference.
Because, if you "fix" it, interval works just fine so, if you want, you can switch back to it from add_months:
SQL> with test (col) as
2 (select '20210622' from dual) --> today's date,
3 select to_char(to_date(col, 'yyyymmdd') - interval '3' month, 'yyyymmdd') result
4 from test;
RESULT
--------
20210322 --> 3 months earlier
SQL>
Hello thanks for your answer. I find the solution by using add_monts : (select to_char(add_months(to_date(x.date,'yyyymmdd'),-3), 'yyyymmdd') not sur its highly optimised but it works at least
Can someone tell me why is this SQL query returning a
ORA-01843. 00000 - "not a valid month"
I now it is wrong but it should be because 2018 is not a valid day. 09 is a valid month. I think..
select to_timestamp('2018-09-05 11:35:41', 'dd/MM/yyyy HH:mi:ss') from dual;
I know that the query is wrong. I just want to know why it isn't saying not a valid day or something like that. the error is now saying that the month is wrong which is false.
The reason is because Oracle is trying to be clever/helpful. So, it is interpreting:
select to_timestamp('2018-09-05 11:35:41', 'dd/MM/yyyy HH:mi:ss') from dual;
---------------------^ddMM
The 20 is interpreted as a valid day. The month then follows.
Oracle is helpfully trying to ignore the separator. Hence, the 18 is an invalid month.
Try this:
select to_timestamp('2012-2012', 'dd/MM/yyyy') from dual
If you "insist" to get an error message related to day then try the FX modifier:
SELECT TO_TIMESTAMP('2018-09-05 11:35:41', 'FXdd/MM/yyyy HH:mi:ss') FROM dual;
Error at line 1
ORA-01861: literal does not match format string
I assume that is the error message you would expect.
Or try a "valid" month, e.g.
SELECT TO_TIMESTAMP('2008-09-05 11:35:41', 'dd/MM/yyyy HH:mi:ss') FROM dual;
Error at line 1
ORA-01830: date format picture ends before converting entire input string