Convert Time format(hh:mm) to Decimal format - db2

I am getting hours by subtracting time fields in the format hh:mm(00:21)
by using this
rtrim(char(TIMESTAMPDIFF(8,char(LABTRANS.finishtime - LABTRANS.starttime)))) || ':'|| rtrim(char(mod(int(TIMESTAMPDIFF(4,char(LABTRANS.finishtime - LABTRANS.starttime))),60))) as total_time,
), but i have to show in decimal value ( 0.3500 ). How can i achieve it.
see my sample table :
-->create table labtrans(starttime TIME,finishtime TIME)
-->insert into labtrans(starttime , finishtime )
values( '08:02 Am','08:42 Am'),
( '07:02 Am','08:42 Am'),
( '01:02 pm','09:02 PM'),
( '06:02 Am','08:00 Am')

When you subtract TIME columns in Db2, there result is DEC(6,0) in the following format: HHMMSS (time duration).
See the Time arithmetic topic for details.
So, if you need to express time duration in the HH+MM/60 format, then you may use the following:
select
starttime, finishtime
, finishtime-starttime hhmmss
, int(finishtime-starttime)/10000
+ dec(mod((finishtime-starttime)/100, 100), 27)/60 as "hh+mm/60"
from labtrans
The result is:
STARTTIME FINISHTIME HHMMSS hh+mm/60
--------- ---------- ------ --------
08:02:00 08:42:00 4000 0.6666
07:02:00 08:42:00 14000 1.6666
13:02:00 21:02:00 80000 8.0000
06:02:00 08:00:00 15800 1.9666

does this help at all?
values hour(current time) + .01 * minute(current time)
returns
1
----------------
10.14
1 record(s) selected.

Related

Postgres generate date series with exactly 100 steps

Lets say we have the dates
'2017-01-01'
and
'2017-01-15'
and I would like to get a series of exactly N timestamps in between these dates, in this case 7 dates:
SELECT * FROM
generate_series_n(
'2017-01-01'::timestamp,
'2017-01-04'::timestamp,
7
)
Which I would like to return something like this:
2017-01-01-00:00:00
2017-01-01-12:00:00
2017-01-02-00:00:00
2017-01-02-12:00:00
2017-01-03-00:00:00
2017-01-03-12:00:00
2017-01-04-00:00:00
How can I do this in postgres?
Possibly this can be useful, using the generate series, and doing the math in the select
select '2022-01-01'::date + generate_series *('2022-05-31'::date - '2022-01-01'::date)/15
FROM generate_series(1, 15)
;
output
?column?
------------
2022-01-11
2022-01-21
2022-01-31
2022-02-10
2022-02-20
2022-03-02
2022-03-12
2022-03-22
2022-04-01
2022-04-11
2022-04-21
2022-05-01
2022-05-11
2022-05-21
2022-05-31
(15 rows)
WITH seconds AS
(
SELECT EXTRACT(epoch FROM('2017-01-04'::timestamp - '2017-01-01'::timestamp))::integer AS sec
),
step_seconds AS
(
SELECT sec / 7 AS step FROM seconds
)
SELECT generate_series('2017-01-01'::timestamp, '2017-01-04'::timestamp, (step || 'S')::interval)
FROM step_seconds
Conversion to function is easy, let me know if have trouble with it.
One problem with this solution is that extract epoch always assumes 30-days months. If this is problem for your use case (long intervals), you can tweak the logic for getting seconds from interval.
You can divide the difference between the end and the start value by the number of values you want:
SELECT *
FROM generate_series('2017-01-01'::timestamp,
'2017-01-04'::timestamp,
('2017-01-04'::timestamp - '2017-01-01'::timestamp) / 7)
This could be wrapped into a function if you want to avoid repeating the start and end value.

Rounding artefacts when converting Double Precision column to Date

