Wrox Programmer Forums
Go Back   Wrox Programmer Forums > SQL Server > SQL Server 2000 > SQL Server 2000
|
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
 
Old August 22nd, 2006, 02:08 PM
Authorized User
 
Join Date: Apr 2005
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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




 
Old August 22nd, 2006, 05:07 PM
Authorized User
 
Join Date: Apr 2005
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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

 
Old August 25th, 2006, 12:28 AM
Friend of Wrox
 
Join Date: May 2006
Posts: 246
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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
 
Old August 30th, 2006, 11:39 PM
Authorized User
 
Join Date: Apr 2005
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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))



 
Old August 31st, 2006, 01:45 AM
Friend of Wrox
 
Join Date: May 2006
Posts: 246
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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


 
Old September 5th, 2006, 11:07 AM
Friend of Wrox
 
Join Date: Mar 2004
Posts: 3,069
Thanks: 0
Thanked 10 Times in 10 Posts
Default

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
 
Old September 5th, 2006, 11:20 AM
Friend of Wrox
 
Join Date: May 2006
Posts: 246
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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')






Similar Threads
Thread Thread Starter Forum Replies Last Post
count occurrence of a character in a string surya Javascript How-To 6 May 7th, 2012 04:23 PM
How to count the number of occurence of a string ? khb3283 C# 12 October 5th, 2008 11:10 PM
count the string from csv format sudhirbharti C# 1 March 25th, 2008 03:16 PM
Count string occurences across recordset goldstein SQL Server 2005 1 July 9th, 2007 06:47 PM
count distinct values Chris Cash XSLT 3 June 8th, 2006 04:55 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.