How can I count the number of 0's and 1's in a variable?

Samantha J T Star

Given a variable that contains a series of 0's and 1's how can I count the number of each in the variable?

Here's an example:

SET @AnswerGridCorrect = '0010010';

What I need to do is to return a comment into a variable called @Hint such that it will contain:

"Select 2 out of 7 choices"
Giorgos Betsos

Use this:

DECLARE @AnswerGridCorrect VARCHAR(MAX)
DECLARE @Question VARCHAR(MAX)

SET @AnswerGridCorrect = '0010010';

SET @question = 'Select ' + CAST(LEN(@AnswerGridCorrect) - LEN(REPLACE(@AnswerGridCorrect, '1', '')) AS VARCHAR(MAX)) +
                ' out of ' + CAST(LEN(@AnswerGridCorrect) AS VARCHAR(MAX)) + ' choices'

SELECT @Question

Output:

Select 2 out of 7 choices

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I count the number of days between a 1 and the next 0

From Dev

To count number of 1's,0's,2's in a string in java

From Dev

How to count the number of 0s between 1s in R?

From Dev

How to count number of 1's in the matrix

From Dev

Count number of 1's from right to left, stopping at the first 0

From Dev

How to reverse a number with 0 in the 1's place?

From Dev

How to match an even number of 1's and any amount of 0's

From Dev

How can i convert character 0s and 1s read from a txt File to byte?

From Dev

How can I generate this matrix (containing only 0s and ±1s)?

From Dev

How to randomly generate sequences of 0's and 1's that have an even number of 1's or odd number of 0's?

From Dev

How can I count the number of

From Dev

How can I find lines with grep count=[some number >0] if my file contains count=0 as well as count=!0?

From Dev

How can I check if a variable is a number between 0 and 999?

From Dev

How do I add a variable's number to another variable in batch?

From Dev

Given a string which consists of only 0, 1 or 2s, count the number of substring which have equal number of 0s, 1s and 2s

From Dev

How can I create a C++ constructor that accepts a variable number of int's

From Dev

How can I create a C++ constructor that accepts a variable number of int's

From Dev

How to generate a random number with fixed length only with 0's and 1's and a fixed amount of 1's?

From Dev

Find number of 1's in sequence of 0's and 1's

From Dev

How do I remove the leading 0's from a number?

From Dev

sql query to count number of -1's and 1's present in a column

From Dev

sql query to count number of -1's and 1's present in a column

From Dev

How can I use a variable's value in a variable name in JavaScript?

From Dev

How can I dynamically count an object wether it's paginated or not?

From Dev

How can I quickly get a count of the messages in a user's mailbox?

From Dev

Regular expression for even number of 0's and even number of 1's

From Dev

MATLAB-How to count 0s and 1s in a vector

From Dev

How count the 1's and 0's with a bit return type and return it in two separate columns

From Dev

MATLAB-How to count 0s and 1s in a vector

Related Related

  1. 1

    How can I count the number of days between a 1 and the next 0

  2. 2

    To count number of 1's,0's,2's in a string in java

  3. 3

    How to count the number of 0s between 1s in R?

  4. 4

    How to count number of 1's in the matrix

  5. 5

    Count number of 1's from right to left, stopping at the first 0

  6. 6

    How to reverse a number with 0 in the 1's place?

  7. 7

    How to match an even number of 1's and any amount of 0's

  8. 8

    How can i convert character 0s and 1s read from a txt File to byte?

  9. 9

    How can I generate this matrix (containing only 0s and ±1s)?

  10. 10

    How to randomly generate sequences of 0's and 1's that have an even number of 1's or odd number of 0's?

  11. 11

    How can I count the number of

  12. 12

    How can I find lines with grep count=[some number >0] if my file contains count=0 as well as count=!0?

  13. 13

    How can I check if a variable is a number between 0 and 999?

  14. 14

    How do I add a variable's number to another variable in batch?

  15. 15

    Given a string which consists of only 0, 1 or 2s, count the number of substring which have equal number of 0s, 1s and 2s

  16. 16

    How can I create a C++ constructor that accepts a variable number of int's

  17. 17

    How can I create a C++ constructor that accepts a variable number of int's

  18. 18

    How to generate a random number with fixed length only with 0's and 1's and a fixed amount of 1's?

  19. 19

    Find number of 1's in sequence of 0's and 1's

  20. 20

    How do I remove the leading 0's from a number?

  21. 21

    sql query to count number of -1's and 1's present in a column

  22. 22

    sql query to count number of -1's and 1's present in a column

  23. 23

    How can I use a variable's value in a variable name in JavaScript?

  24. 24

    How can I dynamically count an object wether it's paginated or not?

  25. 25

    How can I quickly get a count of the messages in a user's mailbox?

  26. 26

    Regular expression for even number of 0's and even number of 1's

  27. 27

    MATLAB-How to count 0s and 1s in a vector

  28. 28

    How count the 1's and 0's with a bit return type and return it in two separate columns

  29. 29

    MATLAB-How to count 0s and 1s in a vector

HotTag

Archive