using Case statement

  • I have created a table Employee with empID and Salary. Using the case statement I tried to add a column called New Salary, which I tried to obtain by multiplying the empID with his existing salary.i wrote the query like this,but it is giving error.Please tell me the correct way to write this query

    select empID,Salary,Case empID when 1 then 1* salary

    when 2 then 2* salary

    when 3 then 3* salary

    End NewSalary,

    from Employee

    Please tell me is it even possible to write this query using case statement.

    Thanks in advance

  • t.mounika01 (8/17/2014)


    I have created a table Employee with empID and Salary. Using the case statement I tried to add a column called New Salary, which I tried to obtain by multiplying the empID with his existing salary.i wrote the query like this,but it is giving error.Please tell me the correct way to write this query

    select empID,Salary,Case empID when 1 then 1* salary

    when 2 then 2* salary

    when 3 then 3* salary

    End NewSalary,

    from Employee

    Please tell me is it even possible to write this query using case statement.

    Thanks in advance

    The only problem I've found in the query is the comma after the NewSalary

    😎

    USE tempdb;

    GO

    DECLARE @EMP TABLE

    (

    empID INT IDENTITY(1,1) NOT NULL

    ,empName VARCHAR(10) NOT NULL

    ,Salary MONEY NOT NULL

    );

    INSERT INTO @EMP (empName,Salary)

    VALUES ('Joe',10000),('Jeff',10000),('Jim',10000);

    select empID,Salary,Case empID when 1 then 1* salary

    when 2 then 2* salary

    when 3 then 3* salary

    End NewSalary

    from @EMP

    Results

    empID Salary NewSalary

    ----------- --------------------- ---------------------

    1 10000.00 10000.00

    2 10000.00 20000.00

    3 10000.00 30000.00

  • oh..thank you 🙂

  • t.mounika01 (8/17/2014)


    oh..thank you 🙂

    No worries. Just a quick question, it puzzles me how an empID can bear any weight on a salary, can you explain this?

    😎

  • Homework problem?

  • nope..empID doesnt bear any weight on salary..I just came up with some dummy numbers while practicing the case statement..

  • nope:-)

  • Good to know. It had the makings of one with the odd requirements....

Viewing 8 posts - 1 through 7 (of 7 total)

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