Add a derived attribute to existing table in SQL Developer - sql

Is there any way to add a derived date/time column (to an existing table) to hold calculated running time from a race.
The values are in hours, round to 2 decimals. There are 2 columns for start time and end time in the table. Below is the last I've tried got a missing expression error.
ALTER TABLE race
ADD race_time AS (SELECT ROUND(DATEDIFF (endtime, starttime),2)FROM race);

There is no datediff() function in Oracle and you also don't need a SELECT statement to create a computed column:
ALTER TABLE race
ADD race_time AS (endtime - starttime);
If endtime and starttime are defined as timestamp the race_time column will be of the type interval.
If those two columns are date columns, the result will be a number representing the number of days between the two days

It's been a while since I used this functionality but I think it would look more like:
ALTER TABLE race
ADD race_time AS (ROUND(DATEDIFF (endtime, starttime),2))
Note that I'm not aware of a DATEDIFF function in Oracle - if you haven't written this yourself and are looking to get the number of hours between two dates it would be more like:
ALTER TABLE race
ADD race_time AS (ROUND((endtime - starttime)*24.0,2))
If your columns are timestamps, it would probably be easier to convert them to dates when doing the math
https://dbfiddle.uk/?rdbms=oracle_11.2&fiddle=3ac1f46f8d7296754d14a3de6191dbe8

Related

MySQL Subtract Date Time and insert results into a new Column

I have a MySQL database, I use Heidi SQL to access it. The following columns are part of one of my tables:
START_DATE
START_TIME
STOP_DATE
STOP_TIME
01-10-2022
02:30:00
01-10-2022
03:30:00
What I need is to subtract start date/time from stop date/time and then create or insert into a 5th column called duration (in minutes), in this case the 5th column would get populated with 60.
Is this even possible?
Thanks
See TIMESTAMPDIFF
Since the DURATION column is to be the 5th column, I am assuming that each of the columns above are separated.
Using MySQL, you could try:
SELECT *,
TIMESTAMPDIFF(MINUTE,START_TIME, STOP_TIME) AS DURATION
FROM
TABLENAME;
The resulting query will display all your previous columns and add on the DURATION column as well. You may save the query to a table when you are done.
All of this is assuming that your working columns, as above, are in DATETIME. To check the information about a table's structure: DESCRIBE

Difference in dates of two columns and automatically updating it in third column

Consider there are two columns named StartDate,EndDate(2017/03/09 11:25:02, 2018/08/10 15:20:03) in sql.
I need to get the DurationDate column(number of years, months, days, hours, minutes, seconds) should be automatically updated in the duration column based on StartDate and EndDate in the sql table.
Since Oracle 11g, you can use generated columns:
alter table t add duration as (EndDate - StartDate);
This will produce an interval result if the two "dates" are stored as timestamps. The difference of two dates is a number of decimal days.
In earlier versions, you need to use views to accomplish the same thing.

Updating postgreSQL column with EXTRACT function (More than one row returned by a subquery used as an expression)

Using PostgreSQL 9.1 table, I want to update day column with extracting day number from timestamp column.
table:
id|name|timesatmp
-----------------
I created a day column then tried to extract the day number following:
SELECT EXTRACT(DAY FROM TIMESTAMP '2001-02-16 20:38:40');
Result: 16
http://www.postgresql.org/docs/current/static/functions-datetime.html
When i tried to update table using:
UPDATE table SET day =
( SELECT EXTRACT (DAY FROM "timestamp") FROM table);
I got this error:
More than one row returned by a subquery used as an expression
I've found that i can repair my statement by adding LIMIT 1 to the subquery to ensure that at most 1 row is returned. but still not sure if it's the right thing to do.
I don't see the need for this sub-select, just update the column:
UPDATE table SET day = EXTRACT (DAY FROM "timestamp");
The real question is though: why do you want to store information that can be derived from the existing data (and in this case: without any additional cost)
Btw: timestamp is a horribly name for a column. First because it's also the name of a datatype and thus is really confusing. Secondly (and more importantly I think) it doesn't document at all what the column contains. A "start date"? an "end date"? a "due date" a "valid date"? ...

Diff. between dates - SQL plus [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
DateDiff Function
I am trying to find the difference between dates in SQL. The difference in days will have to be displayed when the query is given. I get an error saying that DATEDIFF is an invalid identifier. Any ideas?
Here is the table:-
Create table auctions(
item varchar2(50),
datebought date,
datesold date,
days number
);
Insert into auctions values (‘Radio’,’12-MAY-2001’,’21-MAY-2001’);
Select item,datebought,datesold,DATEDIFF(dd ,datebought,datesold )”days”
from auctions;
The title of the question includes "SQL plus", which implies Oracle to me.
Oracle does not have a DATEDIFF function. The simplest method to determine the number of days between 2 dates using Oracle is to subtract one date from the other.
Select item, datebought, datesold, datesold-datebought as days from auctions;
Oracle stores both date and time info in a date field. It is common practice to store dates truncated to midnight within date fields in Oracle if you don't care about the time.
If your dates are already truncated to midnight, then the computed value will be an integral number.
But if your stored dates include time info, then you may want to truncate the value to get an integral value. I suspect you would want to truncate each date before the subtraction, such that if bought on one day, then any time the next day would count as 1 day. Oracle truncates dates to midnight.
select item, datebought, datesold, trunc(datesold)-trunc(datebought) as days from auctions;
I agree with WarrenT that your table should not have the denormalized days column.
try this,Its working for me
Create table auctions(
item varchar(50),
datebought date,
datesold date,
days int
)
Insert into auctions(item,datebought,datesold)
values ('Radio','12-MAY-2001','21-MAY-2001')
Select item,datebought,datesold,DATEDIFF(dd ,datebought,datesold )as days
from auctions;
In a normalized database design, you would not want to define a table column whose value could be calculated from one or more other columns. You would be better off defining the days held in a SELECT or in a VIEW.
If you were using DB2, you could calculate the days held as DAYS(DATESOLD) - DAYS(DATEBOUGHT)

Storing time values in a database and getting the difference between two time fields

I am working on an application the stores time values in a database e.g Expected time of Arrival and actual time of arrival. What is the best way of storing these fields in a database?
What SQL query can I use to obtain the time difference between the two fields?
If you're on SQL Server 2008, use the TIME datatype. Declare those two fields
ExpectedTimeOfArrival TIME(7),
ActualTimeOfArrival TIME(7)
Read up on how to manipulate the TIME datatype here.
Once you have those two fields in place, you could add a computed column to your table that calculates the difference between those two, e.g. in minutes:
ALTER TABLE dbo.YourTable
ADD TimeDifference AS DATEDIFF(MINUTE, ActualTimeOfArrival, ExpectedTimeOfArrival)
and then you could query o that new column TimeDifference as if it were a normal, regular table column.