Conditional Update statement

Posted on 2012-08-28
Last Modified: 2012-08-28
Hello, I need to do a conditional update on the following table:

CREATE TABLE [#TimesheetReport](
      [DateWorked] [varchar](50),
      [JobNumber] [varchar](50),
      [HoursWorked] [decimal](8, 2),
      [MileageTotal] [decimal](8, 2),
      [MileageDescription] [varchar](250),
      [MileagePaid] [decimal](8, 2),
      [HotelAmountPaid] [decimal](8, 2),
      [MealsAmountPaid] [decimal](8, 2),
      [OtherAmountPaid] [decimal](8, 2),
      [ClientId] int

if clientid = 1

update #TimesheetReport
Set MileagePaid =  (MileageTotal * 1.00)


update #TimesheetReport
Set MileagePaid =  (MileageTotal * .51)

How do you do this type of update???
Question by:gogetsome
    LVL 65

    Accepted Solution

    Use a CASE block to test for the ClientID value.

    update #TimesheetReport
    Set MileagePaid =  CASE WHEN ClientID = 1 THEN  MileageTotal * 1.00 ELSE MileageTotal * .51 END
    LVL 65

    Expert Comment

    by:Jim Horn
    <potentially stupid comment>
    In the question the table is created, and the next step is the update, but a newly-created table will have no rows in it, so there's nothing to update.

    Not sure if the CREATE TABLE is there just to frame up the question, or if this is your total script.

    Author Closing Comment

    Exactly what I needed! Thanks,
    That table was just an example. I fill it first then perform the update. ;-)
    LVL 65

    Expert Comment

    by:Jim Horn
    Thought so.  Thanks for the grade.  Good luck with your project.  -Jim

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Introduced in Microsoft SQL Server 2005, the Copy Database Wizard ( is useful in copying databases and associated objects between SQL instances; therefore, it is a good migration and upgrade tool…
    International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
    Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
    Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now