I can't seem to figure this out and I've been at it for sometime now any help would be great thank you.
I have to prepare a simple procedure accepting one parameter, a date value, and using date addition functions in SQL (MySql if possible) return the parameter value minus 1 day
CREATE PROCEDURE get_date
IS
BEGIN
DATE_ADD(date,INTERVAL expr type)
END;
/
EXEC get_date
This was all I could come up with so far. Any help would be greatly appreciated.
That question is not just about select statement (ie, computing minus one day); I need help with a SQL procedure.
Maybe just create a function
CREATE FUNCTION simplefunction (s datetime)
RETURNS datetime DETERMINISTIC
RETURN DATE_SUB(s, INTERVAL 1 DAY);
SELECT simplefunction (anydate);
Related
I am calling a stored procedure in a loop in another stored procedure, But Inner SP runs only the first time. No error is shown there, I can't seem to find the reason, Any help would be highly appreciated
DELIMITER $$
USE `xxx_db`$$
DROP PROCEDURE IF EXISTS `wrapper`$$
CREATE DEFINER=`root`#`%` PROCEDURE `wrapper`(p_StartPeriod DATE, p_EndPeriod DATE)
BEGIN
DECLARE v_startperiod DATE;
DECLARE v_endperiod DATE;
DECLARE v_nextdate DATE;
SET v_startperiod = p_StartPeriod;
SET v_endperiod = p_EndPeriod;
SET #TempStart = v_startperiod;
WHILE (#TempStart < v_endperiod) DO
SET v_nextdate = DATE_ADD(#TempStart, INTERVAL 1 DAY);
call innersp(#TempStart,v_nextdate);
SET #TempStart = DATE_ADD(#TempStart, INTERVAL 1 DAY);
END WHILE;
END$$
DELIMITER ;
In this code, The date arguments are passed to the inner SP, if a date range is given, Inner SP runs only for the first date coming in loop. Please help me to find the issue. The inner SP works fine when it is called alone, It inserts data into a table, but since the number of rows are huge I want to insert on a per day basis, instead of doing the complete date range. It's arguments are dates. That is why I wrote a wrapper SP to call this inner SP giving arguments per day for the complete date range.
I am in desperate need of some help!
I am currently working on a project for SQL in a course I am doing (Due on Friday)
One of the questions is to create a stored procedure which shows dates of sales_orders within a certain range.
I have created the following stored procedure for this question, but all I am getting is blank rows? From what I can tell it seems to be reading the dates wrong, but I have no idea why? Can anyone please help? I am a newbie at SQL.
DELIMITER //
CREATE PROCEDURE customer_order_range (start_date DATE, end_date DATE)
BEGIN
SELECT *
FROM customer_order
WHERE `date` >= start_date
AND `date` <= end_date;
END //
DELIMITER ;
CALL customer_order_range ( 'start_date' = '2020-02-01', 'end_date' = '2020-03-05');
It returns blank rows. My customer_order table has a date column, which is stored as a DATE value.
Any help would be super appreciated!
Conor
I've got it!
This piece of the code is wrong:
CALL customer_order_range ( 'start_date' = '2020-02-01', 'end_date' = '2020-03-05');
It should instead be:
CALL customer_order_range ('2020-02-01','2020-03-05');
The problem arose when I put the parameter names in with their values when calling the stored procedure. Thanks everyone! :)
Remove the delimiters and the columns names like below :
CALL customer_order_range ('2020-02-01', '2020-03-05');
I have a function that calculate time difference excluding weekends, and I have created a stored procedure to call the function. When I tested the sp, only showed message part, I am new and not sure if this is correct. And I have trouble to pass the parameters to SSRS. Please help, many thanks.
Alter Proc Test_1
#DownTime datetime,
#Uptime datetime =null --since there are some null value in uptime column
AS
Begin
print 'enter'
declare #duration int
select #duration = [dbo].[CalcWorkMinutes](#DownTime,#Uptime)
print 'abc'
;with cte_test2
as (
SELECT DownTime
,UpTime
,datediff(hh,DownTime,UpTime) AS duration
FROM Test
)
select DownTime,UpTime,duration from cte_test2
where DownTime = #DownTime and UpTime=#UpTime
and duration=#duration
--print 'pass'
print 'duration'+' '+convert(varchar,#duration)
RETURN
END
--execute [dbo].[test_1] #downtime ='2017-06-02 09:00:00.000', #Uptime ='2017-06-05 09:00:00.000'
Hmm... There's a lot going on in this one. It looks like you are trying to see what downtime was like across a certain timeframe someone will pass in? If that is the case, I'd change the parameters to be: StartDate and EndDate. You don't really need a CTE to apply the parameters... Code below... I am just taking a guess here though:
Alter Proc Test_1
#startdate datetime,
#enddate datetime --since there are some null value in uptime column
AS
Begin
print 'enter'
SELECT
DownTime
,UpTime
,[dbo].[CalcWorkMinutes](DownTime,UpTime) AS duration
FROM Test as a
where
downtime >= #startdate
and b.downtime < dateadd(day,1,#enddate)
RETURN
END
I have already been doing some function in postgresDB, but im completly new to MySQL. I thought that it will be almost same to create procedure in MySQL, but aparently it is not. So I am asking for some help :)
In postgres I would write this function for what I need to accomplish:
DROP PROCEDURE IF EXISTS report_incomes;
DELIMITER //
CREATE FUNCTION reportincomes(IN interval INT)
RETURN report_tab TABLE
(
year_month DATE,
total DECIMAL(30,2),
total_before DECIMAL(30,2)
)
AS
BEGIN
FOR row_d IN SELECT * FROM intervals_generator('2013-01-01 00:00:00', now(), interval) LOOP
SELECT sum(cash) AS total, DATE_FORMAT(datetime, '%b %Y') AS year_month FROM incomes WHERE datetime BETWEEN row_d.start_d AND row_d.end_d//
SELECT sum(cash) AS total_before FROM incomes WHERE datetime BETWEEN DATE_SUB(row_d.start_d, INTERVAL 1 YEAR) AND DATE_SUB(row_d.end_d, INTERVAL 1 YEAR)//
END LOOP//
END//
DELIMITER ;
As you may noticed, the big part of code is already switched to MySQL, but I am kind of stuck with FOR LOOP, as it looks so different in MySQL compared to Postgres.
Of course there will be much more code added inside the LOOP, I just did not wanted to make it too complicated for explanation.
Could please anyone help me with the FOR LOOP?
Basicly, I want to execute these SELECTs for each row returned by function that generates my custom intervals, while using data from that row inside of LOOP
Basically I am trying to convert a date field from my database into a number of seconds old. I used UNIX_TIMESTAMP on our old MySQL database and am looking for an equivalent function in PostgreSQL.
Thanks in advance
SELECT extract(epoch FROM your_datetime_column)
FROM your_table
More details in the manual:
http://www.postgresql.org/docs/current/static/functions-datetime.html#FUNCTIONS-DATETIME-EXTRACT
This:
select CURRENT_TIMESTAMP - '1970-01-01';
Will give you an interval type. You can get seconds out of that interval result if you need it.
I am trying to convince existing MySQL queries to work, this is what I have, seem to be working fine to me.
CREATE OR REPLACE FUNCTION UNIX_TIMESTAMP(date_field timestamp with time zone) RETURNS integer
AS $$
BEGIN
RETURN extract(epoch FROM date_field);
END;
$$ LANGUAGE plpgsql;