Firebird - Converting stored integer to time equivalent - sql

I am currently working on a Firebird database which stores slots for a time (like appointments). In the data the timeslot is stored in an Integer format but is not the slot time.
Example:
Slot Time: 11am
Database Value: 660
The database value is represented as the number of minutes since midnight. So 11am is 660 minutes from midnight. Or 12noon is 720
Question How, in Firebird, can a convert this 660 to display as 1100 (still an integer) or 540 as 900 (basically 9am)?

What you have stored into database is minutes since start of the day. So you just divide with 60 to get hours. Keep in mind that in SQL if both arguments of the division are integers you'll get integer as an answer, not decimal! Something like following should work for you (assuming the number of minutes is stored to timeslot field):
SELECT
cast(timeslot / 60 as varchar(2)) ||
case
when mod(timeslot, 60) < 10 then '0'|| mod(timeslot, 60)
else mod(timeslot, 60)
end
FROM t
This should give you 1130 for 11:30 am, not 1150 (11,5 hours).
Also see the DATEADD function.

First off I'd store it in the database as a TIME column instead of a number of minutes. Note that on a technical level Firebird stores TIME as the number of 100 microseconds since midnight. However if you really want to store as number of minutes, then you can use:
From minutes to TIME using DATEADD:
DATEADD(x MINUTE TO TIME'00:00')
Time to minutes using DATEDIFF:
DATEDIFF(MINUTE FROM TIME'00:00' TO y)
So:
SELECT
DATEADD(660 MINUTE TO TIME'00:00'),
DATEDIFF(MINUTE FROM TIME'00:00' TO TIME'11:00')
FROM RDB$DATABASE
Will return: 11:00:00.000, 660

Related

Formatting a 'duration' column in the query

I have a column 'Duration' it holds the time the therapist spent with the client.
This is always entered as minutes so if the time was 3 hours it is entered as 180. I would like to set this in the query as 3.
This is how it is reporting from a canned report: Total duration time is the entered column, it is
defined as int,null. I would like to make this calculation and formatiing, in the sql for the shown column 'total duration'.
total_duration_num total_duration
10 0:10
120 2:00
30 0:30
5 0:05
60 1:00
One means of achieving this is to use:
the floor function to round down when dividing the minutes by 60 (for whole hours)
the mod function to get the remaining number of minutes after putting however many can fit into "whole" 60-minute hours
the lpad function to put a leading zero before that number of minutes, if <10, so that you see :05 rather than :5 for example
The query would look like this:
select duration,
concat( floor(duration/60) , ':' , lpad(mod(duration,60),2,'0') ) as hrs_mins
from duration_table;
This is a demonstration:
http://sqlfiddle.com/#!9/a52b6c/1/0

I have a sum of difference between dates in a Sql table. How do I calculate its average in days, hours, minutes?

I have a table with a start time and an end time (datetime, example: 2021-07-09 17:12:00) in each row.
When I calculate the sum of all differences between end time and start time by this SQL query:
SELECT
TIME(SUM(TIMEDIFF(`end_time`, `start_time`))) AS TimeDiff
FROM
my_table
I get a result expressed in hours, minutes, seconds (example: 00:02:00)
Now I need to calculate the average time expressed in hours, minutes, seconds of this sum.
It means:
SELECT
TIME(SUM(TIMEDIFF(`end_time`, `start_time`))) AS TimeDiff
FROM
my_table
TimeDiff/number of rows (expressed in hours, minutes, seconds)
Can you help me? Really thanks
Going out on a limb and guessing you're using MySQL due to the TIMEDIFF function.. Always state what DB you use; SQL is a standard, not a product
You asked about converting times to seconds and back, it probably looks like:
SELECT SEC_TO_TIME(AVG(TIME_TO_SEC(TIMEDIFF(end_time, start_time)))) FROM ...

Optimization: How to get TimeId from time for each minute in a week?

I am creating a table which will have 2 columns:
Day_time (time from 1978-01-01 00:00:00 Sunday, till 1978-01-07 23:59:00.0 Saturday, Granularity: Minute)
Time_id (a unique id for each minute), to be populated
I have column one populated. I want to populate column two.
How I am doing it right now:
EXTRACT(dayofweek FROM day_time) * 10000 + DATEDIFF('minutes', TRUNC(day_time), day_time)
I basically want a function where I pass any date and it tells me where I am in a week. So, I need a function, just like the function above. Just more optimized, where I give a date and get a unique ID. The unique ID should repeat weekly.
Example: ID for Jan 1, 2015 00:00:00 will be same as Jan 8, 2015 00:00:00.
Why 1978-01-01? cuz it starts from a Sunday.
Why 10,000? cuz the number of minutes in a day are in four digits.
You can do it all in one fell swoop, without needing to extract the date separately:
SELECT DATEDIFF('minutes', date_trunc('week',day_time), day_time) which I'd expect to be marginally faster.
Another approach that I'd expect to be significantly faster would be converting the timestamp to epoch, dividing by 60 to get minutes from epoch and then taking the value modulus of 10,080 (for 60 * 24 * 7 minutes in a week).
SELECT (extract(epoch from day_time) / 60) % 10080
If you don't care about the size of the weekly index, you could also do:
SELECT (extract(epoch from day_time)) % 604800 and skip the division step altogether, which should make it faster still.

Updating dates with random time

I have a datetime column, all of them at 12:00 am. Is there a way to update them with random hours, minutes to nearest 1/2 hour while keeping the same date(day) value?
Update Activities set ActivityDate = ....
Here's one option using dateadd:
update Activities
set ActivityDate = DateAdd(minute,
30 * (abs(checksum(NewId())) % 47), ActivityDate);
SQL Fiddle Demo
And here's a good post about generating random numbers. Using that, multiple by 30 minutes to get to the nearest half hour.
Note, this uses % 47 since there are 1440 minutes in a day -- that divides into 48 potential half hour segments in that same day.

How to subtract 1 minute from hour

I have a query contains from time and to time parameters.
I need to subtract 1 minute from totime parameter.
I used totime-1.
It is working in database level and it is not working in my crystal reports.I mean it is subtracting 1 hour in report level.
Can any one please tell me other formula for subtracting a minute.
Thanks,
vissu
If you are using SQL Serve/T-sql
DATEADD(MI, -1, totime)
In some databases (SQL Server and Oracle for instance), you can do the following:
select totime - 1.0/(24*60)
Time differences are measured in days, so 1.0/(24*60) is one minute represented as a fraction of a day.
MySQL accepts the syntax, but the timestamp is measured in seconds not days, so you would use:
select totime - 60