add two minutes to a date with time

1

I am doing a query that brings me the records that were updated today with equal time, minutes and seconds How can I make a select so that there is a range of two minutes before and after the date and current time? since I work with a cycle for and within the cycle I place a ajax , then if 10 records are sent the time no longer matches and only brings me 5 since they passed milliseconds and it is not the same time, my query is, knowing that the variable date is DateTime.Now().ToString("yyyy-MM-dd hh:mm:ss")

SELECT * FROM empresa WHERE CAST(F_ENVIO_TEL AS DATETIME2)=CAST('" & fecha & "' AS DATETIME2)
    
asked by Ivxn 01.07.2016 в 17:20
source

1 answer

3

To be able to subtract or add; minutes, seconds, hours in Dates and Hours you can use: INTERVAL , here is an example:

Subtracting Intervals

SELECT NOW() - INTERVAL 4 SECOND;
  >_ 2016-07-01 10:56:16

SELECT NOW() - INTERVAL 2 MINUTE;
  >_ 2016-07-01 10:54:20

SELECT NOW() - INTERVAL 3 HOUR;
  >_ 2016-07-01 07:56:20

SELECT NOW() - INTERVAL 1 DAY;
  >_ 2016-06-30 10:56:20

Adding Intervals

SELECT NOW() + INTERVAL 4 SECOND;
  >_ 2016-07-01 10:56:24

SELECT NOW() + INTERVAL 2 MINUTE;
  >_ 2016-07-01 10:58:20

SELECT NOW() + INTERVAL 3 HOUR;
  >_ 2016-07-01 13:56:20

SELECT NOW() + INTERVAL 1 DAY;
  >_ 2016-07-02 10:56:20

Afterwards you can then consult between dates with the use of BETWEEN .

Example:

 SELECT 'venta_realizada' FROM 'ventas' WHERE 'venta_realizada' BETWEEN ('2016-02-23 17:33:57' - INTERVAL 5 HOUR) AND ('2016-02-23 17:33:57' + INTERVAL 5 HOUR)
   >_ 2016-02-23 17:44:12
   >_ 2016-02-23 17:33:57
   >_ 2016-02-23 17:36:51
    
answered by 01.07.2016 / 18:09
source