In a table dates are being saved as datatype DOUBLE PRECISION. Trying to convert it into date format but incorrect dates coming up when timestamp is greater than 12PM.(i.e gives date for next day if timestamp is greater or equal to 12:00:00.000)
This is what I've tried
dateColumn + CAST ('30.12.1899' AS DATE)
DATE'1899-12-30' + dateColumn
Example
SELECT po.DELIVERYDATE as DOUBLE_FORMAT,
po.DELIVERYDATE + CAST ('30.12.1899' AS TIMESTAMP) as DATE_TIMESTAMP_FORMAT,
po.DELIVERYDATE + CAST ('30.12.1899' AS DATE) as DATE_FORMAT_1,
DATE'1899-12-30' + po.DELIVERYDATE as DATE_FORMAT_2
FROM PURCHASE_ORDER po
Result
DOUBLE_FORMAT DATE_TIMESTAMP_FORMAT DATE_FORMAT_1 DATE_FORMAT_2
------------- ------------------------ ------------- -------------
41485.421586 30.07.2013, 10:07:05.000 30.07.2013 30.07.2013
41488.487419 02.08.2013, 11:41:53.000 02.08.2013 02.08.2013
41488.489792 02.08.2013, 11:45:18.000 02.08.2013 02.08.2013
41506.630035 20.08.2013, 15:07:15.000 21.08.2013 21.08.2013 //<-- Incorrect
41516.514479 30.08.2013, 12:20:51.000 31.08.2013 31.08.2013 //<-- Incorrect
41521.402963 04.09.2013, 09:40:16.000 04.09.2013 04.09.2013
41520.511030 03.09.2013, 12:15:53.000 04.09.2013 04.09.2013 //<-- Incorrect
That's arithmetic: when you are rounding a floating point to integer, then 2.5 is rounded to 3 not to 2.
So you have to
either, explicitly convert float to integer in the direction you want before offsetting the base date
or, since your data is both date and time - then convert it to TIMESTAMP first and only after that conversion do apply the rounding to DATE
Example:
select
41516.514479, -- 41516,514479
cast( 41516.514479 as integer ), -- 41517
round( 41516.514479 ), -- 41517
41516.514479 + DATE '1899-12-30', -- 31.08.2013
floor( 41516.514479 ), -- 41516
floor( 41516.514479 ) + DATE '1899-12-30', -- 30.08.2013
41516.514479 + timestamp '1899-12-30', -- 12:20 30.08.2013
cast(41516.514479 + timestamp '1899-12-30' as DATE) -- 30.08.2013
from rdb$database

hour() function of excel in postgres (equivalent)

I am working recently with postgres and I have to make several calculations. However I have not been able to imitate the HOUR () function of Excel, I read the official information but it did not help me much.
The function receives a decimal and obtains the hour, minutes and seconds of the same, example the decimal 0,99988426 returns 11:59:50. Try doing this in postgres (i use PostgreSQL 10.4) with the to_timestamp function: select to_char (to_timestamp (0.99988426), 'HH24: MI: SS'); this return 19:00:00. Surely I am omitting something, some idea of how to solve this?
24:00:00 or 86400 seconds = 1
Half day(12:00 noon) or 43200 seconds = 43200/86400 = 0.5
11:59:50 or 86390 seconds = 86390/86400 = 0.99988426
So to convert your decimal value to time, all you have to do is multiply it with 86400 which will give you seconds and convert it to your format in following ways:
SELECT TO_CHAR((0.99988426 * 86400) * '1 second'::interval, 'HH24:MI:SS');
SELECT (0.99988426 * 86400) * interval '1 sec';
There are two major differences to handle:
Excel does not consider the time zone. The serial date 0 starts at 0h00, but Postgres uses the time zone so it becomes 19h. You would need to use UTC in Postgres result to have the same as in Excel.
select to_char (to_timestamp (0), 'HH24: MI: SS'),to_char (to_timestamp (0) AT TIME ZONE 'UTC', 'HH24: MI: SS');
to_char | to_char
------------+------------
19: 00: 00 | 00: 00: 00
Excel considers that 1 is one day, while Postgres considers 1 as 1 second. To get the same behavior, multiply your number by the 86400, i.e. the number of seconds in a day
select to_char (to_timestamp (0.99988426*86400) AT TIME ZONE 'UTC', 'HH24: MI: SS');
to_char
------------
23: 59: 50
(1 row)

How to bin timestamp data into buckets of n minutes in postgres

