September 16, 2008 at 5:41 pm
Hi!
I have a table that has a Procedures field which includes several numbers separated by a space (i.e. 99202 10060 99000 A6402). I need to count the number of procedures in this field and I am assuming the best way would be to count the number of spaces and add 1 to it. Only problem is I don't know how to count the spaces or if it can be done. Can anyone offer assistance?
Mishelle
Western Skies Billing Service
September 17, 2008 at 4:21 am
--function that returns number of spaces in string
create function GetSpaceCount(@SearchString nvarchar(2000))
returns int
as
begin
declare @SpacePos int,@SpaceCount int;
set @SpacePos=0;
set @SpaceCount=0;
set @SpacePos=CHARINDEX(' ',@SearchString,@SpacePos)
while @SpacePos<>0
begin
set @SpacePos=CHARINDEX(' ',@SearchString,@SpacePos+1)
set @SpaceCount=@SpaceCount+1
end
return @SpaceCount;
end
--test
select GetSpaceCount('99202 10060 99000 A6402')
September 17, 2008 at 4:41 am
Or look at Jeff (RBAR) Moden's article:- http://www.sqlservercentral.com/articles/TSQL/62867/
September 17, 2008 at 4:57 am
DECLARE @s-2 VARCHAR(30)
SET @s-2='99202 10060 99000 A6402'
SELECT LEN(@S)-LEN(REPLACE(@S,' ','')) AS NumberOfSpaces
____________________________________________________
Deja View - The strange feeling that somewhere, sometime you've optimised this query before
How to get the best help on a forum
http://www.sqlservercentral.com/articles/Best+Practices/61537September 17, 2008 at 9:14 am
Thank you all so much! You totally rock!
September 17, 2008 at 8:07 pm
Mark (9/17/2008)
DECLARE @s-2 VARCHAR(30)
SET @s-2='99202 10060 99000 A6402'
SELECT LEN(@S)-LEN(REPLACE(@S,' ','')) AS NumberOfSpaces
THAT's the way to do it... no RBAR, no join! π
--Jeff Moden
Change is inevitable... Change for the better is not.
September 18, 2008 at 10:45 am
That's the one I used and it worked marvelously. π
September 18, 2008 at 10:55 am
Jeff Moden (9/17/2008)
Mark (9/17/2008)
DECLARE @s-2 VARCHAR(30)
SET @s-2='99202 10060 99000 A6402'
SELECT LEN(@S)-LEN(REPLACE(@S,' ','')) AS NumberOfSpaces
THAT's the way to do it... no RBAR, no join! π
Except...It will return the wrong number if any of the spaces are at the end.......
Try
SELECT LEN(@S+'.')-LEN(REPLACE(@S+'.',' ',''))
----------------------------------------------------------------------------------
Your lack of planning does not constitute an emergency on my part...unless you're my manager...or a director and above...or a really loud-spoken end-user..All right - what was my emergency again?
September 18, 2008 at 11:04 am
Good point. But what does the + '.' do?
September 18, 2008 at 11:08 am
It makes sure that the trailing spaces don't get truncated before you count them. (LEN will not count trailing spaces). by adding it to both - it cancels out in the count.)
----------------------------------------------------------------------------------
Your lack of planning does not constitute an emergency on my part...unless you're my manager...or a director and above...or a really loud-spoken end-user..All right - what was my emergency again?
September 18, 2008 at 11:32 am
Gotcha! Thanks for the info! That is very helpful.
September 18, 2008 at 1:12 pm
Matt Miller (9/18/2008)
Jeff Moden (9/17/2008)
Mark (9/17/2008)
DECLARE @s-2 VARCHAR(30)
SET @s-2='99202 10060 99000 A6402'
SELECT LEN(@S)-LEN(REPLACE(@S,' ','')) AS NumberOfSpaces
THAT's the way to do it... no RBAR, no join! π
Except...It will return the wrong number if any of the spaces are at the end.......
Try
SELECT LEN(@S+'.')-LEN(REPLACE(@S+'.',' ',''))
You could always use DATALENGTH() instead. π
--Jeff Moden
Change is inevitable... Change for the better is not.
September 18, 2008 at 1:45 pm
But consider that for unicode strings DATALENGTH = 2*LEN;)
Best Regards,
Chris BΓΌttner
September 19, 2008 at 7:17 am
Matt Miller (9/18/2008)
It makes sure that the trailing spaces don't get truncated before you count them. (LEN will not count trailing spaces). by adding it to both - it cancels out in the count.)
But doesn't the OP want to count the number of items in the list by counting the spaces BETWEEN them and then adding 1? In that case you don't want to count the trailing spaces.
Terri
To speak algebraically, Mr. M. is execrable, but Mr. C. is
(x+1)-ecrable.
Edgar Allan Poe
[Discussing fellow writers Cornelius Mathews and William Ellery Channing.]
Viewing 15 posts - 1 through 15 (of 22 total)
You must be logged in to reply to this topic. Login to reply