I am working on a set of tables for an assignment related to airports,
CREATE TABLE FLIGHT
(FlightCode VARCHAR2(6),
AirplaneSerialNum VARCHAR2(6) NOT NULL,
RemCapacity NUMBER(3),
FlightDate DATE NOT NULL,
RouteNum NUMBER(5) NOT NULL,
FlightDeparture DATE,
FlightArrival DATE,
CONSTRAINT FlightPK PRIMARY KEY (FlightCode),
CONSTRAINT RouteFK FOREIGN KEY (RouteNum) REFERENCES ROUTE (RouteNum));
Where FlightDeparture and FlightArrival are ideally referenced in the 24H format or 12H format (HHMM), how should I go about writing it, and how can I make a column in a separate table referencing the difference (to 2dp.) in number of hours between FlightArrival and FlightDeparture?
e.g. in another table that references FlightCode in FLIGHT as a FK
HoursInFlight NUMBER(10,2)
where HoursInFlight = FlightArrival - FlightDeparture
DATE datatype looks OK; it stores values in Oracle's internal format, so it is up to you how will you display them to users. For example:
Inserting:
SQL> create table flight (flight_date date);
Table created.
SQL> insert into flight (flight_date) values (sysdate);
1 row created.
SQL> insert into flight (flight_date) values (to_date('18.07.2021 16:58', 'dd.mm.yyyy hh24:Mi'));
1 row created.
Selecting (various options):
SQL> select * from flight;
FLIGHT_D
--------
18.07.21
18.07.21
SQL> select to_char(flight_date, 'dd-mon-yy hh:mi am', 'nls_date_language = english')) val from flight;
select to_char(flight_date, 'dd-mon-yy hh:mi am', 'nls_date_language = english')) val from flight
*
ERROR at line 1:
ORA-00923: FROM keyword not found where expected
SQL> select to_char(flight_date, 'dd-mon-yy hh:mi am', 'nls_date_language = english') val from flight;
VAL
---------------------------
18-jul-21 04:58 PM
18-jul-21 04:58 PM
SQL> select to_char(flight_date, 'dd/mm/yyyy hh24:mi:ss') val from flight;
VAL
-------------------
18/07/2021 16:58:36
18/07/2021 16:58:00
SQL> alter session set nls_Date_Format = 'yyyy-mm-dd hh:mi am';
Session altered.
SQL> select * from flight;
FLIGHT_DATE
-------------------
2021-07-18 04:58 PM
2021-07-18 04:58 PM
SQL>
The difference of two DATE values is number of days between them, so - if you want to "convert" them to hours, multiply it by 24 (as there are 24 hours in a day):
SQL> select to_date('18.07.2021 17:02', 'dd.mm.yyyy hh24:mi') - to_date('18.07.2021 10:44', 'dd.mm.yyyy hh24:mi') diff
2 from dual;
DIFF
----------
,2625
SQL> select 0.2625 * 24 hours from dual;
HOURS
----------
6,3
SQL>
Related
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>
I have a table with TIMESTAMP datatype and I need to compare month value to select all table values. Example the created is the field and wanted to get all the rows which is created between November & December of any year. Tried with below query and it don't work.
select * from table_name where TO_CHAR(created_time, 'mon') in ('nov','dec')
Use EXTRACT:
SELECT *
FROM table_name
WHERE EXTRACT( MONTH from created_time ) IN ( 11, 12 )
Or, you can use TO_CHAR( created_time, 'MM' ) to get the numeric month value (and not worry about language settings as you would have to with the MON format model):
SELECT *
FROM table_name
WHERE TO_CHAR( created_time, 'MM' ) IN ( '11', '12' )
db<>fiddle
Well, there are several ways to achieve this.
1.Using the nlsparam of to_char function
The 'nlsparam' argument specifies the language in which month and day names and abbreviations are returned.
Example
SQL> create table t ( c1 timestamp ) ;
Table created.
SQL> alter session set nls_timestamp_format = 'dd.mm.yyyy hh24:mi:ss.rr' ;
Session altered.
SQL> insert into t values ( systimestamp - 30 ) ;
1 row created.
SQL> insert into t values ( systimestamp ) ;
1 row created.
SQL> select * from t ;
C1
---------------------------------------------------------------------------
30.07.2020 09:29:35.20
29.08.2020 09:29:42.20
SQL> select value from nls_database_parameters where parameter='NLS_LANGUAGE' ;
VALUE
--------------------------------------------------------------------------------
AMERICAN
SQL> select c1 , to_char(c1, 'mon' , 'NLS_DATE_LANGUAGE = american') as mon from t ;
C1
---------------------------------------------------------------------------
MON
------------
30.07.2020 09:29:35
jul
29.08.2020 09:29:42
aug
2.However, to avoid depending in the language, you can use extract and compare the number of the month which is the same in any language. In this case you need to convert the timestamp to a date, but before you need to set the nls_timestamp_format to the specific format.
SQL> alter session set nls_timestamp_format = 'dd.mm.yyyy hh24:mi:ss';
Session altered.
SQL> select c1 , extract(month from to_date(c1,'dd.mm.yyyy hh24:mi:ss')) from t ;
C1
---------------------------------------------------------------------------
EXTRACT(MONTHFROMTO_DATE(C1,'DD.MM.YYYYHH24:MI:SS'))
----------------------------------------------------
30.07.2020 09:29:35
7
29.08.2020 09:29:42
8
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
i have same problem like here
how to calculate sum time with data type char in oracle
but little different. i have 2 table like this :
table employee
emp_id emp_name emp_birth_date
123456 sacha 18/07/1980
Using this query
create table employee (emp_id char(10), emp_name char(10), emp_birth_date date);
insert into employee values ('123456', 'sacha', (TO_DATE('18/07/1980', 'dd/mm/yyyy')));
and table dept
table dept
emp_id emp_reg_date emp_time_in emp_time_off
123456 25/12/2011 10:00:00 19:00:00
using this query
create table dept (emp_id char(10), emp_reg_date date, emp_time_in char(10), emp_time_off char(10));
insert into dept values ('123456', (TO_DATE('25/12/2011', 'dd/mm/yyyy')), '10:00:00', '19:00');
all data type is char except birth_date and reg_date
i can display emp_id, emp_name, emp_reg_date, emp_time_in, emp_time_off using this query select employee.emp_id, employee.emp_name, dept.emp_date_reg, dept.emp_time_in, dept.emp_time_off from employee, dept where employee.emp_id = dept.emp_id;
but how to calculate total time in table dept for emp_time_in and emp_time_off for a day and a month?
What an awful design; what made you create EMP_TIME_IN and EMP_TIME_OFF VARCHAR2 columns? Those should have been DATE ones. I suggest you change that.
Meanwhile, you'll have to concatenate EMP_REG_DATE and those IN and OFF columns in order to get DATE value; then, by subtracting two dates, you'd get number of DAYS and - using a little bit of mathematics - get hours, minutes, or whatever you want.
For example:
SQL> create table test
2 (empno number,
3 emp_reg_date date,
4 emp_time_in varchar2(10),
5 emp_time_off varchar2(10));
Table created.
SQL> insert into test values (1, date '2018-03-20', '10:00', '19:00');
1 row created.
SQL> insert into test values (1, date '2018-03-21', '11:30', '12:30');
1 row created.
SQL> insert into test values (2, date '2018-03-25', '13:00', '16:20');
1 row created.
Employee 1 worked 9 hours + 1 hour = 10 hours in total.
Employee 2 worked 3 hours 20 minutes.
SQL> with dates as
2 (select
3 empno,
4 to_date(to_char(emp_reg_date, 'dd.mm.yyyy') || emp_time_in , 'dd.mm.yyyy hh24:mi') date_in,
5 to_date(to_char(emp_reg_date, 'dd.mm.yyyy') || emp_time_off, 'dd.mm.yyyy hh24:mi') date_off
6 from test
7 ),
8 summary as
9 (select empno,
10 sum(date_off - date_in) diff_days
11 from dates
12 group by empno
13 )
14 select empno,
15 trunc(diff_days * 24) hours,
16 round((diff_days * 24 - trunc(diff_days * 24)) * 60) minutes
17 from summary;
EMPNO HOURS MINUTES
---------- ---------- ----------
1 10 0
2 3 20
SQL>
Note that there's practically no control over what you enter into VARCHAR2 columns TIME_IN and TIME_OUT; what prevents you from entering AX:FM or 99:45 or A-b-e_XF in there? All those are valid strings, but invalid times.
Once again: fix data model.
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 ...