Update with CASE statement

  • nice question.... query will execute successfully..

    but Objective: Update all the M's to F and all F's to M. will not work...

    Manik
    You cannot get to the top by sitting on your bottom.

  • manik123 (2/19/2013)


    nice question.... query will execute successfully..

    but Objective: Update all the M's to F and all F's to M. will not work...

    How so?

  • Koen Verbeeck (2/18/2013)


    Spent 10 minutes looking for the catch, only to realize there wasn't one.

    Thanks for the question.

    I spent another 2-3 re-reading the phrase because it reads very oddly written as above, but I was able to puzzle it out.



    --Mark Tassin
    MCITP - SQL Server DBA
    Proud member of the Anti-RBAR alliance.
    For help with Performance click this link[/url]
    For tips on how to post your problems[/url]

  • A 'confusingly' straightforward question 😉

    ___________________________________________________________________
    If I can answer a question then anyone can answer it..trying to reverse the logic.. :hehe:

  • sestell1 (2/19/2013)


    Andrew Diniz (2/19/2013)


    Carlo Romagnano (2/19/2013)


    The update may fails with error, if the default collation is case sensitive.

    UPDATE GenderUpdate

    SET Gender = CASE WHEN GENDER = 'M' then 'F' ELSE 'M' END

    Carlo's onto something. If the database does not have a CI collation the statement will fail with something like:

    Msg 207, Level 16, State 1, Line 1

    Invalid column name 'GENDER'.

    Since the database collation in the scenario is unknown, adoption of 3-valued logic would dictate a QotD point be awarded to all those who click 'submit' without selecting either True or False. 😉

    Good point, although technically (due to the wording of the question) the answer would still be true. 😉

    Actually no, the update wouldn't fail as the batch would be aborted on the previous statement (which would fail) so that the update would never be attempted - so the wording of the question wouldn't cover that case.

    Tom

  • jdamm (2/19/2013)


    Vel.Velez (2/19/2013)


    In SQL Server 2005, wouldn't this FAIL?

    --#2 Data

    INSERT INTO GenderUpdate (Gender)

    VALUES ('M'), ('M'),('M'),('M'),('M'),('M'),('F'),('F'),('F'),('F'),('F'),('F'),('F'),('F'),('F'),('F')

    If so, #4 would not get to execute?

    Correct - checking my results, my default instance is SQL 2005, and this line failed. Went over to the SQL 2008 R2 and no issues.

    J

    But SQL 2005 is no longer in mainstream support; unless a QotD specifies differently, only releases still in mainstream support should be considered; and this one doesn't specify that SQL 2005 is applicable, so it isn't.

    Tom

  • jdamm (2/19/2013)


    I enjoyed the question. Keep up the work in writing them (I can never seem to come up with a good question idea myself...)

    As for writing the question, I would have moved the select to the end, then asked "What is the output of Select Statement?"

    with options:

    a) F 10

    M 6

    b) F 6

    M 10

    c) F 10

    d) M 10

    J

    Surely no one could fall for C or D? An update statement that deletes 6 rows? Or did you mean 16 instead of 10?

    Tom

  • manik123 (2/19/2013)


    nice question.... query will execute successfully..

    but Objective: Update all the M's to F and all F's to M. will not work...

    You must have a very broken version of SQL Server if it doesn't work for you.

    Tom

  • +1

    Easy.

  • True or False: If a train leave Detroit going east at 40 MPH and a truck leaves Bosie Idaho going west three hours earlier at random speeds which will get to Peru first?

    Possible answers:

    A. Third Base

    B. Paris

    C. 2:00 GMT

    D. All the above

    Actual Answer: 38

    🙂

    Not all gray hairs are Dinosaurs!

  • What has happened to the venerable CASE expression? I am surprised no one has mentioned it thus far but there is no such thing as a CASE statement in SQL Server. In my estimation the entry as a QotD could have been disqualified on that point alone.

    CASE (Transact-SQL)

    There are no special teachers of virtue, because virtue is taught by the whole community.
    --Plato

  • Miles Neale (2/19/2013)


    True or False: If a train leave Detroit going east at 40 MPH and a truck leaves Bosie Idaho going west three hours earlier at random speeds which will get to Peru first?

    Possible answers:

    A. Third Base

    B. Paris

    C. 2:00 GMT

    D. All the above

    Actual Answer: 38

    🙂

    LOL That takes me back! Uh oh...isn't it "Boise," rather than "Bosie," or is there actually a Bosie, Idanno? Maybe Bosie is the next MS SQL release... 😉

    Appreciate the good topic - thanks.

  • Thanks for the easy question!

  • I answered the wrong question.:crying: "Objective: Update all the M's to F and all F's to M. " and the result of statement 4 will not be that objective. And as the question was phrased so confusingly I thought perhaps it meant did it succeed in meeting its objective. Clearly not, but then perhaps I ought to read questions more carefully!


    Tony

  • Tony Bater (2/22/2013)the result of statement 4 will not be that objective.

    Why not?

Viewing 15 posts - 31 through 45 (of 54 total)

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