I have the following query which works, binning timestamped "observations" into buckets whose boundaries are defined by the bins table:
SELECT
count(id),
width_bucket(
time :: TIMESTAMP,
(SELECT ARRAY(SELECT start_time
FROM bins
WHERE owner_id = 'some id'
ORDER BY start_time ASC) :: TIMESTAMP[])
) bucket
FROM observations
WHERE owner_id = 'some id'
GROUP BY bucket
ORDER BY bucket;
I would like to modify this to allow for querying arbitrary n-minute bins starting from a specified timestamp, rather than having to pull from from an actual "bins" table.
That is, given a start time, a "bin width" in minutes, and a number of bins, is there a way I can generate the array of timestamps to pass into the width_bucket function?
Alternatively, is there a different/simpler approach to get the same results?
Use the function generate_series(start, stop, step interval), e.g.
select array(
select generate_series(
timestamp '2018-04-15 00:00',
'2018-04-15 01:00',
'30 minutes'))
array
---------------------------------------------------------------------
{"2018-04-15 00:00:00","2018-04-15 00:30:00","2018-04-15 01:00:00"}
(1 row)
Example in Db<>fiddle.
The above answers seem to do what you want, but as of PostgreSQL 14, there is now a function date_bin just for binning timestamps.
Quoting the documentation:
date_bin(stride,source,origin)
source is a value expression of type timestamp or timestamp with time zone. (Values of type date are cast automatically to timestamp.) stride is a value expression of type interval. The return value is likewise of type timestamp or timestamp with time zone, and it marks the beginning of the bin into which the source is placed.
Examples:
SELECT date_bin('15 minutes', TIMESTAMP '2020-02-11 15:44:17', TIMESTAMP > '2001-01-01');
Result: 2020-02-11 15:30:00
SELECT date_bin('15 minutes', TIMESTAMP '2020-02-11 15:44:17', TIMESTAMP '2001-01-01 00:02:30');
Result: 2020-02-11 15:32:30
In the case of full units (1 minute, 1 hour, etc.), it gives the same result as the analogous date_trunc call, but the difference is that date_bin can truncate to an arbitrary interval.
The stride interval must be greater than zero and cannot contain units of month or larger.
I would like to call special attention to the line
The return value [...] marks the beginning of the bin into which the source is placed.
This means that input timestamps will always be binned by "rounding down", rather than binning to whichever bin is closest. E.g. if you do:
SELECT date_bin('1 hour', '2021-10-13 00:59:59', '2021-10-13 00:00:00');
Then the result will be 2020-10-13 00:00:00 (rounded down by 59 minutes and 59 seconds), NOT 2021-10-13 01:00:00 (which is only one second away from the supplied timestamp). So the date_bin function does something slightly different than exactly what you ask for, but I figure this is good to post for anyone coming here in the future.
A different approach without a series:
Divide the difference of time and start by the width of the bin (5 minutes in the example) and add 1 because the first bucket of width_bucket(...) is 1 not 0.
floor(extract(epoch from (time - '2019-06-04 00:00'::timestamp)) / (5 * 60) ) + 1 as bucket
Getting the start of the bin is also possible
to_timestamp(floor(extract(epoch from a.time) / (5 * 60)) * (5 * 60)) as bin_start
Putting this all together:
SELECT
count(id),
floor(extract(epoch from (time - '2019-06-04 00:00'::timestamp)) / (5 * 60) ) + 1 as bucket,
to_timestamp(floor(extract(epoch from time) / (5 * 60)) * (5 * 60)) as bin_start
FROM observations
WHERE owner_id = 'some id'
GROUP BY bucket, bin_start
ORDER BY bucket;

PostgreSQL difference between two dates and return in hours and minutes

Hello I want to return in my PostgreSQL the difference between two dates:
START: 2016-06-01 00:00:00
END: 2016-06-06 08:35:33
Expected return value: 128:35:33, formatted like format [h]:mm:ss;# in Excel. Hours must be added up if there is more than 24 hours of difference.
Here's my SQL:
SELECT EXTRACT(EPOCH FROM dt_termino::timestamp - dt_inicio::timestamp)/3600 FROM crm.task_interacao WHERE id_task_tarefa = 1
UPDATE!!!
hello now i'm facing another problema I have a table like this:
my table in database like this
start;end
2013-06-01 09:29:33;2016-06-07 14:08:19
2016-06-07 14:22:09;2016-06-07 14:22:43
2016-06-07 14:22:51; null
i need to sum values ....i'm trying as you said (1st awnser).. I cant use function because i'm using inside a php code
SELECT SUM(COALESCE(end::timestamp, now()::timestamp) - start::timestamp) FROM crm.task_interacao WHERE id_task_tarefa = 1
but is returning
1102 days 26:07:54.864879
why 26 hours??? I was supose be te at maximum 24...
no problem now to return (Days HH:MM:SS) and not miliseconds
You can simply subtract timestamps to get interval:
select '2016-06-06 08:35:33'::timestamp- '2016-06-01 00:00:00' result
result
-----------------
5 days 08:35:33
(1 row)
There is no standard function to convert the result to the format you need but you can write one:
create or replace function interval_without_days(interval)
returns interval language sql as $$
select $1- date_part('day', $1)* '1d'::interval+ date_part('day', $1)* '24h'::interval;
$$;
select interval_without_days('2016-06-06 08:35:33'::timestamp- '2016-06-01 00:00:00');
interval_without_days
-----------------------
128:35:33
(1 row)
Question #2. Use the functions date_trunc(text, interval) and justify_hours(interval):
select date_trunc('sec', justify_hours('1102 days 26:07:54.864879'));
date_trunc
--------------------
1103 days 02:07:54
(1 row)