If i have string 'ABCDAAARSTRLAMA ' how can i count 'A' this char from the given string in sql server 2008
            Asked
            
        
        
            Active
            
        
            Viewed 182 times
        
    4 Answers
2
            
            
        use len   and Replace function 
declare @myvar varchar(20)
    set @myvar = 'ABCDAAARSTRLAMA'
    select len(@myvar) - len(replace(@myvar,'A',''))
        Zaynul Abadin Tuhin
        
- 31,407
 - 5
 - 33
 - 63
 
1
            
            
        SELECT LENGTH( 'ABCDAAARSTRLAMA' ) - LENGTH( replace( 'ABCDAAARSTRLAMA', 'A', '' ) );
Length Function
The Length function in SQL is used to get the length of a string. This function has a different name for different databases:
MySQL: LENGTH( )
Oracle: LENGTH( )
SQL Server: LEN( ) 
Syntax
The syntax for the Length function is as follows:
LENGTH(str)
        Sinto
        
- 3,915
 - 11
 - 36
 - 70
 
0
            
            
        Try this one
SELECT (LENGTH('ABCDAAARSTRLAMA') - LENGTH(REPLACE('ABCDAAARSTRLAMA', 'A', '')));
        Aritra Bhattacharya
        
- 720
 - 1
 - 7
 - 18
 
0
            
            
        select len('ABCDAAARSTRLAMA') - len(replace('ABCDAAARSTRLAMA', 'A', ''))
This would give the count of 'A' from the string
        NicA
        
- 1
 - 1