 |
| SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

August 22nd, 2006, 02:08 PM
|
|
Authorized User
|
|
Join Date: Apr 2005
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
getting a count of values from within a string
What is the best way to get the following results from a field.
column says "02, 04, 05, 06, 08, 09"
the count I need in this case is 6
|
|

August 22nd, 2006, 05:07 PM
|
|
Authorized User
|
|
Join Date: Apr 2005
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
I've tried using a function to return the count from the field but it doesn't permit the use of using a select to return a value to the client.
here's how the code looks:
CREATE FUNCTION fnRunner(@Runners varchar(50))
RETURNS int -- count of runners
AS
BEGIN
declare @Count int
, @start int
set @Count = 0
set @start = 1
while @start < @Runners
begin
select charindex(' ',@Runners,@start)
set @start = charindex(' ',@Runners)
set @Count = @Count + 1
end
return @Count
END
GO
|
|

August 25th, 2006, 12:28 AM
|
|
Friend of Wrox
|
|
Join Date: May 2006
Posts: 246
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Or this simple one line query...
DECLARE @Field VARCHAR(100)
SELECT @Field = '02, 04, 05, 06, 08, 09'
SELECT 1 + LEN(@Field) - LEN(REPLACE(@Field, ',', ''))
In you environment, that query will look like
SELECT YourColumn, 1 + LEN(YourColumn) - LEN(REPLACE(YourColumn, ',', ''))
FROM YourTable
|
|

August 30th, 2006, 11:39 PM
|
|
Authorized User
|
|
Join Date: Apr 2005
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
not sure how that would help as I need the count of numbers in the field. (i.e. 6 = count(01 03 05 06 08 11))
|
|

August 31st, 2006, 01:45 AM
|
|
Friend of Wrox
|
|
Join Date: May 2006
Posts: 246
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Since the numbers are separated with commas, I just see the difference for the full string and the string with commas deleted. The difference in length is the number of commas deleted. Add 1 to that result and you get the count of numbers in the string.
Very basic.
This works even for the example you have given last, with spaces as delimiters.
SELECT YourColumn, 1 + LEN(YourColumn) - LEN(REPLACE(YourColumn, ' ', ''))
FROM YourTable
|
|

September 5th, 2006, 11:07 AM
|
|
Friend of Wrox
|
|
Join Date: Mar 2004
Posts: 3,069
Thanks: 0
Thanked 10 Times in 10 Posts
|
|
It looks like you might want:
SELECT Count(Table.Column) AS CountOfColumn
FROM Table;
This will turn this:
Column
02
04
05
06
08
08
Into this:
CountOfColumn
6
Is this what you want?
mmcdonal
|
|

September 5th, 2006, 11:20 AM
|
|
Friend of Wrox
|
|
Join Date: May 2006
Posts: 246
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
The function Cole wrote above will work to, but due to a "feature" in SQL 2000, you must use dbo as owner.
select dbo.fnRunner('02, 04, 05, 06, 08, 09')
|
|
 |