I am trying to convert hours to minutes using Stored Procedures in MySQL. For example,I am getting the output as 3 hour 15 minutes . I need to convert it as 195 minutes. I tried using multiple cases but nothing worked out for me.. Can you please help me if you have any code to solve this???
You could use Function for this purpose.
DELIMITER $$
CREATE FUNCTION `toMinutes`
(
hour INT,
minute INT
)
RETURNS INTEGER
BEGIN
DECLARE result INT;
SET result = (hour * 60) + minute;
RETURN result;
END$$
DELIMITER ;
Call the function:
SELECT toMinutes(1,1);
I worked on the requirement and able to create a function successfully as follows!!!
/****** Object: UserDefinedFunction [dbo].[BUSINESS_DURATION_MINTS_FN] Script Date: 10/17/2017 12:22:12 PM ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER FUNCTION [dbo].[BUSINESS_DURATION_MINTS_FN]
(#string varchar(100) )
RETURNS INT
AS
BEGIN
DECLARE #total_mints INT
DECLARE #hours INT
DECLARE #Mints INT
IF #string IS NOT NULL
BEGIN
SET #string = REPLACE(UPPER(#string),'HOURS','HOUR')
SET #string = REPLACE(UPPER(#string),'MINUTES','MINUTE')
IF ((CHARINDEX(UPPER('HOUR'),UPPER(#string))!=0) AND (CHARINDEX(UPPER('MINUTE'),UPPER(#string))!= 0))
BEGIN
SET #hours = CONVERT(INT,SUBSTRING(#string,1, (CHARINDEX(UPPER('HOUR'), UPPER(#string))-2)))
SET #Mints = CONVERT(INT,SUBSTRING(#string,(CHARINDEX(UPPER('HOUR'),UPPER(#string))+4),(CHARINDEX(UPPER('MINUTE'),UPPER(#string))-(CHARINDEX(UPPER('HOUR'), UPPER(#string))+5))))
END
IF ((CHARINDEX(UPPER('HOUR'),UPPER(#string))=0) AND (CHARINDEX(UPPER('MINUTE'),UPPER(#string))!= 0))
BEGIN
SET #hours = 0
SET #Mints = CONVERT(INT,SUBSTRING(#string,1, (CHARINDEX(UPPER('MINUTE'), UPPER(#string))-2)))
END
IF ((CHARINDEX(UPPER('HOUR'),UPPER(#string))!=0) AND (CHARINDEX(UPPER('MINUTE'),UPPER(#string))=0))
BEGIN
SET #hours = CONVERT(INT,SUBSTRING(#string,1, (CHARINDEX(UPPER('HOUR'), UPPER(#string))-2)))
SET #Mints = 0
END
IF ((CHARINDEX(UPPER('HOUR'),UPPER(#string))= 0) AND (CHARINDEX(UPPER('MINUTE'),UPPER(#string))=0))
BEGIN
SET #hours = 0
SET #Mints = 0
END
SET #total_mints = #hours*60 + #Mints
END
IF #string IS NULL
BEGIN
SET #total_mints = NULL
END
RETURN #total_mints
END
GO
Related
I have created a function on a MS SQL Server 2000 database that converts a base 10 number to base 64 and works fine for my purposes. I also need this function to be in a MySQL database I have converted it however it throws an exception saying that
Truncated double value 'B'
for example an example, now if I keep the number below 64 it converts it fine.
SQL function
CREATE FUNCTION ToBase64(#value int)
RETURNS varchar(50)
AS
BEGIN
DECLARE #seq char(64)
DECLARE #result varchar(50)
DECLARE #digit char(1)
SET #seq = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
SET #result = SUBSTRING(#seq, (#value%64)+1, 1)
WHILE #value > 0
BEGIN
SET #digit = SUBSTRING(#seq, ((#value/64)%64)+1, 1)
SET #value = #value/64
IF #value <> 0 SET #result = #digit + #result
END
RETURN #result
END
GO
mySQL function
DELIMITER $$
CREATE FUNCTION ToBase64( Pvalue int) RETURNS varchar(50)
DETERMINISTIC
BEGIN
DECLARE seq char(64);
DECLARE result varchar(50);
DECLARE digit char(1);
SET seq = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
SET result = SUBSTRING(seq, (Pvalue%64)+1, 1);
WHILE Pvalue > 0 do
SET digit = SUBSTRING(seq, ((Pvalue/64)%64)+1, 1);
SET Pvalue = Pvalue/64;
IF Pvalue <> 0 THEN
SET result = digit + result;
END IF;
End While;
RETURN (result);
END
This may be your problem:
IF Pvalue <> 0 THEN
SET result = digit + result;
END IF;
MySQL uses CONCAT() for string concatenation:
IF Pvalue <> 0 THEN
SET result = CONCAT(digit, result);
END IF;
I need to convert this tSQL query into MySQL query version 5.7.14
I tried declare parameter with the following syntax
declare #commissionType INT;
but I reached a dead end and need help to convert this query
CREATE PROCEDURE MyProc
#chainId int
as
BEGIN
declare #commissionMethod int
declare #commissionType int
declare #value decimal(18,2)
set #commissionMethod = 1
set #value = 200000
set #commissionType = (select CommissionType from CommissionRules
where ChainId = #chainId )
IF(#commissionMethod = 1)
BEGIN
print('alert')
select (Fixed * #value) / 100 from CommissionRules where ChainId = #chainId
END
END
This is pretty much a literal conversion. But check your logic. It doesn't make any sense. v_commissionMethod is always 1 and v_commissionType is not even being used after selecting a value.
DELIMITER //
CREATE PROCEDURE MyProc (
p_chainId int)
BEGIN
declare v_commissionMethod int;
declare v_commissionType int;
declare v_value decimal(18,2);
set v_commissionMethod = 1;
set v_value = 200000;
select CommissionType into v_commissionType from CommissionRules
where ChainId = p_chainId ;
IF(v_commissionMethod = 1)
THEN
/* print('alert') */
select (Fixed * v_value) / 100 from CommissionRules where ChainId = p_chainId;
END IF;
END;
//
DELIMITER ;
Something like this:
DELIMITER $$
CREATE PROCEDURE MyProc (
in_chainId unsigned
) as
BEGIN
select v_CommissionType := CommissionType,
v_commissionMethod = 1,
v_value = 200000
from CommissionRules
where ChainId = in_chainId;
if v_commissionMethod = 1 then
select 'alert';
select (Fixed * v_value) / 100
from CommissionRules
where ChainId = in_chainId
end if;
END;$$
DELIMITER ;
Here is the error I get when I try to run the code below.
I spent a day on mySQL tutorials + forums without any success.
DROP PROCEDURE IF EXISTS update_end_date_dwh_dimension_product;
delimiter $$
CREATE PROCEDURE update_end_date_dwh_dimension_product()
BEGIN
DROP TEMPORARY TABLE IF EXISTS __temp_BI__;
CREATE TEMPORARY TABLE __temp_BI__
SELECT dp.sku, dp.start_date
FROM dwh_dimension_product dp
ORDER BY dp.sku, dp.start_date;
DECLARE end_loop BOOLEAN DEFAULT TRUE;
DECLARE sku1_ VARCHAR(50);
DECLARE sku2_ VARCHAR(50);
DECLARE start_date1_ TIMESTAMP;
DECLARE start_date2_ TIMESTAMP;
DECLARE cursor_temp CURSOR
FOR select * from __temp_BI__;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET end_loop = FALSE;
OPEN cursor_temp;
FETCH cursor_temp INTO sku1_, start_date1_;
while_label: WHILE (end_loop) DO
IF end_loop = FALSE THEN
LEAVE while_label;
END IF;
FETCH cursor_temp INTO sku2_, start_date2_;
while_label2: WHILE (sku1_ = sku2_) DO
UPDATE dwh_dimension_product dp1
SET dp1.end_date = DATE_SUB(start_date2_, INTERVAL 1 SECOND)
WHERE dp1.sku = sku1_
AND dp1.start_date = start_date1_;
SET sku1_ = sku2_;
SET start_date1_ = start_date2_;
FETCH cursor_temp INTO sku2_, start_date2_;
IF end_loop = FALSE THEN
LEAVE while_label2;
END IF;
END WHILE;
UPDATE dwh_dimension_product dp2
SET dp2.end_date = NULL
WHERE dp2.sku = sku1_
AND dp2.start_date = start_date1_;
SET sku1_ = sku2_;
SET start_date1_ = start_date2_;
END WHILE;
CLOSE cursor_temp;
DROP TABLE IF EXISTS __temp_BI__;
END $$
delimiter ;
If I put this following code outside the procedure() it works
DROP TEMPORARY TABLE IF EXISTS __temp_BI__;
CREATE TEMPORARY TABLE __temp_BI__
SELECT dp.sku, dp.start_date
FROM dwh_dimension_product dp
ORDER BY dp.sku, dp.start_date;
Thank you for your advice.
I have a MySQL variable as below.
DECLARE str TEXT DEFAULT '2014-01-02 13:00:00|2014-02-04 12:59:59#0#2014-02-04 13:00:00|2014-03-04 12:59:59#0#2014-03-04 13:00:00|2014-04-02 13:59:59#0#2014-04-02 14:00:00|2014-05-02 14:59:59#0#2014-05-02 15:00:00|2014-06-03 14:59:59';
I want to break this whole string first by using the separator #0# and from the results break the string using separator |.
I have tried MySQL split_str function but I am not able to do it.
Its giving me the error split_str does not exist.
Please suggest some other way to do this.
Finally i have resolved my problem using below procedure.
I am able to solve it using temporary table and procedure. I am no more using mysql variable for this. We can call the procedure as below....
CALL SplitString('2014-01-02 13:00:00|2014-02-04 12:59:59#0#2014-02-04 13:00:00|2014-03-04 12:59:59#0#2014-03-04 13:00:00|2014-04-02 13:59:59#0#2014-04-02 14:00:00|2014-05-02 14:59:59#0#2014-05-02 15:00:00|2014-06-03 14:59:59', '#0#', 'tblindex');
Here tblindex is temporary table i have used.
My procedure is below....
DELIMITER $$
DROP PROCEDURE IF EXISTS `SplitString`$$
CREATE PROCEDURE `SplitString`( IN input TEXT,IN delm VARCHAR(10),tblnm varchar(50))
BEGIN
DECLARE cur_position INT DEFAULT 1 ;
DECLARE remainder TEXT;
DECLARE cur_string VARCHAR(1000);
DECLARE delm_length TINYINT UNSIGNED;
set #sql_drop = concat("DROP TABLE IF EXISTS ",tblnm);
prepare st_drop from #sql_drop;
execute st_drop;
set #sql_create = concat("CREATE TEMPORARY TABLE ",tblnm," (value VARCHAR(2000) NOT NULL ) ENGINE=MEMORY;");
prepare st_create from #sql_create;
execute st_create;
SET remainder = input;
SET delm_length = CHAR_LENGTH(delm);
WHILE CHAR_LENGTH(remainder) > 0 AND cur_position > 0
DO
SET cur_position = INSTR(remainder, delm);
IF cur_position = 0 THEN
SET cur_string = remainder;
ELSE
SET cur_string = LEFT(remainder, cur_position - 1);
END IF;
-- select cur_string;
IF TRIM(cur_string) != '' THEN
set #sql_insert = concat("INSERT INTO ",tblnm," VALUES ('",cur_string,"');");
prepare st_insert from #sql_insert;
execute st_insert;
END IF;
SET remainder = SUBSTRING(remainder, cur_position + delm_length);
END WHILE;
END$$
DELIMITER ;
I am trying to create a search function in mysql. In order to make the search result more reliability i need to compare two string by words. Inputs are 2 strings and output is number word two strings match. In MySql i did as below.
CREATE DEFINER=`root`#`localhost` FUNCTION `CompareStrings`(str1 VARCHAR(255),str2 VARCHAR(255)) RETURNS double
BEGIN
DECLARE cur_position INT DEFAULT 1 ;
DECLARE remainder TEXT;
DECLARE cur_string VARCHAR(50);
DECLARE delimiter_length TINYINT UNSIGNED;
DECLARE numberMatch INT;
DECLARE total INT;
DECLARE result DOUBLE DEFAULT 0;
DECLARE delim VARCHAR(10);
DECLARE string2 VARCHAR(255);
SET delim = ' ';
DROP TEMPORARY TABLE IF EXISTS SplitString1;
CREATE TEMPORARY TABLE SplitString1 (
SplitString1ID INT NOT NULL PRIMARY KEY AUTO_INCREMENT ,
val VARCHAR(50) NOT NULL
) ENGINE=MyISAM;
DROP TEMPORARY TABLE IF EXISTS SplitString2;
CREATE TEMPORARY TABLE SplitString2 (
SplitString1ID INT NOT NULL PRIMARY KEY AUTO_INCREMENT ,
val VARCHAR(50) NOT NULL
) ENGINE=MyISAM;
SET remainder = str1;
SET delimiter_length = CHAR_LENGTH(delim);
WHILE CHAR_LENGTH(remainder) > 0 AND cur_position > 0 DO
SET cur_position = INSTR(remainder, delim);
IF cur_position = 0 THEN
SET cur_string = remainder;
ELSE
SET cur_string = LEFT(remainder, cur_position - 1);
END IF;
IF TRIM(cur_string) != '' THEN
INSERT INTO SplitString1(val) VALUES (cur_string);
END IF;
SET remainder = SUBSTRING(remainder, cur_position + delimiter_length);
END WHILE;
SET remainder = str2;
SET cur_position = 1;
WHILE CHAR_LENGTH(remainder) > 0 AND cur_position > 0 DO
SET cur_position = INSTR(remainder, delim);
IF cur_position = 0 THEN
SET cur_string = remainder;
ELSE
SET cur_string = LEFT(remainder, cur_position - 1);
END IF;
IF TRIM(cur_string) != '' THEN
INSERT INTO SplitString2(val) VALUES (cur_string);
END IF;
SET remainder = SUBSTRING(remainder, cur_position + delimiter_length);
END WHILE;
SELECT count(*) INTO numberMatch
FROM SplitString1 s1 JOIN SplitString2 s2 ON s1.val = s2.val;
RETURN result;
END
The idea is create two temporary table store each word and then compare these 2 tables. The result is good but the performace is awful. Anybody has better idea, please give me an advice.
Many thanks!
I don't think this will work as stated.
The logic is sound but you have not assigned any value to your result variable. Hence this function will always return 0. Replace:
RETURN result;
with
RETURN numberMatch;
Also replace:
CREATE DEFINER=`root`#`localhost` FUNCTION `CompareStrings`(str1 VARCHAR(255),str2 VARCHAR(255)) RETURNS double
with
CREATE DEFINER=`root`#`localhost` FUNCTION `CompareStrings`(str1 VARCHAR(255),str2 VARCHAR(255)) RETURNS double READS SQL DATA
As far as efficiency goes it looks pretty efficient. When you say 'performance is awful' - what constitutes 'awful'? Have you got any benchmark figures e.g. x calls took y millis?