Trouble with convert integer to date+time in Oracle - sql

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

Related

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.

How do you convert YYYY_MM to date in Oracle SQL?

I have a column which has dates as texts. Like: '2021_01' (I will reefer this column as TextDate)
I convert it to '2021-01' with this code:
SELECT REPLACE(at.TextDate,'_','-') as DataFormat FROM tableName at
But when I try to cast it to date, or convert it to date, I always get 'Missing expression' errors. I tried this:
SELECT REPLACE(CONVERT(VARCHAR(7), at.TextDate, 126, '_', '-') as date FROM tableName at
But it will give me errors. Any suggestion?
convert means something completely different in Oracle than it does elsewhere. You need the to_date() function:
SELECT TO_DATE(at.textDate, 'YYYY_MM') as DataFormat FROM tableName at
If you want to display it in a particular format then you can either let your client/application do that - most clients by default will use your session's NLS_DATE_FORMAT setting - or explicitly convert it back to a string with the complementary to_char() function.
db<>fiddle
The valid date elements are also in the documentation. You should only convert to a string for display though; while you are manipulating or storing it you should treat it as a date.
How can I filter last 3 months with it?
You need to use Oracle syntax, not SQL Server or other syntax. You also can't refer to a column alias in the same level of query. SO you can recalculate the date value; or as your string format is relatively sane you can convert the target date to a string and compare that, which might allow an index to be used. Something like:
SELECT TO_DATE(at.textDate, 'YYYY_MM') as DataFormat
FROM tableName at
WHERE at.textDate >= TO_CHAR(ADD_MONTHS(TRUNC(SYSDATE, 'MM'), -3), 'YYYY_MM')
db<>fiddle
TO_DATE with appropriate format mask.
Just to know what's what:
SQL> alter session set nls_date_format = 'dd.mm.yyyy hh24:mi:ss';
Session altered.
Code:
SQL> select to_date('2021-01', 'yyyy_mm') from dual;
TO_DATE('2021-01','
-------------------
01.01.2021 00:00:00
SQL>

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.

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

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;

Error when using TO_DATE in SQL

I understand the basic TO_DATE usage in Oracle SQL. i google and found some guides to use TO_DATE which is to convert julien date to normal date.
the basic working code is :
SELECT TO_CHAR((TO_DATE(2016365, 'YYYYDDD'))) FROM DUAL)
However, i want to convert date that is in a column which has thousands of them.
What i did was :
SELECT TO_CHAR((TO_DATE(PREVDT, 'YYYYDDD'))) FROM DUAL)
The changes is PREVDT because all my julian date is in PREVDT column. However, im getting invalid identifier.....can anyone help me?
I also tried this but no luck:
TO_CHAR((TO_DATE(PREVDT, 'YYYYDDD')))
You need to provide the table name in which your column PREVDT is present.
select TO_CHAR((TO_DATE(PREVDT, 'YYYYDDD'))) FROM yourTable
Oracle DUAL table does not have the column PREVDT. What you want to do is run the query against your table, the one with column PREVDT.
SELECT TO_CHAR((TO_DATE(PREVDT, 'YYYYDDD'))) FROM your_table_name;
The PREVDT values should be in the specified date format otherwise you will get an error.
Read more about the DUAL table here.