I'm having issues with the below, I've looked through some of the similar questions but haven't gotten to a full understanding of what is going on and how to resolve it.
'4,2'
is a comma seperated string, it could as easily be '4,2,7,19'
.
SELECT FN_ADMIN_EDIT_ARTICLE (
123
, '4,2'
);
It throws:
Error Code: 1292. Truncated incorrect DOUBLE value: '4,2'
I've tried to cast the input as an NCHAR
but it does't seem to help.
SELECT FN_ADMIN_EDIT_ARTICLE (
123
, CAST('4,2' AS NCHAR)
);
Below is what FN_ADMIN_EDIT_ARTICLE
is doing.
CREATE FUNCTION `FN_ADMIN_EDIT_ARTICLE`(
`id` INT(11)
, `tags` VARCHAR(250)
) RETURNS int
BEGIN
DECLARE `tag` TEXT DEFAULT '';
DECLARE i INT DEFAULT 1;
/* Remove Old Tags */
DELETE FROM `td_article_tag`
WHERE `ata_art_id` = `id`
AND `ata_tag_id` NOT IN (`tags`);
IF `tags` <> "" THEN
/* Add New Tags */
`tag_loop`: LOOP
SET `tag` = FN_SPLIT_STR(`tags`, ",", i);
SET i = i + 1;
/* Exit loop if no more tags */
IF `tag` = '' THEN
LEAVE `tag_loop`;
END IF;
INSERT INTO `td_article_tag` (
`ata_art_id`
, `ata_tag_id`
) VALUES (
`id`
, `tag`
)
ON DUPLICATE KEY UPDATE `ata_tag_id` = `tag`;
END LOOP;
END IF;
RETURN `id`;
END
FN_SPLIT_STR
CREATE FUNCTION FN_SPLIT_STR (`value` TEXT, delim VARCHAR(12), pos INT)
RETURNS TEXT
RETURN REPLACE(SUBSTRING(SUBSTRING_INDEX(`value`, delim, pos), LENGTH(SUBSTRING_INDEX(`value`, delim, pos -1)) + 1), delim, '');