|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Monday, October 29, 2012 2:52 PM
Points: 2,
Visits: 49
|
|
| Comments posted to this topic are about the item List Count
|
|
|
|
|
SSC Veteran
      
Group: General Forum Members
Last Login: Thursday, June 13, 2013 4:42 AM
Points: 284,
Visits: 503
|
|
| Use a tally table surely?
|
|
|
|
|
SSC Veteran
      
Group: General Forum Members
Last Login: Yesterday @ 3:33 AM
Points: 276,
Visits: 525
|
|
This is simpler...
declare @mylist nvarchar(100); set @mylist = 'A,B, C, D , 1, 2,345, EFG, H,';
declare @delim varchar(2)=','; select LEN(@mylist) - LEN(replace(@mylist,@delim,''));
|
|
|
|
|
SSC Rookie
      
Group: General Forum Members
Last Login: Friday, May 24, 2013 11:59 AM
Points: 42,
Visits: 63
|
|
| @paul_ramster - My thoughts exactly. That is the way I've always done it in SQL and other languages. The only reason you'd need to use a loop to get a count in a list like this is if you had a text qualifier, where when the delimiter falls within the text qualifier, it is not a delimiter.
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Monday, October 29, 2012 2:52 PM
Points: 2,
Visits: 49
|
|
| You're exactly right. I was lost in a solution that considered delimiters within text qualifiers and ended up with something far more complex than was necessary.
|
|
|
|
|
SSChasing Mays
      
Group: General Forum Members
Last Login: 2 days ago @ 3:46 AM
Points: 606,
Visits: 373
|
|
DECLARE @Numbertable table ( ID INT PRIMARY KEY )
INSERT INTO @Numbertable select TOP 1000 ROW_NUMBER() OVER(order by si.object_id) from sys.objects si,sys.objects s
declare @mylist nvarchar(100) set @mylist = 'A,B, C, D , 1, 2,345, EFG, H,'
select COUNT(string) from ( select NULLIF(SUBSTRING(@mylist,id,CHARINDEX(',',@mylist+',',id)-ID),'') AS string from @Numbertable where id <= LEN(@mylist) and SUBSTRING(','+@mylist,ID,1) = ',' )d
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Friday, June 14, 2013 11:54 AM
Points: 3,
Visits: 135
|
|
declare @mylist nvarchar(100), @delimiter VARCHAR(2) set @mylist = 'A,B, C, D , 1, 2,345, EFG, H, ' set @delimiter = ',' select @mylist list, (LEN(@mylist) - LEN(replace(@mylist,@delimiter,'')))/LEN(@delimiter) + case when right(rtrim(LTRIM(@mylist)),1) = @delimiter then 0 else 1 end itemcount
|
|
|
|