← Back to team overview

maria-discuss team mailing list archive

Re: help - i'm not finding one sql function

 

Obivously this is the best solution
SELECT LENGTH("banana") - LENGTH( REPLACE("banana","n",""));
(so no need or a Stored Function)

.. however you may consider to replace LENGTH with CHAR_LENGTH, This is
important if you use UTF8 and if you have non-ASCII characters (what you
undoubtedly have as a Brazilian person (I guess you are?)). This example
with the Spanish "ñ" character illustrates:

SET NAMES utf8;
SELECT LENGTH("bañaña") - LENGTH( REPLACE("bañaña","ñ",""));
-- returns "4" (4 *bytes* more exactly) - and this result is not what your
are after!

-- whereas
SET NAMES utf8;
SELECT CHAR_LENGTH("bañaña") - CHAR_LENGTH( REPLACE("bañaña","ñ",""));
-- returns "2" (2 *characters* more exactly) - and this is what you want.


-- Peter


On Wed, Mar 19, 2014 at 6:16 PM, Federico Razzoli <federico_raz@xxxxxxxx>wrote:

> When a PHP function does something you cannot do in SQL, you could call
> PHP from your queries using the Gearman UDF.
>
> https://launchpad.net/gearman-mysql-udf
>
> In most cases this should be faster than a stored function. I hope that in
> the future this will change, but who knows...
>
> Regards,
> Federico
>
>
> --------------------------------------------
> Mer 19/3/14, Roberto Spadim <roberto@xxxxxxxxxxxxx> ha scritto:
>
>  Oggetto: Re: [Maria-discuss] help - i'm not finding one sql function
>  A: "pslawek83" <pslawek83@xxxxx>
>  Cc: "Maria Discuss" <maria-discuss@xxxxxxxxxxxxxxxxxxx>
>  Data: Mercoledì 19 marzo 2014, 16:18
>
>  wow!
>  thanks kk i was trying to find a php like functionyour
>  idea is very nice :) thank you!
>  substr_count =>
>  (length(field)-length(replace('
>  ','',field)))
>
>  /)
>
>  2014-03-19 12:16 GMT-03:00
>  pslawek83 <pslawek83@xxxxx>:
>
>  Hi
>  Roberto,
>  You'll have to replace char
>  => empty and get the difference in length.
>
>  SELECT
>  (length("banana") - replace("n",
>  "", "banana"))
>
>
>  There's probably no
>  "standard" function to do that.
>
>
>  Dnia 19 marca
>  2014 15:51 Roberto Spadim <roberto@xxxxxxxxxxxxx>
>  napisał(a):
>
>
>
>
>
>
>  hi guys, i'm not finding a function to
>  return how many character i have, for example:
>
>  "banana"
>
>  i want a function that return 2 "n"
>  characters, example:
>  substr_count("banana","n") =>
>  2
>
>  sorry it a begginners question, but i didn't found
>  it in mysql/mariadb manual
>
>
>  --
>
>  Roberto Spadim
>  SPAEmpresarial
>  Eng. Automação e Controle
>
>
>
>
>
>
>
>
>
>
>
>  --
>  Roberto Spadim
>  SPAEmpresarialEng. Automação e
>  Controle
>
>  -----Segue allegato-----
>
>  _______________________________________________
>  Mailing list: https://launchpad.net/~maria-discuss
>  Post to     : maria-discuss@xxxxxxxxxxxxxxxxxxx
>  Unsubscribe : https://launchpad.net/~maria-discuss
>  More help   : https://help.launchpad.net/ListHelp
>
>
> _______________________________________________
> Mailing list: https://launchpad.net/~maria-discuss
> Post to     : maria-discuss@xxxxxxxxxxxxxxxxxxx
> Unsubscribe : https://launchpad.net/~maria-discuss
> More help   : https://help.launchpad.net/ListHelp
>

Follow ups

References