{"id":335,"date":"2015-06-26T15:47:32","date_gmt":"2015-06-26T15:47:32","guid":{"rendered":"http:\/\/www.gnial.com.br\/gnialhelp\/?p=335"},"modified":"2019-11-26T10:32:23","modified_gmt":"2019-11-26T13:32:23","slug":"mysql-criar-funcao-tipo-split-create-function-with-split-delimited-strings","status":"publish","type":"post","link":"http:\/\/www.gnial.com.br\/gnialhelp\/mysql-criar-funcao-tipo-split-create-function-with-split-delimited-strings\/","title":{"rendered":"MySql &#8211; Criar fun\u00e7\u00e3o tipo SPLIT \/ Create function with SPLIT delimited strings"},"content":{"rendered":"<h3>Create function syntax<\/h3>\n<p>A <a href=\"http:\/\/dev.mysql.com\/doc\/refman\/5.0\/en\/create-procedure.html\" target=\"_blank\" rel=\"noopener noreferrer\">user-defined function<\/a> is a way to extend MySQL with a new function that works like a native MySQL function.<\/p>\n<pre><span style=\"color: #333399;\"><strong>CREATE [AGGREGATE] FUNCTION function_name\r\nRETURNS {STRING|INTEGER|REAL|DECIMAL}<\/strong><\/span><\/pre>\n<p>To create a function, you must have the INSERT privilege for the &lt;mysql&gt; database.<\/p>\n<h3>Split delimited strings<\/h3>\n<p>The following example function takes 3 parameters, performs an operation using an SQL function, and returns the result.<\/p>\n<p><strong>Function<\/strong><\/p>\n<pre><strong><span style=\"color: #333399;\">CREATE FUNCTION SPLIT_STR(\r\n  x VARCHAR(255),\r\n  delim VARCHAR(12),\r\n  pos INT\r\n)\r\nRETURNS VARCHAR(255)\r\nRETURN REPLACE(SUBSTRING(SUBSTRING_INDEX(x, delim, pos),\r\n       LENGTH(SUBSTRING_INDEX(x, delim, pos -1)) + 1),\r\n       delim, '');<\/span><\/strong><\/pre>\n<p><strong>Usage<\/strong><\/p>\n<pre>SELECT SPLIT_STR(string, delimiter, position)<\/pre>\n<p><strong>Example<\/strong><\/p>\n<pre><strong><span style=\"color: #333399;\">SELECT SPLIT_STR('a|bb|ccc|dd', '|', 3) as third;<\/span><\/strong>\r\n\r\n+-------+\r\n| third |\r\n+-------+\r\n| ccc   |\r\n+-------+\r\n\r\nfonte: http:\/\/blog.fedecarg.com\/2009\/02\/22\/mysql-split-string-function\/<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Create function syntax A user-defined function is a way to extend MySQL with a new function that works like a native MySQL function. CREATE [AGGREGATE] FUNCTION function_name RETURNS {STRING|INTEGER|REAL|DECIMAL} To create a function, you must have the INSERT privilege for the &lt;mysql&gt; database. Split delimited strings The following example function takes 3 parameters, performs an [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":565,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-335","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","has-thumbnail"],"_links":{"self":[{"href":"http:\/\/www.gnial.com.br\/gnialhelp\/wp-json\/wp\/v2\/posts\/335","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.gnial.com.br\/gnialhelp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.gnial.com.br\/gnialhelp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.gnial.com.br\/gnialhelp\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.gnial.com.br\/gnialhelp\/wp-json\/wp\/v2\/comments?post=335"}],"version-history":[{"count":3,"href":"http:\/\/www.gnial.com.br\/gnialhelp\/wp-json\/wp\/v2\/posts\/335\/revisions"}],"predecessor-version":[{"id":567,"href":"http:\/\/www.gnial.com.br\/gnialhelp\/wp-json\/wp\/v2\/posts\/335\/revisions\/567"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/www.gnial.com.br\/gnialhelp\/wp-json\/wp\/v2\/media\/565"}],"wp:attachment":[{"href":"http:\/\/www.gnial.com.br\/gnialhelp\/wp-json\/wp\/v2\/media?parent=335"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.gnial.com.br\/gnialhelp\/wp-json\/wp\/v2\/categories?post=335"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.gnial.com.br\/gnialhelp\/wp-json\/wp\/v2\/tags?post=335"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}