convert seconds to hour and min format - postgresql

I have a column that return seconds from a task and I'd like to convert it into hours and minutes
Example: if the column has 16200 seconds I'd like to get the result as 4h30m
Also, need to get sum of all the values as hr and min,....sum(column name)=sum(hr)sum(min)

If your seconds column is a numeric type, you can use this:
SELECT TO_CHAR((16200 || ' second')::interval, 'fmHH24hfmMIm')
If its a string type, use this:
SELECT TO_CHAR('16200'::interval, 'fmHH24hfmMIm')
Of course, replace 16200 with your appropriate column name.
Precede HH and MI with fm for Fill Mode. This will remove the preceding zero values from hour and minute.
Output:
4h30m
See Fiddle.
To get the SUM of all values, add the SUM function to your seconds column like so:
SELECT TO_CHAR(SUM(seconds_column::interval), 'fmHH24hfmMIm') FROM test_table
See Fiddle.

Related

Recurring future date at every 3 month after create date in PostgreSQL

I am looking for a function in PostgreSQL which help me to generate recurring date after every 90 days from created date
for example: here is a demo table of mine.
id date name
1 "2020-09-08" "abc"
2 "2020-09-08" "xyz"
3 "2020-09-08" "def"
I need furure date like 2020-12-08, 2021-03-08, 2021-06-08, and so on
First it's important to note that, if you happen to have a date represented as text, then you can convert it to a date via:
SELECT TO_DATE('2017-01-03','YYYY-MM-DD');
So, if you happen to have a text as an input, then you will need to convert it to date. Next, you need to know that if you have a date, you can add days to it, like
SELECT CURRENT_DATE + INTERVAL '90 day';
Now, you need to understand that you can use dynamic variables, like:
select now() + interval '1 day' * 180;
Finally, you will need a temporary table to generate several values described as above. Read more here: How to return temp table result in postgresql function
Summary:
create a function
that generates a temporary table
where you insert as many records as you like
having the date shifted
and converting text to date if needed
You can create a function that returns a SETOF dates/timestamps. The below function takes 3 parameters: a timestamp, an interval, the num_of_periods desired. It returns num_of_periods + 1 timestamps, as it returns the original timestamp and the num_of_periods each the specified interval apart.
create or replace
function generate_periodic_time_intervals
( start_date timestamp
, period_length interval
, num_of_periods integer
, out gen_timestamp timestamp
)
returns setof timestamp
language sql
immutable strict
as $$
select (start_date + n * period_length)::timestamp
from generate_series(0,num_of_periods) gs(n)
$$;
For your particular case to timestamp/date as necessary. The same function would work for your case with the interval specified as '3 months' or of '90 days'. Just a note the interval specified can be any valid INTERVAL data type. See here. It also demonstrates the difference between 3 months and 90 days.

Postgres truncate the millisecond part from three to two digits

I am formatting an input that I get in Timestamp datatype and sending the output as a text in the below format
2020-07-30 10:45:23.638 PM
For this I'm using the below query
select to_char(input_timestamp, 'YYYY-MM-DD HH:MI:SS.MS AM');
But I want the microsecond part truncated to 2 digits instead of 3 digits. The output will be in TEXT datatype. Expecting help on this.
Try this:
select
left(to_char(input_timestamp::timestamp(2), 'YYYY-MM-DD HH:MI:SS.MS'), 22) ||
to_char(input_timestamp, 'FM AM');
First round to two microsecond' digits, convert to string, chop to 22 characters and then concatenate the meridiem indicator.

need tsql code for time intervel and the values should go to that particular interval the original time is in epoch format

