Select Case

  • Giving the results into a table with the columns answer1, answer2, answer3, answer4, answer5,.. as "right" or "wrong". The first name and name is also included.

    I am trying to get the totel of the right answers, for example 3 of 7 answers

    <% select Case total

    case when rs("answers1")= "right" then 1 else 0 end +

    case when rs("answer2")= "right" then 1 else 0 end +

    case when rs("answer3")= "right" then 1 else 0 end +

    case when rs("answer4")= "right" then 1 else 0 end +

    end select %>

    the error message ist

    Microsoft VBScript compilation error '800a0401'

    Expected end of statement

    case when rs("antwort1")= "richtig" then 1 else 0 end +

    ------------------------------------^

    I have no idea yet.

  • I'm not sure why you posted this on a SQL Server forum when it is asp/VBScript error.

    It looks like you are trying to what you really want is something like this:

    This was supposed to be similar to what Lowell posted but putting the Less Than and Greater than symbols in the code block made it not work

    Or, and my VBscript skills are really rusty so this is more psuedo code:

    Dim i

    Dim j

    j = 0

    i = 1

    While i < rs.columns.count

    If Left(rs(i).Name, 7) = "answers" then

    If rs(i) = "right" Then

    j = j + 1

    End If

    End If

    i = i + 1

    Loop

  • looks like you are trying to do a sum on the web page, which is NOT the same a s a sql statement running on the server.

    on the server, the sql statement would be something like this:

    <% total = total + iif(rs("answer1")= "right" ,1,0)

    + iif(rs("answer2")= "right" ,1,0)

    + iif(rs("answer3")= "right" ,1,0)

    + iif(rs("answer4")= "right" ,1,0)

    Response.Write "Total Score:" & total %>

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply