Store time while inserting data into oracle table - oracle

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

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

Oracle: Date Format Problems YYYY-MM-DD

I have a problem that could seems naive (and could actually be naive) but I cannot solve...
The thing is that I'm trying to insert some data from Pentaho to a Oracle DB and in Pentaho I have my Date in format YYYY-MM-DD, but when in Oracle it appears YYYY-MM-DD 00:00:00. The point is that the format of the attribute is Date, and when I manually, through SQL, insert data into the table, it happens the same.
INSERT INTO TABLE1
VALUES(TO_DATE('2012-02-01','yyyy-mm-dd'))
When I visualize the data, I see again 2012-02-01 00:00:00.
I'm using Dbeaver, and when I go to Properties to see the Date format, it appears YYYY-MM-DD, so I don't understand what's happening neither how to solve it.
Thank you!
The date format visualized in the oracle depends on the NLS_DATE_FORMAT parameter. But please note that oracle does not store the Date in any format but it has its own binary representation for dates.
so you can configure how you want to see dates in your session or at the database level or at an instance level.
At session-level, You can alter NLS_DATE_FORMAT as follows:
SQL> ALTER SESSION SET NLS_DATE_FORMAT = 'YYYY-MM-DD';
Session altered.
SQL> SELECT SYSDATE FROM DUAL;
SYSDATE
----------
2020-11-27
SQL> ALTER SESSION SET NLS_DATE_FORMAT = 'YYYY-MM-DD HH24:MI:SS';
Session altered.
SQL> SELECT SYSDATE FROM DUAL;
SYSDATE
-------------------
2020-11-27 15:17:02
SQL> ALTER SESSION SET NLS_DATE_FORMAT = 'DD-MON-YYYY';
Session altered.
SQL> SELECT SYSDATE FROM DUAL;
SYSDATE
-----------
27-NOV-2020
SQL>
To check for the current value of NLS_DATE_FORMAT, You can use the following view:
SELECT * FROM NLS_SESSION_PARAMETERS WHERE PARAMETER = 'NLS_DATE_FORMAT';
SELECT * FROM NLS_INSTANCE_PARAMETERS WHERE PARAMETER = 'NLS_DATE_FORMAT';
SELECT * FROM NLS_DATABASE_PARAMETERS WHERE PARAMETER = 'NLS_DATE_FORMAT';
I would not rely on the NLS_DATE_FORMAT nls parameter (what if you have multiple queries which need to output the dates in different formats?). If you need to view your dates in a specific format, use to_char(), e.g.:
SELECT to_char(date_col, 'dd/mm/yyyy') date_col
FROM table1;

I would like to keep my table's data type as date but I want my table to show date and time format in the same column

I would like to keep my table's data type as date but I want my table to show date and time in the same column.
This is what I have so far. How do I change the format? I use Oracle SQL.
insert into student
values
(001,
to_date('2018-02-02 21:05:18', 'YYYY-MM-DD HH24:MI:SS'),
'Oriel Road Brisbane',
103,
2486675,
760024,
'fdg57690gmig'
);
You don't have to do anything; that column already contains both date and time. It is the front-end that is supposed to display it as you want. Here are some examples:
SQL> create table test (datum date);
Table created.
SQL> insert into test (datum) values (to_date('2018-02-02 21:05:18', 'YYYY-MM-DD HH24:MI:SS'));
1 row created.
SQL> select * from test;
DATUM
--------
02.02.18
SQL> select to_char(datum, 'hh24:mi:ss') only_time,
2 to_char(datum, 'dd-mon-yy') date_format_1,
3 to_char(datum, 'yyyy-mm-dd hh24:mi:ss') date_and_time
4 from test;
ONLY_TIM DATE_FORMAT_1 DATE_AND_TIME
-------- ------------------ -------------------
21:05:18 02-vel-18 2018-02-02 21:05:18
SQL>
Which means: use TO_CHAR with appropriate format mask, if you use SELECT statement. If it is about e.g. Oracle Apex, Forms or Reports, modify item's format mask.
Alternatively, you may talk to DBA to change NLS settings for the whole database, or you can do it for your session:
SQL> alter session set nls_date_format = 'dd.mm.yyyy hh24:mi:ss';
Session altered.
SQL> select datum from test;
DATUM
-------------------
02.02.2018 21:05:18
Oracle's DATE datatype does store the date and time - there is not date-only datatype in Oracle, unlike some other databases, such as MySQL, that has DATE and DATETIME datatypes.
Your code is correctly inserting a date/time value in the table.
When Oracle displays a date, it uses by default the format defined by parameter NLS_DATE_FORMAT (which, for your database, is probably something that does not include the time portion).
You ca either change the setting at session level:
alter session set nls_date_format = 'yyyy-mm-dd hh24:mi:ss';
Or you can use to_char() on the date column:
select to_char(mydatecol, 'yyyy-mm-dd hh24:mi:ss') as mydatestring
from mytable

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

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 ...

Resources