How to find a A available in AADBRFDT in Oracle SQL

Viewed 25

Hello All I'm Working on SQL to get execute complex queries I found a problem in the below query

I have input or a String Contain value as like AANFEDSSAAAA.

now I want to count how many A available in this.

I have written a code as below but I'm unable to get desired output.

Length('AAABCDA)-LENGTH (replace('AAABCDA','A'));

when I'm executing this i got an error as below.

 Error starting at line : 109 in command -
Length('AAABCDA)-LENGTH (replace('AAABCDA','A'))
Error report -
Unknown Command

how to number of A which is available in the above examples.

the same problem i can solve using a RegExp as below

SELECT REGEXP_COUNT ('AAABCDA', 'A') FROM dual;

When I execute this I will get an output as below.

4

need help in first Code what is wrong with it and how many ways are available to get the desired outputs.

how can manually we can code instead of using inbuilt Functions?

1 Answers

Your length trick is a good way to do this, but your syntax is slightly off. Use this version:

SELECT LENGTH('AAABCDA') - LENGTH(REPLACE('AAABCDA', 'A', '')) AS num_a  -- 4
FROM dual;

Check the demo here.

Related