Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 count number of 'd'

Author  Topic 

funketekun
Constraint Violating Yak Guru

491 Posts

Posted - 2006-11-30 : 11:29:37
i just want one row instead of multiple rows results.

declare @name varchar(30)
declare @count int
declare @a int
set @name = 'dennis dennis'
set @count = 0
set @a = 1


while(@count < len(@name))
begin
select count(substring(@name, @a, 1)), substring(@name, @a, 1)
where substring(@name, @a, 1) = 'd'


select @count = @count +1
select @a = @a+1

end

snSQL
Master Smack Fu Yak Hacker

1837 Posts

Posted - 2006-11-30 : 11:35:10
[code]declare @name varchar(30)
declare @count int
declare @a int
declare @dcount int
set @name = 'dennis dennis'
set @dcount = 0
set @count = 0
set @a = 1


while(@count < len(@name))
begin
select @dcount = @dcount + count(substring(@name, @a, 1))
where substring(@name, @a, 1) = 'd'


select @count = @count +1
select @a = @a+1

end
select @dcount[/code]
Go to Top of Page

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2006-11-30 : 11:39:50
or this:

declare @name varchar(30)
set @name = 'dennis dennis'
select len(@name) - len(replace(@name,'d','')) as CountOfD

Be One with the Optimizer
TG
Go to Top of Page

funketekun
Constraint Violating Yak Guru

491 Posts

Posted - 2006-11-30 : 11:46:49
excellent thanks.
Go to Top of Page
   

- Advertisement -