Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hiii,

It is possible is ms sql to count characters in each words indivisually in string. if yes then please give me solution its urgent.

e.g. string is I Love Code Project
and I want something like this. 1,4,4,7

Thanks in Advance..
Posted

Yes, it is possible, in my opinion, since SQL Server provides quite powerful string functions[^]. However why do you want to use the database engine for such a task? I would use a more appropriate programming language.
 
Share this answer
 
You could create a function and call it like this:

CREATE FUNCTION [dbo].[fnSplitString] 
( 
    @string NVARCHAR(MAX), 
    @delimiter CHAR(1) 
) 
RETURNS @output TABLE(splitdata NVARCHAR(MAX) 
) 
BEGIN 
    DECLARE @start INT, @end INT 
    SELECT @start = 1, @end = CHARINDEX(@delimiter, @string) 
    WHILE @start < LEN(@string) + 1 BEGIN 
        IF @end = 0  
            SET @end = LEN(@string) + 1
       
        INSERT INTO @output (splitdata)  
        VALUES(SUBSTRING(@string, @start, @end - @start)) 
        SET @start = @end + 1 
        SET @end = CHARINDEX(@delimiter, @string, @start)
        
    END 
    RETURN 
END


Select (Stuff((Select ', ' + CONVERT(varchar,  LEN(SplitData)) FROM dbo.fnSplitString('I Love Code Project','') FOR XML PATH('')),1,2,''))
 
Share this answer
 
Comments
bburhanbohra 29-Apr-14 4:16am    
Thanx Bro..Great work..
Hello,

Check this codeproject article: [^]

JAFC
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900