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

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.

Related

Design SQL table to get data from different SQL query at regular intervals

I ma trying to capture cpu usage of my current SQL server over a time period and came across a query from here
Please guide me how can i use the above query to insert the results in permanent table over collected period of time without overwriting timestamp values or duplicating entries?
Thanks
You could use a scheduled job to execute the query and insert data into your table
https://learn.microsoft.com/en-us/sql/ssms/agent/schedule-a-job?view=sql-server-ver15
If you use the syntax
INSERT INTO dbo.sysUseLig
SELECT --the query
Having previously created your table dbo.sysUseLig with columns with the correct data types you will have what you need.
There is a timestamp based column in the select.

Add a derived attribute to existing table in SQL Developer

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

Is it possible to have same SYSDATETIME result for 2 transactions

According to documentation, the precision of SYSDATETIME() function in sql server is approximately 100 nanoseconds. I have seen that just like GETDATE(), the SYSDATETIME function also returns the same result within a transaction. Also, the time differs in two batches separated by GO.
Now my real question is, is it safe to assume that two transactions will always have different SYSDATETIME, no matter how close to concurrency they can reach within the same server/database instance, irrespective of the number of cores/hardware, the server has?
Background: I am trying to implement audit on an existing database using temporal tables. We are already keeping a modified by column in all tables. But we cannot identify who deleted a record using temporal tables. So I was thinking of dumping user id (end user's id) into a table for all transactions. So, if the time matches with temporal table, I might be able to identify the user based on date-time.
First i need to inform you that GETDATE() and SYSDATETIME gives the different datetime formate. SYSDATETIME() will give you result as follow - 2019-06-03 16:11:07.3683245 and GETDATE() will - 2019-06-03 16:11:07.367. Now the things is you need to add two columns in a table for which user updated the records and what time. And if you are not using any time consuming process between update temporal table and update main table than it will get same time both. But if any reason it will take time to update both record than it can be different time in both table.
You can use Declare method in sql to have same datetime in both table. No problem when it will update at different time. you can use Declare method to get datetime same as like follow.
Declare #date Datetime2 = SYSDATETIME()
Select #Date
You can use #Date When you are updating datetime in query.
I hope it will work.

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.

Calculate date and time key in fact table using existing date time field

I have date time field in a fact table in the format MM/DD/YY HH:MM:SS (e.g 2/24/2009 11:18:47 AM) and I have seperate date and time dimension
tables. What I would like ask is that how I can create date key and time key in the fact table using the date time field so that I can join the
date and time dimension.
There are alot of reference for creating seperate date and time dimension and their benefit but I could not find how to create date and time keys
in the fact table using existing date time field.
I have also heard that having date time field in fact table has certain benefit. If so, what would you recommend, should I have all three (date key, time key
and date time field) in the fact table. Date key and time key are must to have for me and I am concerned about fact table size if I have date time field
also in the fact table.
Thank you all for any help you can give.
What you need to do (if I understand correctly) is to create two fields in your Fact table:kTime, kDate.
We would always suggest using the primary keys for DimTime and DimDate as having meaning (this being a special case normally Dim tables' promary keys dont have any meaning). So e.g. in DimDate, we would have kDate as primary key with values formatted as YYYYMMDD so that you can order by kDate and it puts them in date order. Then have DimTime table having kTime primary key in the form HHMM or HHMMSS (depending on the resolution you need.
It is best to keep the actual date time field on the fact table as well, as it allows SQL to use its inbuilt date/time functions to do subsetting, but if you extend your Dim tables with useful extra columns : DimDate (add DayOfWeek, IsHoliday, DayNumber, MonthNumber, YearNumber, etc) and DimTime (HourNumber, MinuteNumber, IsWorkingTime), then you can perform very interesting queries very simply.
So to answer your question, "how to create date and time keys in the fact table using existing date time field?" ... as you are loading the data into the fact table, use the inbuilt date/time functions to create separate Date fields and Time fields.
It depends very much on how many rows you expect in your Fact table wether this approach will produce a lot of data, but it is the easiest to work with from a data warehouse point of view.
best of luck!