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)
Related
I have 3 columns in my data set.
email
first name
unsubscribe date (timestamp)
How can i create a pivot table in SQL to have years on the left in rows, and months in columns and count the number of people who unsubscribed by month and year?
Thanks for the help
Unfortunately, you can't. Timestamp is poorly named, and has nothing to do with the time the person unsubscribed.
I am trying to query a from scratch database I created. For each job posting in my database, I store the date of which it was posted in the 'date' datatype, which is in the format of "DD-MON-YY". I want to know which job postings haven't been filled after one month after its posted date.
So far I can query my database and find which jobs haven't been filled, due to a particular attribute being null in my table. But I don't know how to add the condition that it has been at least one month.
SELECT JOBID
FROM JOB
WHERE HIRED IS NULL;
For reference I have created this database in Oracle, and am using SQL developer.
I guess to rephrase, how would I make that condition, would I add one month to the date stored and the compare it with today's date?
Compare your date column to the current date with -1 months added to it:
SELECT JOBID
FROM JOB
WHERE HIRED IS NULL
AND date_column <= ADD_MONTHS(SYSDATE, -1);
I would like to compare two datetime values from different rows that share a reference number. Both datetime values with the same reference are always from the same day, so the expected result is a column with the number of hours, minutes and seconds that passed between the datetime of each row and the first row with the same reference available in the table.
The problem is that there are usually more than two rows with the same reference and I always need to substract the earliest datetime available for this reference from each row's datetime.
How could I do this in SQL Server?
Best regards
You would simply use a window function. Something like this:
select t.*,
datediff(second, min(dt_col) over (partition by ref), dt_col) as time_in_seconds
from t;
Your question doesn't appear to be about formatting the result as a time, so this just returns the value as seconds.
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
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.