DELIMITER$$
DROP FUNCTION IF EXISTS hitungchar$$
CREATE FUNCTION hitungchar (kalimat VARCHAR(255), karakter CHAR(1))
RETURNS INT
BEGIN
DECLARE hasil INT DEFAULT 0;
SET @jumlah = LENGTH(kalimat);
SET @i=0;
WHILE @i < @jumlah DO
IF karakter = RIGHT(LEFT(kalimat,@i),1) THEN
SET hasil = hasil+1;
END IF;
SET @i = @i+1;
END WHILE;
RETURN hasil;
END$$
SELECT hitungchar ('Saya belajar BDL','l') AS hasil;
DROP FUNCTION IF EXISTS hitungchar$$
CREATE FUNCTION hitungchar (kalimat VARCHAR(255), karakter CHAR(1))
RETURNS INT
BEGIN
DECLARE hasil INT DEFAULT 0;
SET @jumlah = LENGTH(kalimat);
SET @i=0;
WHILE @i < @jumlah DO
IF karakter = RIGHT(LEFT(kalimat,@i),1) THEN
SET hasil = hasil+1;
END IF;
SET @i = @i+1;
END WHILE;
RETURN hasil;
END$$
SELECT hitungchar ('Saya belajar BDL','l') AS hasil;
0 komentar:
Posting Komentar