I am learning SQL and using Oracle SQL Developer. I have a table that contains the following columns
I have inserted values using either
TO_DATE('10:45', 'hh24:mi')
or
TO_DATE('20/10/2000', 'DD/MM/YYYY')
When I do a SELECT * FROM TABLE_NAME, the DepartureTime and ArrivalTime display a date (which I have not entered). How do I display the date in the first column and time in the other 2 columns?
I have tried `
SELECT to_char(DepartureTime, 'HH24:MI' ) AS Departure
to_char( ArrivalTime, 'HH24:MI' ) AS Arrival
FROM FLIGHT;
` Although the above statement displays the right values, I want to write a statement to output all the columns (because the actual table has more than 3 columns), but in the format explained above - a date for FlightDate and time for DepartureTime and ArrivalTime.
In Oracle, a DATE
is a binary data type that consists of 7 bytes representing century, year-of-century, month, day, hour, minute and second. It ALWAYS has all of those components and it is NEVER stored in any particular (human-readable) format.
Therefore, if you have a DATE
it will always be both a date and a time.
If you only want to store the date component of a date then you will need to use the entire date but could add a constraint to ensure the time is always midnight.
If you want to store a time without a date then you can either:
DATE
data type and just set the time component (and ignore the default values of the date component); orINTERVAL DAY TO SECOND
data type.For example, your table could be:
CREATE TABLE table_name (
FlightDate DATE
CONSTRAINT table_name__flightdate__chk CHECK (flightdate = TRUNC(flightdate)),
DepartureTime INTERVAL DAY(0) TO SECOND(0) NOT NULL,
ArrivalTime INTERVAL DAY(1) TO SECOND(0) NOT NULL
);
Or, you could simplify your table to:
CREATE TABLE table_name (
Departure DATE NOT NULL,
Arrival DATE NOT NULL
);
and not worry about having separate dates and times.