I have this Timestamp field which looks like this:
2015-08-24 16:24:28.763915
and I want to get only the date and insert to date field
I tried this:
select
TO_DATE(CAST (CON1.AF_UPDATE_DT AS VARCHAR(10)), DD/MM/YYYY)
FROM AF_EMR_MEM_CONT CON1
but i get this error
00904. 00000 - "%s: invalid identifier"
If I try to do this sql:
select AF_UPDATE_DT
TO_DATE(CAST (CON1.AF_UPDATE_DT AS VARCHAR(10)), YYYY/MM/DD)
FROM AF_EMR_MEM_CONT CON1
I get the error:
00923. 00000 - "FROM keyword not found where expected"
You can just truncate the TIMESTAMP, result of the TRUNC function is DATE:
SELECT TRUNC(LOCALTIMESTAMP) FROM DUAL;
You can use SUBSTR to extract the date portion from the string, and then TO_DATE to modify it to a date object:
select TO_DATE(
SUBSTR('2015-08-24 16:24:28.763915', 1, 10), 'YYYY-MM-DD') -- 2015-08-24
from DUAL
See this demo
Related
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 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
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';
I want to remove millisecond from 21-02-14 10:41:08.000000000 PM.
I try
select to_date('21-02-14 10:41:08.000000000 PM','DD/MM/YY HH:MI:SS:SSSSS PM') from dual;
error:
Error starting at line : 1 in command -
select to_date('21-02-14 10:41:08.000000000 PM','DD/MM/YY HH:MI:SS:PM') from dual
Error report -
SQL Error: ORA-01855: AM/A.M. or PM/P.M. required
01855. 00000 - "AM/A.M. or PM/P.M. required"
*Cause:
*Action:
You're specifying fractional seconds, but your format has "seconds past midnight" - which doesn't have 9 digits. You're also specifying a colon in your format string, when your data has a dot. Try:
select cast(to_timestamp('21-02-14 10:41:08.000000000 PM','DD-MM-YY HH:MI:SS.FF9 PM') as date) from dual;
My query is
select TO_CHAR('03-JAN-2013', 'D') from dual;
but an error occured as
ORA-01722: invalid number
01722. 00000 - "invalid number"
*Cause:
*Action:
But when query changed as select TO_CHAR(sysdate, 'D') from dual;
Result is right answer 5.
I can't understand why it is behaving like this, please help me.
Thanks in advance
Please cast the string to date before selecting.
SELECT TO_CHAR(CAST('03-JAN-2013' AS DATE), 'D') FROM DUAL;
OR
SELECT TO_CHAR(TO_DATE('03-JAN-2013'), 'D') FROM DUAL;
The '03-JAN-2013' string literal must be converted to the date data type before invoking TO_CHAR function:
select TO_CHAR(to_date('03-JAN-2013', 'dd-MON-YYYY'), 'D') as res
from dual
RES
-----
5