Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to substract/add minutes from a timestamp in postgreSQL

Tags:

sql

postgresql

I have the following scenario:

I have employees who register their check in/out from their work. But they have 10 minutes of tolerance.

The late entries I get with this view:

CREATE OR REPLACE VIEW employees_late_entries
(
  id,
  created_datetime,
  entry_datetime,
  contact_id,
  contact_name,
  user_id,
  employees_perm_id
)
AS 
 SELECT precence_records.id,
    precence_records.created AS created_datetime,
    ("substring"(precence_records.created::text, 0, 11) || ' '::text) || contacts.entry_time::text AS entry_datetime,
    contacts.id AS contact_id,
    contacts.name AS contact_name,
    precence_records.user_id,
    precence_records.employees_perm_id
   FROM precence_records,
    contacts
  WHERE 
    precence_records.type::text = 'entry'::text AND 
    contacts.employee = true AND 
    contacts.id = precence_records.contact_id AND 
    ( ("substring"(precence_records.created::text, 0, 11) || ' '::text) || contacts.entry_time::text) < precence_records.created::text AND 
    precence_records.employees_perm_id IS NULL;

the precence_records.created is the check in time and contacts.entry_time its the time of the schedule entry time for the employee.

This is the condition contacts.entry_time vs precence_records.created to get the late entries:

 ( ("substring"(precence_records.created::text, 0, 11) || ' '::text) || contacts.entry_time::text) < precence_records.created::text

So I wanna do something like that:

 (  ("substring"(precence_records.created::text, 0, 11) || ' '::text) || (contacts.entry_time::text + 10 MINUTES)  ) < precence_records.created::text

DATA TYPES:

precence_records.created TIMESTAMP contacts.entry_time VARCHAR

Can you help me please

like image 393
juanpscotto Avatar asked May 11 '16 21:05

juanpscotto


People also ask

How do I subtract minutes from a time in PostgreSQL?

In PostgreSQL, we can use the - operator to subtract one or more minutes from a time value. By “time” value, this could be an actual time value, a timestamp, or an interval. We can also subtract minutes from a date value or a date and time combination.

How to select current date and time in PostgreSQL?

Basically, timestamp data type divided into types with and without a time zone. Below is the function of the timestamp data type is as follows. Now function is used to select the current date and time in PostgreSQL.

How to use timestamptz in PostgreSQL?

Note: PostgreSQL performs the timestamptz indirectly if we provide the timestamp as a string to the timezone () function. To cast a timestamp value to the timestamptz data type directly as it is always an excellent approach, as shown in the below command:

Is it possible to add/subtract an interval value in PostgreSQL?

Yes, but what is the format of the values in there (and again: why varchar ?) Dates, Times and Timestamps in PostgreSQL can be added/subtracted an INTERVAL value: If your timestamp field is varchar, you can cast it first to timestamp data type and then subtract the interval:


1 Answers

Dates, Times and Timestamps in PostgreSQL can be added/subtracted an INTERVAL value:

SELECT now()::time - INTERVAL '10 min'

If your timestamp field is varchar, you can cast it first to timestamp data type and then subtract the interval:

 ( (left(precence_records.created::text, 11) || ' ') ||
   (contacts.entry_time::time + INTERVAL '10min')::text )::timestamp <
 precence_records.created::timestamp
like image 127
Ezequiel Tolnay Avatar answered Oct 11 '22 18:10

Ezequiel Tolnay