How to add minutes from other column to timestamp column in Teradata SQL - teradata-sql-assistant

Hi, I'm trying to add Minutes field to Effective_from_date but couldn't in Teradata.
I have tried this
select (Effective_from_date + INTERVAL imit_in_minutes MINUTE) effective_to_date
below is working fine, but need to add the minutes dynamically
select (Effective_from_date + INTERVAL '30' MINUTE) effective_to_date

Related

Create a dynamic time range in sql

Is there a way to create a dynamic way to make a sql query that queries the data between the first day and the last day of every current month? I have a field called created and and I want to get the data where created is between the first day and the last day of the month.
Something like this will do it:
WHERE created BETWEEN DATE_FORMAT(Now(), '%Y-%M-01 00:00:00') AND DATE_FORMAT(DATE_ADD(DATE_ADD(Now(),INTERVAL 1 MONTH), INTERVAL -12 HOUR), '%Y-%M-%d 23:59:59')
It’s not pretty, but it gets the job done.
Is there a way to create a dynamic way to make a sql query that queries the data between the first day and the last day of every current month?
A simple method uses just date functions:
where created_at >= curdate() + interval (1 - day(curdate()) day and
created_at < (curdate() + interval (1 - day(curdate()) day) + interval 1 month
In addition to only using date arithmetic, this doesn't have an error of missing the last second of the month.

MySQL. Issue with: INTERVAL 60 MINUTES + last_run < NOW()

I am attempting to select a row from the database based on a time value.
My database contains a table of actions. These actions run ever x number of minutes. When the action is ran, the row is updated and it's last_run column which is a TIMESTAMP containing the value of NOW()
Here is a valid row in the database. As you can see from this row it was last run some days ago. It's set to run every 60 minutes.
I use INTERVAL last_run + interval_minutes < NOW(). So, the last run time, plus sixty minutes, would be less than the value of NOW() if 60 minutes has passed.
The query I am trying to run is below:
SELECT
*
FROM
routine_actions
WHERE
routine_actions.routine_id = 12
AND
INTERVAL routine_actions.interval_minutes MINUTE + routine_actions.last_run < NOW()
GROUP BY routine_id
But, I'm getting no results. Any idea's what I could be doing wrong?
The query you're trying looks ok but in the first part of your question, the following
INTERVAL last_run + interval_minutes MINUTES < NOW()
should be
INTERVAL interval_minutes MINUTES + last_run < NOW()

Better way to add time to a timestamp in MySQL

I like to add a 5 minutes to a timestamp:
SELECT timestamp+300 FROM table
works with minutes (5*60 = 300) But if I like to add a month or a year I can't calculate in seconds.
I came up with this query:
SELECT UNIX_TIMESTAMP(FROM_UNIXTIME(timestamp) + INTERVAL 5 MINUTE) FROM table
So I can go with years as well:
SELECT UNIX_TIMESTAMP(FROM_UNIXTIME(timestamp) + INTERVAL 2 YEAR) FROM table
Is this "the" solution or is there a more, elegant way?

Mysql time range query using extract

I'm trying to get an mysql query similar to date_trunc in psql.
Unfortunate Mysql do not have date_trunc function and I found I can use extract instead in Mysql.
What I want to do is write a script which i will run let say 10 minutes past each hour but I want to only select data from begin of an hour till end of this hour.
For example I will run script 12:10 and I want to display data from 11:00:00 till 11:59:59.
In PSQL query would look like that:
SELECT *
FROM data
WHERE time > ( date_trunc('hour',now()) - interval '1 hour' )
AND time <= ( date_trunc('hour',now()) ) ORDER BY time;
I was trying to use extract in similar fashion but I have no rows returned or error :/
Query below returns for example some narrowed data but it's like 2 hours each day from day one when database was started not last hour only:
SELECT *
FROM data
WHERE extract(hour from cr_date) between extract(hour from now()) - interval 1 hour)
AND extract(hour from now())
ORDER BY cr_date;
Any ideas how this can be achieved? or what I'm doing wrong in this query?
Hour is only an integer, so it's finding any matches between , for example, 9 and 10, regardless of the date.
I would recommend
select * FROM data
where cr_date >= date(now()) + INTERVAL hour(now())-1 HOUR
and cr_date <= date(now()) + INTERVAL hour(now()) HOUR
date(now()) returns midnight, and hour(now()) returns the number of hours since midnight
so, at 11:10 am, it should result in a results between midnight + 10 hours (10 am) and midnight + 11 hours (11 am)

MySQL select a date between two dates using the time now

I am trying to select a row from a table assuming that the kick off time is within an hour's range of the current time.
The table is just an id and a datetime field.
SELECT * FROM kick_offs WHERE NOW() BETWEEN (DATE_SUB(`time`, INTERVAL 30 MINUTES)) AND (DATE_ADD(`time`, INTERVAL 30 MINUTES))
SELECT * FROM kick_offs WHERE `time` BETWEEN (DATE_SUB(NOW(), INTERVAL 30 MINUTES)) AND (DATE_ADD(NOW(), INTERVAL 30 MINUTES))
These two queries both fail. I'm not really sure why. The server is running MySQL 5.0. What am I doing wrong?
Not sure where you got your SQL from, but this should do it I think.
SELECT * FROM kick_offs WHERE `time` < DATE_ADD(NOW(), INTERVAL 1 HOUR) AND `time`>= NOW()
See:
Display rows from MySQL where a datetime is within the next hour