How to sperated character from Mysql? - mysql

I use the example below :
select '10+20+30'
and generate value :
'++'
In other words, strip any digit and leave only signs.

Function:
DROP FUNCTION IF EXISTS getMOps;
DELIMITER $$
CREATE FUNCTION getMOps
( s varchar(100)
)
RETURNS VARCHAR(100) DETERMINISTIC
BEGIN
DECLARE sOut VARCHAR(100);
DECLARE theLen,iLooper INT;
SET iLooper=1;
SET theLen=LENGTH(s);
SET sOut='';
WHILE iLooper<theLen DO
IF SUBSTR(s,iLooper,1) IN ('+','-','*','/','^') THEN
SET sOut=CONCAT(sOut,SUBSTR(s,iLooper,1));
END IF;
SET iLooper=iLooper+1;
END WHILE;
return (sOut);
END$$
DELIMITER ;
Test:
select getMOps('fish'); -- blank string
select getMOps('1+2+7-1'); -- '++-'
Modify the IN clause to suit your tastes. I am sure there are better ways.

Related

How can i fix the Error Code: 1318 in SQL?

I CREATED TWO same FUNCTION without the function name
DELIMITER //
CREATE FUNCTION fnc_credit_custstatus(custcredit INT)
RETURNS VARCHAR(6) DETERMINISTIC
BEGIN
DECLARE custstatus VARCHAR(6);
IF CustCredit>=1000 THEN SET custstatus='VIP';
ELSEIF CustCredit<1000 THEN SET custstatus='NONVIP';
END IF;
RETURN (custstatus);
END//
DELIMITER ;
and the other one is :
DELIMITER //
CREATE FUNCTION f(custcredit INT)
RETURNS VARCHAR(6) DETERMINISTIC
BEGIN
DECLARE custstatus VARCHAR(6);
IF CustCredit>=1000 THEN SET custstatus='VIP';
ELSEIF CustCredit<1000 THEN SET custstatus='NONVIP';
END IF;
RETURN (custstatus);
END//
DELIMITER ;
This two function are all exists
but when I want to select the same query with different name,
SELECT *,fnc_credit_custstatus(custcredit)AS custstatus FROM customer_salon;
SELECT *, f(custcredit) AS custstatus FROM customer_salon;
BUT the longer name one can't run out
Error Code: 1318
Incorrect number of arguments for FUNCTION database.fnc_credit_custstatus; expected 0, got 1`
I don't know why

**When i execute this procedure than get error 1064**

CREATE DEFINER=`root`#`localhost` PROCEDURE `GetStateList`(IN _CountryName VARCHAR(255))
BEGIN
DECLARE #CCode VARCHAR(50)
SET #CCode = (SELECT CountryID from countrylist where CountryName = _CountryName);
SELECT #CCode;
END
You need to redefine the Delimiter to something else (eg: $$) other than ;. At the end, reset the limiter back to ;. Also, a semicolon was missing in the Declare statement:
DELIMITER $$
CREATE DEFINER=`root`#`localhost` PROCEDURE `GetStateList`(IN _CountryName VARCHAR(255))
BEGIN
DECLARE #CCode VARCHAR(50); -- semicolon was missing here
SET #CCode = (SELECT CountryID
from countrylist
where CountryName = _CountryName);
SELECT #CCode;
END$$
DELIMITER ;

select only digits, numbers from text

I have a field text, In it there is information about such
sch hcbhsc hscbshcbc xxxxxxxx sgxfag jdhajdh;
dchbdbc bdcbdh bchdbd xx/xx-xxxx/xx svdhs sbjbsc
bdchbdc jncjdnc jbcjb xx/xx-xxxxx/xx gcvsgc jcbjsb
dchjbd bhjcbdcb bdcbcd xx-xxxx/xx shchscv hscbhsc
dhcbhd jdcbjdb jdcnjdcn xx-xxxxx/xx shcvsch jbscjc
Place x is only a digit, I need to write select and only those numbers are taken
Use SUBSTRING and PATINDEX string functions IN SQL server :
SELECT SUBSTRING(Your_FieldName, PATINDEX('%[0-9]%', Your_FieldName),
LEN(Your_FieldName))
For MYSQL refer below URL :
Query to get only numbers from a string
string
There is no formal PATINDEX() function in MySQL that achieves both the regex pattern lookup with returned character index, define User-Defined function that loops through each character in the length of a string and checks a REGEXP pattern on the character. Once created, use such a function in-line of a query.
DROP FUNCTION IF EXISTS PatIndex;
DELIMITER $$
CREATE FUNCTION PatIndex(pattern VARCHAR(255), tblString VARCHAR(255)) RETURNS INTEGER
DETERMINISTIC
BEGIN
DECLARE i INTEGER;
SET i = 1;
myloop: WHILE (i <= LENGTH(tblString)) DO
IF SUBSTRING(tblString, i, 1) REGEXP pattern THEN
RETURN(i);
LEAVE myloop;
END IF;
SET i = i + 1;
END WHILE;
RETURN(0);
END
Here is a MySQL function (routine) that will do just that. It is an improved version from the solution given here: how-to-get-only-digits-from-string-in-mysql
This improved version can handle much larger numbers. The old solution was limited by the INTEGER value, so if you had phone numbers for example (or string containing many digits), it would fail with out of range for column.
DELIMITER $$
CREATE FUNCTION ExtractNumber (in_string VARCHAR(50))
RETURNS varchar(50)
NO SQL
BEGIN
DECLARE ctrNumber VARCHAR(50);
DECLARE finNumber VARCHAR(50) DEFAULT '';
DECLARE sChar VARCHAR(1);
DECLARE inti VARCHAR(50) DEFAULT 1;
IF LENGTH(in_string) > 0 THEN
WHILE(inti <= LENGTH(in_string)) DO
SET sChar = SUBSTRING(in_string, inti, 1);
SET ctrNumber = FIND_IN_SET(sChar, '0,1,2,3,4,5,6,7,8,9');
IF ctrNumber > 0 THEN
SET finNumber = CONCAT(finNumber, sChar);
END IF;
SET inti = inti + 1;
END WHILE;
RETURN CAST(finNumber AS UNSIGNED);
ELSE
RETURN 0;
END IF;
END$$
DELIMITER ;
Now you can do this:
SELECT ExtractNumber(my_field)
FROM my_table;

MySQL syntax (function) is error

CREATE FUNCTION FC_IDKRITERIA()
RETURNS CHAR(3)
AS
BEGIN
DECLARE #MAX INT , #KODEBARU CHAR(3)
SELECT #MAX = MAX (RIGHT(IDKRITERIA,2)) FROM KRITERIA
IF #MAX IS NULL
SET #MAX = 0
SET #KODEBARU = 'K' + RIGHT('0'+CONVERT(VARCHAR(3),#MAX+ 1 ) ,2)
RETURN #KODEBARU
END
Every statement in a procedure must end with ;. To keep this from ending the function definition, use the DELIMITER command to change the command delimiter to something else.
And when doing a variable assignment in a SELECT clause, you have to use :=.
There's no AS at the beginning of a function definition.
You don't declare variables that begin with #.
You need THEN and END IF in an IF statement.
To concatenate strings, use CONCAT(), not +.
You have the arguments to CONVERT() in the wrong order, and VARCHAR(3) is not a valid type argument, it should be CHAR(3).
In a function, you can't use a SELECT statement at the top-level, because that means to return the result set, and functions can only return single values. So you have to assign #MAX from a (SELECT ...) expression.
DELIMITER $$
CREATE FUNCTION FC_IDKRITERIA() RETURNS CHAR(3)
BEGIN
SET #MAX = (SELECT MAX (RIGHT(IDKRITERIA,2)) FROM KRITERIA);
IF #MAX IS NULL
THEN SET #MAX = 0;
END IF;
SET #KODEBARU = CONCAT('K', RIGHT('0'+CONVERT(#MAX+ 1, CHAR(3)) ,2));
RETURN #KODEBARU;
END;
$$
DELIMITER ;

How to check output of function or variable for NULL

I am bit confused with Mysql syntax. I want to check for NULL the value of ExtractValue(xml, '//order[1]/quantity[$#i]') function. It can be assign to variable or this action can be skipped. I tried this and there is syntax error:
DELIMITER $$
DROP PROCEDURE IF EXISTS `sp_test_for_null`$$
CREATE PROCEDURE `sp_test_for_null`()
BEGIN
DECLARE xml VARCHAR(1000);
SET xml = '';
DECLARE test VARCHAR(1000);
SET test = (SELECT ExtractValue(xml, '//order[1]/quantity[$#i]');
IF (test IS NULL) THEN SELECT 1; END IF;
END$$
DELIMITER ;
CALL sp_test_for_null;
I'd try this (note that all DECLAREs are at the beginning:
DELIMITER $$
DROP PROCEDURE IF EXISTS `sp_test_for_null`$$
CREATE PROCEDURE `sp_test_for_null`()
BEGIN
DECLARE xml VARCHAR(1000);
DECLARE test VARCHAR(1000);
SET xml = '';
SET test = (SELECT ExtractValue(xml, '//order[1]/quantity[$#i]');
SELECT ISNULL(test, 1, 0);
END$$
DELIMITER ;
CALL sp_test_for_null;