Split Comma Separated String in My SQL


DELIMITER $$

CREATE DEFINER=`root`@`localhost` PROCEDURE `String_Split`(
 vString VARCHAR(8000),
  vSeparator VARCHAR(5)
)
BEGIN
DECLARE vDone tinyint(1) DEFAULT 1;
DECLARE vIndex INT DEFAULT 1;
DECLARE vSubString VARCHAR(15);

DROP TABLE IF EXISTS tmpIDList;
CREATE TEMPORARY TABLE tmpIDList (ID INT);

WHILE vDone > 0
DO
  SET vSubString = SUBSTRING(vString, vIndex,
                    IF(LOCATE(vSeparator, vString, vIndex) > 0,
                      LOCATE(vSeparator, vString, vIndex) - vIndex,
                      LENGTH(vString)
                    ));
                   
                     IF LENGTH(vSubString) > 0 THEN
      SET vIndex = vIndex + LENGTH(vSubString) + 1;
      INSERT INTO tmpIDList VALUES (vSubString);
  ELSE
      SET vDone = 0;
  END IF;
 
  END WHILE;
 
END


you can call this for every string passed by fronend and use this any sql query ,After that you should drop the temp table
    like :  call  String_Split('1,1,1,12,3,34,22',',');
     select mem_name from member where Id in (select id from tmpIDList )
      DROP table tmpIDList;
   

Comments

Popular posts from this blog

Solution of This Error : Error CS0012: The type 'Object' is defined in an assembly that is not referenced. You must add a reference to assembly 'netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'.

How to creat slideshow in javascript