2

I have columns in a mysql table that stores names of people as combinations of strings and incremented digits for uniqueness, so I have names stored as so :

Patrick, Patrick1, Patrick2, ..... Patrick10, David, David2, .... David5

How do I retrieve just the alpha name itself, without the digits? Say I want to group by the distinct names, and count per group, so I get a result resembling the following.

name    | frequency
-----------------
Patrick | 10
David   | 5

4 Answers 4

1

A solution would be this:(it doesn't look to good, but it works)

SELECT 
  TRIM(TRAILING '0' FROM 
    TRIM(TRAILING '1' FROM 
      TRIM(TRAILING '2' FROM 
        TRIM(TRAILING '3' FROM 
          -- ... 
            TRIM(TRAILING '8' FROM 
              TRIM(TRAILING '9' FROM name)))))) AS name
FROM your_table

Then you can select with GROUP BY from the result:

SELECT name, count(*) AS frequency FROM (
-- previous select
) AS t
GROUP BY name
Sign up to request clarification or add additional context in comments.

2 Comments

That'd be OK provided the numbers don't reach as high as 21.
Whoops! Then the digits should be replaced to a single one and then use TRIM.
0

I'll have a little think about that, but I would recommend that if you need a distinguishing number, you keep it in a different column. That way, you won't have difficulties of this sort.

2 Comments

I am not maintaining that table, and I don't have access to the code that populates the columns, hence my request
You have my deepest sympathy, Olaseni.
0

You can "chain" the replace command like this (this will remove the digits 0,1,2 in the query). You can expand this for the other digits, but I don't know if this will perform very well on large datasets:

select replace(replace(replace(Name,"0",""),"1",""),"2","") from users;

I would think also, it will be better to do what Brian suggested.

2 Comments

How do you ensure that only appended digits are removed?
I was about to say, Volker had cracked it. I think you'll have to create your own stored function to process the name. More information in 12.1.15 of the MySQL Reference Manual if you need it.
0

you could use a udf.

and then try Something like follwing

select REGEX_REPLACE(name, [0-9], '') as Name, Count(Name) 
      from tableName  
      Group by Name

2 Comments

I think that's Oracle and PostgreSQL; I can't find it in the MySQL manual.
A MySQL version of REGEXP_REPLACE is available as a UDF from launchpad.net/mysql-udf-regexp

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.