Suriya Muthukumar
Suriya Muthukumar

Reputation: 11

Count how many letters in a name with input "A" in Sql

For Example: "Mahatma Gandhi" - how many A's are in the name ? I need a query for this. any help pls !

Upvotes: 1

Views: 936

Answers (2)

Jacob
Jacob

Reputation: 14731

REGEX could be used to get the count of a letter in a string

SELECT REGEXP_COUNT ('Mahatma Gandhi', 'a') FROM DUAL;

For case-insensitive search

SELECT REGEXP_COUNT ('Mahatma Gandhi',
                     'A',
                     1,
                     'i')
          REGEXP_COUNT
  FROM DUAL;

Reference

Upvotes: 3

ScaisEdge
ScaisEdge

Reputation: 133370

You could try using length and replace

select length( 'Mahatma Gandhi') -  length( replace('Mahatma Gandhi', 'a','')) num_char
from dual

Upvotes: 2

Related Questions