Oracle: How to convert the following date to a different format: - oracle

in my select query i have the following
substr(to_date(NEXT_ARRIVAL_DATE, 'yyyy-mm-dd'), 0, 10)
Which yields:
08-JUN-11
What i need is it to yield:
2011-06-08
EDIT:
The data was coming in wrong. sorry. The below workds fine
to_char(to_date(NEXT_ARRIVAL_DATE, 'mm/dd/yyyy'), 'YYYY-MM-DD') || ' 00:000:00'

Your DATE_NEXT_ARRIVAL column obviously has a date datatype.
SQL> create table t23 (next_arrival_date date)
2 /
Table created.
SQL> insert into t23 values (sysdate+7)
2 /
1 row created.
SQL> select to_date(next_arrival_date, 'YYYY-MM-DD')
2 from t23
3 /
TO_DATE(N
---------
11-JUN-08
SQL>
If you want to display the date in a different format you need to use TO_CHAR() i.e. convert it to a string:
SQL> select to_char(next_arrival_date, 'YYYY-MM-DD')
2 from t23
3 /
TO_CHAR(NE
----------
2011-06-08
SQL>
If you have to do this for a whole bunch of dates, you might want to change the session settings instead....
SQL> alter session set nls_date_format='YYYY-MM-DD'
2 /
Session altered.
SQL> select sysdate, next_arrival_date
2 from t23
3 /
SYSDATE NEXT_ARRIV
---------- ----------
2011-06-01 2011-06-08
SQL>

in Oracle you can convert a DATE column to string with
to_char(NEXT_ARRIVAL_DATE, 'yyyy-mm-dd')
but it looks like the value of NEXT_ARRIVAL_DATE is a string in the required format.
so you can just do select NEXT_ARRIVAL_DATE from ...

Related

Charts in Oracle Apex

Hi everyone I wanna ask u about how I can bring data last 24 hours into bar charts, is there any methods to make it please
I have this table without data
datetime
clientchannel
servicename
service_count
13_02_2022 9:35
*****
notification
2
It is a WHERE clause you need, I presume. Something like this:
select ...
from your_table
where datetime >= sysdate - 1;
Why? Because - when you subtract a number from DATE datatype value in Oracle - it subtracts that many days.
SQL> alter session set nls_date_format = 'dd.mm.yyyy hh24:mi:ss';
Session altered.
SQL> select sysdate right_now,
2 sysdate - 1 yesterday
3 from dual;
RIGHT_NOW YESTERDAY
------------------- -------------------
13.02.2022 11:01:34 12.02.2022 11:01:34
SQL>
If you store date values as strings (which means that DATETIME column is declared as e.g. VARCHAR2(20), and that's really bad idea), then you first have to convert it to a valid date datatype value - use TO_DATE function with appropriate format mask:
where to_date(datetime, 'dd_mm_yyyy hh24:mi') >= sysdate - 1
[EDIT] If you want to go 60 minutes back, then subtract that many minutes:
SQL> select sysdate right_now,
2 sysdate - interval '60' minute an_hour_ago
3 from dual;
RIGHT_NOW AN_HOUR_AGO
------------------- -------------------
14.02.2022 07:09:30 14.02.2022 06:09:30
SQL>

string to timestamp conversion plsql

I've inherited some data from an external source which is a timestamp. This was put into warehouse by someone as a varchar2. I need to convert this to a legitimate timestamp but am unsure how. This is how the string looks. "2021-04-23T11:02:17.00Z".
Would appreciate some help.
PS Ideally, I'd also like to know how to trunc this to a more traditional date format of DD-MMM-YYYY e.g. 21-Jan-2021 or even DD-MM-YYYY is fine.
Use to_timestamp_tz() to get the corresponding timstamp with time zone, convert it to the timezone you want it in (for example sessiontimezone) with AT TIME ZONE and cast() that to a timestamp.
SELECT cast(to_timestamp_tz('2021-04-23T11:02:17.00Z', 'YYYY-MM-DD"T"HH24:MI:SS.FF2:TZR') AT TIME ZONE sessiontimezone AS timestamp)
FROM dual;
db<>fiddle
Shouldn't be too difficult. Extract the "date" part, apply TO_DATE function to it (with appropriate format mask) and - that's all. It means that you should "stop" at the date_value in the following query. The last, final_result is a string again, just formatted as you wanted.
SQL> with test (col) as
2 (select '2021-04-23T11:02:17.00Z' from dual)
3 select substr(col, 1, 10) string,
4 --
5 to_date(substr(col, 1, 10), 'yyyy-mm-dd') date_value,
6 --
7 to_char(to_date(substr(col, 1, 10), 'yyyy-mm-dd'), 'dd-mm-yyyy') final_result
8 from test;
STRING DATE_VALUE FINAL_RESU
---------- ---------- ----------
2021-04-23 2021-04-23 23-04-2021
SQL>
In order to avoid that "operation", you might even create a view. For example:
This is a table you currently have:
SQL> create table test as
2 (select 1 id, 'Littlefoot' name, '2021-04-23T11:02:17.00Z' col from dual);
Table created.
Create a view, re-using code I posted above:
SQL> create or replace view v_test as
2 select id, name,
3 to_date(substr(col, 1, 10), 'yyyy-mm-dd') col
4 from test;
View created.
Select from it:
SQL> select * from v_test;
ID NAME COL
---------- ---------- ----------
1 Littlefoot 2021-04-23
Want another format? No problem:
SQL> alter session set nls_date_format = 'dd-mon-yyyy';
Session altered.
SQL> select * from v_test;
ID NAME COL
---------- ---------- -----------
1 Littlefoot 23-apr-2021
SQL>
Or, apply TO_CHAR to view's col column (also demonstrated in code I posted first; see the final_result).
You can use the TO_TIMESTAMP function to do this.
Try:
TO_TIMESTAMP('2021-04-23T11:02:17.00Z', 'YYYY-MM-DDTHH:MI:SS')
You can read more about this function in their Documentation

Insert Birthdate with only YY format for period before 2000 Oracle DB

I have afile where i recieve Birthdates and insert them into my Database.
the format is like the following
03-JUN-52
I use the following script to insert the date
update data."PersonBDates" set BIRTHDATE = to_date('13-SEP-47', 'DD-MON-YY');
and i also used
update data."PersonBDates" set BIRTHDATE = to_date('13-SEP-47', 'DD-MON-RR');
but when i check if find it 2074 not 1947.
How to insert this date into my oracle database?
Generally speaking, RR should work, but - not in all cases. You'll have to fix data first because RR will return different values:
for years from 00 to 49 you'll get this century, 20xx, while
50 to 99 will return previous century, 19xx
Here's an example:
SQL> alter session set nls_date_format = 'dd.mm.yyyy';
Session altered.
SQL> select
2 to_date('03-07-52', 'dd-mm-rr') rr1,
3 to_date('03-07-52', 'dd-mm-yy') yy1 ,
4 --
5 to_date('03-07-47', 'dd-mm-rr') rr2,
6 to_date('03-07-47', 'dd-mm-yy') yy2
7 from dual;
RR1 YY1 RR2 YY2
---------- ---------- ---------- ----------
03.07.1952 03.07.2052 03.07.2047 03.07.2047
SQL>
As you can see, both RR and YY format mask for year 47 return 2047.
What to do? Concatenate 19 to all years, e.g.
SQL> with test (col) as
2 (select '03-07-52' from dual union all
3 select '03-07-47' from dual
4 )
5 select col,
6 to_date(substr(col, 1, 6) || '19' || substr(col, -2), 'dd-mm-rrrr') result
7 ---------------- ---------------
8 -- this is "03-07-" "19" the last 2 digits
9 --
10 from test;
COL RESULT
-------- ----------
03-07-52 03.07.1952
03-07-47 03.07.1947
SQL>
[EDIT]
If your current inserting script works OK - which I doubt, regarding error code you mentioned in a comment:
ORA-01858: a non-numeric character was found where a numeric was expected
which means that not all input data have the same, expected & correct format of DD-MON-YY, then a simple way to fix birthdates might be this:
subtract 100 years from all dates whose year is larger than 2000
Here's how:
SQL> create table test (birthdate date);
Table created.
SQL> insert into test
2 select to_date('03-07-52', 'dd-mm-rr') from dual union all
3 select to_date('03-07-47', 'dd-mm-rr') from dual;
2 rows created.
SQL> select * from test;
BIRTHDATE
----------
03.07.1952
03.07.2047
SQL> update test set
2 birthdate = add_months(birthdate, -100 * 12)
3 where extract (year from birthdate) > 2000;
1 row updated.
SQL> select * from test;
BIRTHDATE
----------
03.07.1952
03.07.1947
SQL>
You can modify that, of course, if there's someone who actually was born in 2000 or later.
As of error you got (ORA-01858), well, fixing it depends on how exactly you're entering those values into a table.
if it was a SQL*Loader, invalid values would be rejected and stored into the .bad file and you could fix them and reprocess them later
if it was using an external tables, you could use a where clause and omit invalid rows; for example, use regexp_like
Or, your best option is to make sure that all input values are valid dates. Then any option you choose (I mentioned previously) would work without ORA-xxxxx errors.
Alternate way of concatenating 19 to all years, as Littlefoot suggested.
to_date(regexp_replace('13-SEP-47', '([0-9]+$)', '19\1'), 'DD-MON-YYYY')
I would suggest to implement the solution where 01 is not considered as 1901 but 2001 or something similar (I assume that birthday year is not 1901 for any person in your system).
Case when substr(col, -2) < to_char(sysdate,'YY')
then to_date(col, 'DD-MON-YY')
else to_date(substr(col, 1, 6) || '19' || substr(col, -2), 'dd-mm-rrrr'
End
Cheers!!

Getting data from different tables between dates when each table has it on date modified

Like to get some views from you all, regarding one scenario i'm struggling with currently. Below is a problem statement
I have Table A , B , C
A has below column
user|modified date| wokred_on A | ..some more related to user operation
B has columns
user | modified date | worked on B | ..some other columns
C has columsn
user | modified date | worked on C| ..some other columns
these tables are not have any direct relation except then user.
we have to pull data from these tables for a user between given dates with the count op action or work he has done between a given date range?
my struggle here is these each table has it's own date modified if a date range selected which is not in other column but still i need to pull the data as user has worked on it in between dates.
can it be possible to select these dates and have the in one column so that one can put that in where clause and having outer joins to pull other records ?
Sorry for this big problem statement. any suggestions are very much appreciated
Below is a use case.just extending the assumption given by littlefoot
First, test case:
SQL> create table a (cuser varchar2(10), modified_date date,action );
varchar2 (10) )
Table created.
SQL> create table b (
Table created.cuser varchar2(10), modified_date date,action
varchar2 (10) );
SQL> create table c (cuser varchar2(10), modified_date date,action
varchar2 (10) ));
Table created.
SQL> insert into a values ('lf', date '2018-05-01', 'issue raised');
1 row created.
SQL> insert into a values ('mc', date '2018-05-01', 'issue raised ');
1 row created.
SQL> insert into b values ('lf', date '2018-05-01',issue raised');
1 row created.
SQL> insert into b values ('lf', date '2018-05-01','issue resolved');
1 row created.
SQL> insert into c values ('if', date '2018-05-28',' issue resolved');
1 row created.
SQL> insert into c values ('mc', date '2018-05-13','issue raised');
1 row created.
SQL> insert into c values ('mc', date '2018-05-13','issue resolved');
1 row created.
SQL> alter session set nls_date_format = 'yyyy-mm-dd';
Session altered.
SQL> select * from a;
CUSER MODIFIED_D. ACTION
---------- ----------
lf 2018-05-01 issue raised
mc 2018-05-01 issue raised
SQL> select * from b;
CUSER MODIFIED_D ACTION
---------- ----------. ______________
lf 2018-05-01 issue raised
lf 2018-05-01. issue resolve
SQL> select * from c;
CUSER MODIFIED_D. ACTION
---------- ----------
If 2018-05-28. issue resolve
mc 2018-05-13. issue raised
mc 2018-05-13. issue resolve
CUSER DATE CNT_ISSUE_RAISED CNT_ISSUE_RESOLVED
------ ------- --------------- -------------------
if 2018-05-01 2 1
lf 2018-05-28 0 1
mc 2018-05-01 0 1
mc 2018-05-13 1 1
This is how I understood the question.
First, test case:
SQL> create table a (cuser varchar2(10), modified_date date);
Table created.
SQL> create table b (cuser varchar2(10), modified_date date);
Table created.
SQL> create table c (cuser varchar2(10), modified_date date);
Table created.
SQL> insert into a values ('lf', date '2018-05-01');
1 row created.
SQL> insert into a values ('mc', date '2018-05-15');
1 row created.
SQL> insert into b values ('lf', date '2018-05-07');
1 row created.
SQL> insert into b values ('lf', date '2018-05-08');
1 row created.
SQL> insert into c values ('jw', date '2018-05-28');
1 row created.
SQL> insert into c values ('mc', date '2018-05-13');
1 row created.
SQL> insert into c values ('mc', date '2018-05-22');
1 row created.
SQL> alter session set nls_date_format = 'yyyy-mm-dd';
Session altered.
SQL> select * from a;
CUSER MODIFIED_D
---------- ----------
lf 2018-05-01
mc 2018-05-15
SQL> select * from b;
CUSER MODIFIED_D
---------- ----------
lf 2018-05-07
lf 2018-05-08
SQL> select * from c;
CUSER MODIFIED_D
---------- ----------
jw 2018-05-28
mc 2018-05-13
mc 2018-05-22
Query which returns desired result - number of rows per each user in every table, in desired date period. As I use SQL*Plus, variables are preceded by && to avoid multiple insert requests. In a tool you use, that might be a colon (:).
SQL> select nvl(nvl(a.cuser, b.cuser), c.cuser) cuser,
2 count(distinct a.modified_date) cnt_a,
3 count(distinct b.modified_date) cnt_b,
4 count(distinct c.modified_date) cnt_c
5 from a full outer join b on a.cuser = b.cuser
6 full outer join c on a.cuser = c.cuser
7 where a.modified_date between &&date_from and &&date_to
8 or b.modified_date between &&date_from and &&date_to
9 or c.modified_date between &&date_from and &&date_to
10 group by nvl(nvl(a.cuser, b.cuser), c.cuser)
11 order by 1;
Enter value for date_from: '2018-05-01'
Enter value for date_to: '2018-06-01'
CUSER CNT_A CNT_B CNT_C
---------- ---------- ---------- ----------
jw 0 0 1
lf 1 2 0
mc 1 0 2
SQL>

Store time while inserting data into oracle table

In one of my column, I want to store time also in below condition. Here is the query below.
APPROVED_DATE = CASE WHEN PAPPROVED_BY IS NULL THEN NULL ELSE SYSDATE END,
how to add time part in SYSDATE here
It's already there; if you don't see it, it is because your NLS settings. Here's an example of what you might do: ALTER SESSION or use TO_CHAR:
SQL> create table test (approved_Date date);
Table created.
SQL> insert into test values (sysdate);
1 row created.
SQL> select * from test;
APPROVED
--------
05.01.18
SQL> select to_char(approved_Date, 'dd.mm.yyyy hh24:mi:ss') from test;
TO_CHAR(APPROVED_DA
-------------------
05.01.2018 10:37:38
SQL> alter session set nls_date_format = 'dd-mm-yyyy hh:mi:ss am';
Session altered.
SQL> select * from test;
APPROVED_DATE
----------------------
05-01-2018 10:37:38 AM
SQL>
you CASE code is running perfectly fine. you just need to change your display setting as sysdate already have time element in it.
if you are using PL/SQL go to
tools > preference > Date/Time
and change to format as per your requirement.
and while accessing this field use to_char(sysdate , 'DD/MM/YYYY hh24:mi:ss')

Resources