Remove milli second from timstamp in oracle sql - sql

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;

Related

Why is Oracle returning a Not Valid Month error

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

Getting date time stamp difference minutes in Oracle Database 12c Enterprise Edition

I have this query to get the date time stamp difference minutes in Oracle Database 12c Enterprise Edition Release 12.1.0.1.0 - 64bit Production
SELECT TO_CHAR(GPS_FULL_DATE+2/24, 'MM-DD-YYYY HH24:MI:SS') GPS_DATE,
TO_CHAR(CREATION_DATE, 'MM-DD-YYYY HH24:MI:SS') CREATION_DATE,
extract(minute from ((GPS_FULL_DATE+2/24)-CREATION_DATE)) mins
FROM server_data sd
But I got this error:
ORA-30076: invalid extract field for extract source
30076. 00000 - "invalid extract field for extract source"
*Cause: The extract source does not contain the specified extract field.
*Action:
Error at Line: 2 Column: 42
(GPS_FULL_DATE + 2/24) - CREATION_DATE
returns number of days between two dates. Therefore, you can't extract number of minutes of it - you could, though, multiply the result by 24 * 60 (hours in a day * minutes in an hour) to get the desired value.

casting timestamp as date oracle

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

Find difference between two dates Oracle SQL

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';

oracle sql query giving error

Following query is giving error:
SELECT to_char(last_day(add_months(to_char(to_date('01-02-2013','dd-mm-yyyy'),
'dd-MON-yyyy'),-1)) + 1,'dd-mm-yyyy') FROM dual;
ORA-01858: a non-numeric character was found where a numeric was expected
I tried this on two systems:
with NLS_DATE_FORMAT='DD-MON-RR' - this query works fine.
With NLS_DATE_FORMAT='MM-DD-YYYY' - gives me error ORA-01858: a non-numeric character was found where a numeric was expected.
Any clues as to why this query is failing? I can't have the queries be dependent on the DATE format.
Why are you doing a to_char when calling add_months , you need to pass a date like
SELECT to_char(last_day(add_months(to_date('01-02-2013','dd-mm-yyyy'),
,-1)) + 1,'dd-mm-yyyy') FROM dual;
You have an implicit char-to-date conversion, in the add_months() call; the argument you're passing is a string, not a date. The to_char() you have inside that is redundant, and causing the error when your NLS_DATE_FORMAT doesn't match the format you're using in that to_char():
SELECT to_char(last_day(add_months(to_date('01-02-2013','dd-mm-yyyy'),-1)) + 1,
'dd-mm-yyyy') FROM dual;
I'm not entirely sure what you're doing though... if you want the first day of the month that date is in, you can do this:
SELECT to_char(trunc(to_date('01-02-2013', 'dd-mm-yyyy'), 'MM'),
'dd-mm-yyyy') FROM dual;
This uses the TRUNC(date) function to effectively round down to the start of the month.