Sunday, 5 January 2020

Count total number of char and their type in a string using sql server

//////////////// Sql String


declare @str varchar(max)='this is a test string', @tlen int=0,@lstStr varchar(max)='',@fStr varchar(max)='',@Fchar varchar(10)=''

set @tlen=len(@str)
while @tlen>0
begin
set @Fchar=left(@str,1)
set @fStr=@fStr+'Total Count of '''+@Fchar+''' :  '+cast(len(@str)-len(replace(@str,@Fchar,'')) as varchar)+', '
set @str=replace(@str,@Fchar,'')
set @tlen=len(@str)
end

print @fStr



///////////////////// Result

Total Count of 't' :  4, Total Count of 'h' :  1, Total Count of 'i' :  3, Total Count of 's' :  4, Total Count of ' ' :  4, Total Count of 'a' :  1, Total Count of 'e' :  1, Total Count of 'r' :  1, Total Count of 'n' :  1, Total Count of 'g' :  1,


No comments:

Post a Comment

How to highlight selected text in notepad++

  –> To highlight a block of code in Notepad++, please do the following steps step-1  :- Select the required text. step-2  :- Right click...