ORA-01861: literal does not match format string in minus query - sql

I tried executing the following query:
select to_date(SUBJ_VST_FRM_VST_DT,'dd-MON-YY')
from SMARTTRIAL_ODR_LANDING.RAVE_LND_SUBJ_VST_FRM
minus
select SUBJ_VST_FRM_VST_DT
from SMARTTRIAL_ODR_STAGE.LS_STG_SUBJ_VST_FRM;
but the following error is returned: ORA-01861: literal does not match format string
What could the problem be?

Looking at your sample data it seems that your nvarchar column contains date data in the following format (YYYY-MM-DD hh24:mi:ss).
The following query works for me.
SELECT TO_DATE('2000-01-03 00:00:00','YYYY-MM-DD hh24:mi:ss') FROM DUAL;
So I am hopeful that the following query should work for you.
SELECT TO_DATE(subj_vst_frm_vst_dt,'YYYY-MM-DD hh24:mi:ss')
FROM smarttrial_odr_landing.rave_lnd_subj_vst_frm
MINUS
SELECT subj_vst_frm_vst_dt
FROM smarttrial_odr_stage.ls_stg_subj_vst_frm;
However, the above query will also contain the timestamp. If the column subj_vst_frm_vst_dt in the ls_stg_subj_vst_frm table does not contain a timestamp or if you dont want to compare the timestamp you can try the following query.
SELECT TRUNC(TO_DATE(subj_vst_frm_vst_dt,'YYYY-MM-DD hh24:mi:ss'))
FROM smarttrial_odr_landing.rave_lnd_subj_vst_frm
MINUS
SELECT TRUNC(subj_vst_frm_vst_dt)
FROM smarttrial_odr_stage.ls_stg_subj_vst_frm;

Use below query. let me know if it gives correct output
select NVL(to_date(SUBJ_VST_FRM_VST_DT,'DD-MON-YY') ,SYSDATE)
from SMARTTRIAL_ODR_LANDING.RAVE_LND_SUBJ_VST_FRM
minus
select to_date(NVL(SUBJ_VST_FRM_VST_DT,SYSDATE),'DD-MON-YY')
from SMARTTRIAL_ODR_STAGE.LS_STG_SUBJ_VST_FRM;

Related

Trouble with convert integer to date+time in Oracle

i have in table one column in integer. This integer looks like:
2204010044 - it is YYMMDDHH24MI.
I want this column in sql query convert to date. But when i try, i get error:
ORA-01840.
I testing this: TO_DATE("mycolumn",'yymmddhh24mi')
I've tried multiple options, but always to no avail.
NLS_DATE_FORMAT is for database: DD.MM.YY (i dont know,if its relevant)
Maybe with something like this:
select to_timestamp(cast(2204010044 as varchar(10)),'YYMMDDHH24MI')
from dual
If you want to keep the time, you must cast to timestamp, not date.
If you want just the date, use:
select to_date(2204010044,'YYMMDDHH24MI')
from dual
You can test on this db<>fiddle
Try the following SQL query:
select TO_DATE(2204010044,'yymmdd hh24:MI:ss')
FROM dual
dbfiddle

How to parse varchar to actual time value?

I am trying to insert the data into the final table in snowflake from the staging table. When the command is run it give the error:
Can't parse '20211101132344205550' as timestamp with format 'YYYYMMDD HH24:MI:SS.FF'
My table definition and insert statement is here.
I used the same kind of method last time it worked. Thank you so much in advance.
CREATE OR REPLACE TABLE dw.tb_fidctp_order(
sysdate DATE,
record_id NUMBER(18,0) ,
timestamp TIMESTAMP_NTZ(9),
<trim_excess>
);
INSERT INTO dw.tb_fidctp_order(
sysdate,
record_id,
timestamp,
<trim_excess>
)
SELECT
TO_DATE(LEFT(timestamp, 8), 'YYYYMMDD')
,CAST(record_id AS NUMBER(18,0))
,TO_TIMESTAMP(LEFT(timestamp,24),'YYYYMMDD HH24:MI:SS.FF')
<trim_excess>
FROM stg.tb_fidctp_order_input;
In Snowflake you need to define what your format is. So, if all of your timestamps are formatted as a straight string like that, then you are defining it incorrectly. Try something more like this:
SELECT to_timestamp(left(EXPIRY_DATETIME,24),'YYYYMMDDHH24MISSFF');
The to_timestamp() function is defining how the input string is formatted, not how you want the timestamp to be formatted as an output.
So the error message is the critical point, your formating string for timestamps have space and : time formatting, which needs to be removed.
Below I have used the tru_to_timestamp function because it returns NULL istead of erroring with is helpful to show the problem is your formatting:
SELECT
'20211101132344205550' AS a
,try_to_timestamp(a, 'YYYYMMDD HH24:MI:SS.FF') as b
,try_to_timestamp(a, 'YYYYMMDDHH24MISSFF') as c;
gives:
A
B
C
20211101132344205550
2021-11-01 13:23:44.205
which shows your ,TO_TIMESTAMP(LEFT(timestamp,24),'YYYYMMDD HH24:MI:SS.FF')
should be:
,TO_TIMESTAMP(LEFT(timestamp,24),'YYYYMMDDHH24MISSFF')
and then you would have no problem.

Oracle: select with date range filter throws invalid number error

Select
*
from
view
where
datecolumn = to_date('2019-02-26 12:11:23','YYYY-MM-DD HH24:MI:SS')
returns records but
Select
*
from
view
where
datecolumn > to_date('2019-02-26 12:11:23','YYYY-MM-DD HH24:MI:SS')
throws invalid number error.
But not always only fails for few dates.
Please let me know if I am missing anything.
What is the data type of column datecolumn ?
I guess your table column datecolumn is varchar type and having date stored in different format.
As JonHeller said the issue is with type conversion went wrong in some other column.

SQL SERVER - Date conversion failed

I have a table in which AccessDate column is of type datetime.
And I am trying to do something like this:
SELECT 'AccessDate' UNION ALL SELECT AccessDate FROM table_name
I am trying to insert the header of the table "AccessDate" into the result of the query.
And this error shows up after executing:
Conversion failed when converting date and/or time from character string.
Can someone help me with this? Thank you.
Not surprising. You appear to want a date in a column where there is already a character. You need to convert the date to a string:
SELECT 'AccessDate' UNION ALL
SELECT CONVERT(VARCHAR(10), AccessDate, 121)
FROM table_name;
You can use whatever format you like. I prefer YYYY-MM-DD.

Comparing date with sysdate in oracle

I have a column which is of "DATE" type and I want to run a query on it comparing it with sysdate.
But I am getting following error, Can someone please let me know what I am missing here?
SQL> select distinct file_name as r
from table_1
where view_day >= TO_DATE(SYSDATE-10, 'YYYY/MM/DD');
ERROR at line 1:
ORA-01858: a non-numeric character was found where a numeric was expected
You shouldn't use to_date on a date, To_date is for casting a varchar to date, not a date.
If you do use the function to_date on a date, then oracle will refer to it as a string according to nls_date_format which may vary in different environments.
As #jonearles said, if you want to remove the time in sysdate then use TRUNC
USE:
select distinct file_name as r
from table_1
where view_day >= TRUNC(SYSDATE-10)
Error shows that a VIEW_DAY column is varchar so you need to convert DATE to String. Use TO_CHAR or convert VIEW_DAY to date type.