Home > front end >  How do I pass two ranges to DateAdd?
How do I pass two ranges to DateAdd?

Time:07-12

Hello I'm developing a script that checks dates based on a time range.

POS_DATAPOSICAO BETWEEN @data AND DATEADD(MINUTE,3, @data)

In this example I am passing an interval of 3 minutes more.

How do I put 3 minutes more and 3 minutes less on the same DATEADD?

CodePudding user response:

Not sure what SQL version you're in, as you didn't say

  1. Function in MySQL is not "dateadd()" but "adddate()" which is a synonym for DATE_ADD().

  2. Format in MySQL is "@data, INTERVAL 3 MINUTE" rather than "MINUTE, 3, @data"

  3. your first paramater just needs to be altered for the start like you altered the second parameter for the end:

    POS_DATAPOSICAO BETWEEN DATE_SUB(@data, INTERVAL 3 MINUTE) AND DATE_ADD(@data, INTERVAL 3 MINUTE)

CodePudding user response:

You can use the smaller date as first Argument like so:

POS_DATAPOSICAO BETWEEN DATEADD(MINUTE, -3, @data) AND DATEADD(MINUTE, 3, @data)

I think this might solve your problem. You can find further information at https://docs.microsoft.com/en-us/sql/t-sql/functions/dateadd-transact-sql?view=sql-server-ver16 or https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_date-add

  • Related