How do I assign DateTime field to a variable - mysql

I want to assign value of a query to a datetime variable in MySql Stored Procedure.
I'm trying this -
DECLARE myDate DATETIME;
SET myDate = (SELECT date1 FROM myTable WHERE Id = var_myId);
And this
DECLARE myDate DATETIME;
SELECT date1 into myDate FROM myTable WHERE Id = var_myId;
Both don't seem to work as I am not getting the desired result after running the proc.
EDIT
Problem is in this statement -
initial_Date DATETIME; -- param1
interval INTEGER; -- param2
SET var_date2 = DATE_ADD(initial_Date , INTERVAL interval MINUTE);
When I select var_date2 I get null as result.

I find two issue with your stored procedure.
First, you are naming a variable using an sql key word interval. You should rename with an word which is not an sql key word.
The other is you are not setting an out put variable. You can see the MySQL tutorials on stored procedure.
You can use and try the code below:
delimiter //
create procedure test(in initial_time datetime, in minuteInterval integer(2),
out final_time datetime)
begin
set final_time = date_add(initial_time, interval minuteInterval minute);
end//
delimiter ;
For testing you can try:
call test(now(), 60, #final_time);
select #final_time;
Here is the screenshot of my test with mysql 5.5.21:

Related

How to create a Stored Procedure with Optional DateTime Parameters in mysql

So the parameters in the stored procedure are optional, meaning they can be null. The issue with my cade is that there is no output whenever I call the SP. I need my params to be able to accept null as well
CREATE PROCEDURE `GET`(
in startDate DATETIME,
in endDate DATETIME
)
BEGIN
SELECT * FROM DB.table
WHERE
DB.table.colunm >= startDate
AND
DB.table.colunm <= endDate;
END
when I call the stored procedure, I never get any result
call GET(2022-05-28, 2022-05-30)
call GET(null, null)
If you want to retrieve all records when you pass the null value, you can use the IF... ELSE... Statement to determine what your Stored Procedure will select.
Check the following SP:
CREATE PROCEDURE GETDT(
in startDate DATETIME,
in endDate DATETIME
)
BEGIN
if startDate is null or endDate is null then
SELECT * FROM test;
else
SELECT * FROM test
WHERE
test.sdate >= startDate
AND
test.edate <= endDate;
end if;
END
See the result from db-fiddle.

MySQL Stored Procedures - Adding integer variable to date returns null

I am writing my first mysql procedure. I have a date variable and 2 integer variables. I am multiplying the two integer variables and adding the resultant integer to the date variable.
For example:
pint=6;
noftimes=3
sdate=2020-05-05
totDays=pint*noftimes
edate=2020-05-05+totDays
I am able to multiple pint*noftimes but not add sdate+totDays. Whereas If I add sdate+10 then I am getting a correct incremental date value. The following is my procedure:
DELIMITER $$
CREATE DEFINER=`pattu`#`localhost` PROCEDURE `getFieldWorkDates`(IN `p_id` INT, OUT `totDays` INT, OUT `edate` DATE)
BEGIN
DECLARE noftimes int;
DECLARE pint int;
DECLARE sdate DATE;
DECLARE tdays int;
SELECT startDate into sdate from projects where idprojects=p_id;
SELECT projectDuration into noftimes from projects where idprojects=p_id;
SELECT recFreq into pint from projects where idprojects=p_id;
SET totDays=pint*noftimes;
SET edate = sdate+(pint*noftimes);
END$$
DELIMITER ;
When I execute this, I am getting the message, your query has been executed successfully. 0 rows affected
SET edate = sdate+(pint*noftimes);
You cannot add integer to date. Use DATE_ADD() function.
CREATE
DEFINER=pattu#localhost
PROCEDURE getFieldWorkDates ( IN p_id INT,
OUT totDays INT,
OUT edate DATE)
SELECT recFreq * projectDuration,
startDate + INTERVAL recFreq * projectDuration DAY
INTO totDays,
edate
FROM projects
WHERE idprojects=p_id;

MySQL procedure to update a price based on a date change

I am trying to write a procedure that update the the content in one column, based a change in date is more than 20 days. In context, the procedure decreases the price of a good if the time passed since acquired is more than 20 days.
I am using the DATEDIFF function as well as GETDATE() statement but I am really strugling. At this point I am stucked.
Can anybody tell what is wrong with my sql code?
DELIMITER //
CREATE PROCEDURE UPDATE_PRICE
DECLARE #TIMEPASSED AS TIME
SET #TIMEPASSED = GETDATE()
BEGIN
UPDATE sales SET sales.SalesPrice = sales.SalesPrice - 30
WHERE DATEDIFF(#GETDATE(), Sales.AcquisitionDate;
END;;
Any help on this one would be much appreciated
Variable declarations should be in the BEGIN/END block, but they are not really needed in this case. GETDATE() is not a MySQL function. So:
DELIMITER //
CREATE PROCEDURE UPDATE_PRICE
BEGIN
UPDATE sales s
SET s.SalesPrice = s.SalesPrice - 30
WHERE s.AcquisitionDate < CURDATE() - INTERVAL 20 day;
END;
DELIMITER ;
Try this:
DELIMITER //
CREATE PROCEDURE UPDATE_PRICE
DECLARE #TIMEPASSED AS TIME
SET #TIMEPASSED = GETDATE()
BEGIN
UPDATE sales SET sales.SalesPrice = sales.SalesPrice - 30
WHERE DATEDIFF(#GETDATE(), Sales.AcquisitionDate) > 20;
END;

Create a loop based on date Mysql

I have a query :
insert into fookoo_business
select stat_date, sum(spend), sum(revenue)
from hooloo_business;
that i want to run for each date from '2017-01-20' until yesterday (it means the query will run 434 times if we're at 01/04/2018), for each date separately
(in a loop).
how can i create a loop in Mysql to do it for me?
I have tried:
creating procedure for the query select #stat_date, sum(spend), sum(revenue)
I called 'query'
then :
CREATE PROCEDURE loop_procedure()
BEGIN
SET #stat_date='2018-03-20';
CALL 'query';
REPEAT
SET #stat_date = #stat_date + INTERVAL 1 DAY;
UNTIL #stat_date = CURDATE() END REPEAT;
END
eventually i've used the following logic within a stored procedure to fetch the data:
PROCEDURE `x_monitoring_loop`()
BEGIN
DECLARE i INT;
DECLARE len INT;
SET len = 434;
SET i = 0;
WHILE (i < len) DO
SET #stat_date= CURDATE()-INTERVAL 1 DAY;
SET #stat_date= #stat_date- INTERVAL i DAY;
Insert query;
SET i = i +1;
END WHILE;
This way the query ran 434 times for each day, beginning at current date - 1 day.
I do not know why you want to use a procedure,I think we can just use a query sql to do it:
INSERT INTO fookoo_business
SELECT stat_date, SUM(spend), SUM(revenue)
FROM hooloo_business
WHERE stat_date BETWEEN STR_TO_DATE('2017-01-02', '%Y-%m-%d') -- start date
AND DATE_SUB(NOW(), INTERVAL 1 DAY) -- end date
GROUP BY stat_date;

conditional interval in stored procedure

I would like to change the interval in this SQL statement, based on a parameter in a stored procedure. I want to use three different intervals: 1 day, 8 hours, 1 hour
CREATE DEFINER= 'dbshizzle' PROCEDURE `getData`(in sD text(17), in sT text(8))
BEGIN
select stime, sval
from tblNumber
where sDix = 'allright'
and timestamp >= now() - interval 1 day
order by timestamp;
END
Should I use an IF statement with an integer parameter, or a text parameter?
How about just adjusting the parameter and passing in the value as hours?
CREATE DEFINER = 'dbshizzle' PROCEDURE `getData`(
in in_sD text(17), -- should change to varchar
in in_sT text(8), -- should change to varchar
in in_hours int
)
BEGIN
select stime, sval
from tblNumber
where sDix = 'allright'
and timestamp >= now() - interval in_hours hour
order by timestamp;
END;