I have to add column to table.
The column value is calculated based on the column already present in the table.
I have to get time-stamp (column already present) and then group them into 5 min time-slots.
E.g: if the time is:
13:03/13:02 then it should go as 13:00;
13:53/13:52 then it should go as 13:50;
13:21 then should go as 13:20 and so on
PS: basically I have to get time stamp in epoch (UNIX time stamp)format [the table has values in epoch as well as in regular time stamp]
So what I'm seeing is that you have times and you need to round them down to the nearest 5 minute increment. Try this:
DECLARE #table TABLE (times TIME)
INSERT INTO #table
VALUES ('13:03'),
('13:02'),
('13:53'),
('13:52'),
('13:21');
SELECT times,
DATEADD(MINUTE,-DATEDIFF(MINUTE,0,times) % 5,times) five_minute_increments
FROM #table
Results:
times five_minute_increments
---------------- ----------------------
13:03:00.0000000 13:00:00.0000000
13:02:00.0000000 13:00:00.0000000
13:53:00.0000000 13:50:00.0000000
13:52:00.0000000 13:50:00.0000000
13:21:00.0000000 13:20:00.0000000
Epoch Version
DECLARE #epoch BIGINT;
--Epoch is the seconds since Jan 1,1970
SET #epoch = DATEDIFF(SECOND,'1970-01-01','2015-04-01 12:06:00.000');
SELECT CAST(DATEADD(SECOND,#epoch - (#epoch %300),'1970-01-01 00:00:00.000') AS TIME) AS epochTimes
Results:
12:05:00.0000000
create table epoch1(epoch int not null,
epoch_date as dateadd(s,epoch,'19700101'))
insert epoch1(epoch) values(1331070999)
insert epoch1(epoch) values(1331070956)
insert epoch1(epoch) values(1331071998)
insert epoch1(epoch) values(1331071999)
select DATEADD(MINUTE,-DATEDIFF(MINUTE,0,dateadd(MINUTE,epoch,'19700101')) % 5,dateadd(MINUTE,epoch,'19700101')) as human ,DATEDIFF(MINUTE, '1970-01-01 00:00:00', DATEADD(MINUTE,-DATEDIFF(MINUTE,0,dateadd(MINUTE,epoch,'19700101')) % 5,dateadd(MINUTE,epoch,'19700101'))) as timeinterval
from epoch1

Insert Column with value (#days) between dates in other columns

I have two columns; CommencementDate and ExpectedCompletionDate. I would like to insert a column (Days) which gives the number of days between the two date columns in my table.
I'm not sure where to start. I'm on day 5 of writing queries!
You just have to substract ExpectedCompletionDate and CommencementDate and return the days of that.
SELECT DATEDIFF(Day, ExpectedCompletionDate, CommencementDate);
Also you can return the absolute value to ensure this is a positive value.
SELECT ABS(DATEDIFF(Day, ExpectedCompletionDate, CommencementDate));
You can see the documentation in microsft
http://technet.microsoft.com/en-us/library/ms189794.aspx -> Datediff
http://technet.microsoft.com/en-us/library/ms189800.aspx -> ABS

TSQL update Datetime with Random Value between 2 Dates

What's the easiest way to update a table that contains a DATETIME column on TSQL with RANDOM value between 2 dates?
I see various post related to that but their Random values are really sequential when you ORDER BY DATE after the update.
Assumptions
First assume that you have a database containing a table with a start datetime column and a end datetime column, which together define a datetime range:
CREATE DATABASE StackOverflow11387226;
GO
USE StackOverflow11387226;
GO
CREATE TABLE DateTimeRanges (
StartDateTime DATETIME NOT NULL,
EndDateTime DATETIME NOT NULL
);
GO
ALTER TABLE DateTimeRanges
ADD CONSTRAINT CK_PositiveRange CHECK (EndDateTime > StartDateTime);
And assume that the table contains some data:
INSERT INTO DateTimeRanges (
StartDateTime,
EndDateTime
)
VALUES
('2012-07-09 00:30', '2012-07-09 01:30'),
('2012-01-01 00:00', '2013-01-01 00:00'),
('1988-07-25 22:30', '2012-07-09 00:30');
GO
Method
The following SELECT statement returns the start datetime, the end datetime, and a pseudorandom datetime with minute precision greater than or equal to the start datetime and less than the second datetime:
SELECT
StartDateTime,
EndDateTime,
DATEADD(
MINUTE,
ABS(CHECKSUM(NEWID())) % DATEDIFF(MINUTE, StartDateTime, EndDateTime) + DATEDIFF(MINUTE, 0, StartDateTime),
0
) AS RandomDateTime
FROM DateTimeRanges;
Result
Because the NEWID() function is nondeterministic, this will return a different result set for every execution. Here is the result set I generated just now:
StartDateTime EndDateTime RandomDateTime
----------------------- ----------------------- -----------------------
2012-07-09 00:30:00.000 2012-07-09 01:30:00.000 2012-07-09 00:44:00.000
2012-01-01 00:00:00.000 2013-01-01 00:00:00.000 2012-09-08 20:41:00.000
1988-07-25 22:30:00.000 2012-07-09 00:30:00.000 1996-01-05 23:48:00.000
All the values in the column RandomDateTime lie between the values in columns StartDateTime and EndDateTime.
Explanation
This technique for generating random values is due to Jeff Moden. He wrote a great article on SQL Server Central about data generation. Read it for a more thorough explanation. Registration is required, but it's well worth it.
The idea is to generate a random offset from the start datetime, and add the offset to the start datetime to get a new datetime in between the start datetime and the end datetime.
The expression DATEDIFF(MINUTE, StartDateTime, EndDateTime) represents the total number of minutes between the start datetime and the end datetime. The offset must be less than or equal to this value.
The expression ABS(CHECKSUM(NEWID())) generates an independent random positive integer for every row. The expression can have any value from 0 to 2,147,483,647. This expression mod the first expression gives a valid offset in minutes.
The epxression DATEDIFF(MINUTE, 0, StartDateTime) represents the total number of minutes between the start datetime and a reference datetime of 0, which is shorthand for '1900-01-01 00:00:00.000'. The value of the reference datetime does not matter, but it matters that the same reference date is used in the whole expression. Add this to the offset to get the total number of minutes between the reference datetime.
The ecapsulating DATEADD function converts this to a datetime value by adding the number of minutes produced by the previous expression to the reference datetime.
You can use RAND for this:
select cast(cast(RAND()*100000 as int) as datetime)
from here
Sql-Fiddle looks quite good: http://sqlfiddle.com/#!3/b9e44/2/0