SQL extracting hour and minute from date into a single column - sql

I have a column in the format YYYY-MM-DDBHH:MI:SS and I wish to extract just the hour and minutes from this into a column. Currently on the code they are using
Extract(HOUR From table) AS HR
, Extract(MINUTE From table) AS MN
but this outputs them into 2 columns. I'm new to SQL so is there an easier way to extract hour and minutes into one column in the format HH:MI.
Thanks

Using Teradata, simply concatenate the output from each of the functions to give the answer:
Extract(HOUR From table) || ':' || Extract(MINUTE From table) AS OUTPUT

Don't know about teradata but something like this.
SELECT CONCAT(Extract(HOUR From table),Extract(MINUTE From table)) as HRandMI

If you got a Timestamp the easiest way to get a HH:MI string is:
to_char(mycol, 'HH24:MI')

Related

How to extract only the hour from a timestamp in sql?

Hi I have data with column name 'trx_time'.
The data looks like this:
'2022-03-06 11:25:36'
I just want to extract the hour from the string. How can I do it?
I was thinking regex_extract. But don't know how to write the regex.
This will extract the Hour from a date field in Presto.
SELECT extract(HOUR FROM trx_time) as hour FROM table_name;
However, if your field is a String type representing a timestamp (ISO 8601), you would have to use the from_iso8601_date or from_iso8601_timestamp functions.
SELECT extract(HOUR FROM from_iso8601_date(trx_time)) as hour FROM table_name;

How can I do to get the hour from a date column with Postgres? [duplicate]

I have timestamp in my table and i want to extract only hour from it. I search and find a extract function but unable to use as a query. Do i need to convert first timestamp in varchar and then extract hour from it?
Here is my query:
select extract(hour from timestamp '2001-02-16 20:38:40') // example
actual query:
select extract(hour from timestamp observationtime) from smartvakt_device_report
The following should work
select extract(hour from observationtime) from smartvakt_device_report
SELECT to_char(now(), 'HH24:MI:SS') hour_minute_second
The word timezone is redundant (read: wrong). You just need to give the column's name. E.g.:
db=> select extract(hour from observationtime) from smartvakt_device_report;
date_part
-----------
19
(1 row)
EXTRACT does not work with Grafana but date_part does.
The solution for me was:
SELECT date_part('hour', observationtime::TIMESTAMP) FROM smartvakt_device_report;
Reference: https://www.postgresql.org/docs/current/functions-datetime.html#FUNCTIONS-DATETIME-EXTRACT

Subtract time using Oracle PL/SQL

I'm creating a report using BI Publisher. Now, I want is to subtract 4 hours in the date. For example, the date I get in the oracle database is below,
2019-09-23T10:09:34.054+00:00
Now I want it to return in report using sql is,
2019-09-23T06:09:34.054+00:00
How can I do that?
Thanks!
Use INTERVAL?
SELECT ts - INTERVAL '4' HOUR
FROM yourTable;
Demo
If your source data is actually text, and not a bona fide timestamp column, then you may use TO_TIMESTAMP_TZ to first do a conversion:
SELECT
TO_TIMESTAMP_TZ(REPLACE(s, 'T', ' ') 'YYYY-MM-DD HH24:MI:SS.FF3TZH:TZM') AS ts_original,
TO_TIMESTAMP_TZ(REPLACE(s, 'T', ' ') 'YYYY-MM-DD HH24:MI:SS.FF3TZH:TZM') - INTERVAL '4' HOUR AS ts_offset
FROM yourTable;
You just need to subtract a fraction of time. Like this for 4 hours ago:
select sysdate-1/6 from dual;

PSQL filter by timestamp field by minutes

I have a table that has a column called begin_time which is of type timestamp in a postgres db.
begin_time
------------------------
2014-06-02 10:00:00-04
2016-04-07 16:45:00-04
How do I filter based on the minute values in this table? I want the times where minutes = 45.
Thanks for your help!
You can use EXTRACT combined with WHERE.
SELECT * FROM your-table
WHERE EXTRACT(MINUTE FROM begin_time) = '45'
For more information about the arguments which can be used with EXTRACT see the official tutorial
You can use the EXTRACT function like this:
SELECT EXTRACT(MINUTE FROM SYSTIMESTAMP) AS CURRENT_MINUTE FROM DUAL;
visit the site http://mycodesoftware.com/ for more details.

How to write mysql TIMESTAMPDIFF function in Oracle sql query

I have query in Mysql which return minutes using TIMESTAMPDIFF in table. But now i have migrated my data to Oracle. So i want to use the same query to get the TIMESTAMPDIFF in a table in Oracle. Oracle also dont support NOW() function in mysql. The PROCESS_START_DATE column in query have data which contains date and time. I tried EXTRACT function in oraclebut did not work. Here is my query :
select * from(
select trunc(abs(to_date('27/01/2015 08:00:00','dd/mm/yyyy hh:mi:ss') - PMS.PROCESS_START_DATE)*24*60),PM.NAME,PM.ENABLED
from PROCESS_MONITOR_STATISTIC PMS
JOIN PROCESS_MONITOR PM ON PM.ID=PMS.PROCESS_MONITOR_ID
WHERE PM.ENABLED=1 AND PM.NAME= 'WORKFLOWENGINE1'
order by PMS.PROCESS_START_DATE desc
)
where ROWNUM = 1
You can do something like this:
--in case you are working with dates
select trunc(abs(to_date('26/01/2015 08:00:00','dd/mm/yyyy hh:mi:ss') - sysdate)*24*60) from dual;
This represent difference in minutes between a date and now(sysdate) with dates.
--timestamp case
select abs(
extract (day from diff)*24*60 + extract (hour from diff)*60 + extract (minute from diff)) from
(select to_timestamp('27/01/2015 09:07:00','dd/mm/yyyy hh:mi:ss') - systimestamp diff from dual);
This represent difference in minutes between a date and now(systimestamp) with timestamp.
Edit:
This query calculate minutes in a year:
select 365*24*60 from dual -- this returns 525600
This is your query. i change the time. Check that the difference between these dates is one year and five minutes
select trunc(abs((to_date('26/01/14 09:00:00','dd/mm/yy hh24:mi:ss')-
to_date('26/01/2015 09:05:01','dd/mm/yyyy hh24:mi:ss'))*24*60)) from dual;
So, when run this query result is 525605, five minutes more than a year. So it looks to be working.