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"
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.
Comments