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
 counting digits in a column

Author  Topic 

gavakie
Posting Yak Master

221 Posts

Posted - 2010-09-24 : 09:24:42
I need to count everytime there is a one in a column like this

000011111111 -- 9
000000000000 -- 0
000000000000 --0
011010110101 -- 7

Can this be done?

X002548
Not Just a Number

15586 Posts

Posted - 2010-09-24 : 09:29:56
is it just 1's and 0's

is the column datatype varchar?

DECALRE @Col varchar(25); SET @Col = '000011111111'
SELECT LEN(REPLACE(@COL,'0',''))

not tested

Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx





Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2010-09-24 : 09:31:16
is it just 1's and 0's

is the column datatype varchar?

DECALRE @Col varchar(25); SET @Col = '000011111111'
SELECT LEN(REPLACE(@COL,'0',''))

not tested

Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx





Go to Top of Page

blindman
Master Smack Fu Yak Hacker

2365 Posts

Posted - 2010-09-24 : 16:51:52
And if it is not just ones and zeros:

SELECT LEN(@COL)-LEN(REPLACE(@COL,'1',''))


________________________________________________
If it is not practically useful, then it is practically useless.
________________________________________________
Go to Top of Page
   

- Advertisement -