[Last Call] Learn how to a build a cloud-first strategyRegister Now


Conditional Update statement

Posted on 2012-08-28
Medium Priority
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
  • 3
LVL 66

Accepted Solution

Jim Horn earned 2000 total points
ID: 38341878
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 66

Expert Comment

by:Jim Horn
ID: 38341972
<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

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

Expert Comment

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

Featured Post

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
Suggested Courses

831 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