Applying trigger or functionality to a new column

  • I have a table like

    Name Age Salary Bonus

    1000 500

    Question 1

    I want a new column that has only four options to choose from

    a.High(Bonus >500)

    b.Medium(300<Bonus <500)

    c.Low(100<Bonus <300)

    d.Meager(Bonus < 100)

    How do i add this new column to my table with 50,000 rows already present.

    O/P shd look like

    Name Age Salary Bonus Type

    Tom 34 1000 555 High

    Tom 34 1000 50 Meager

    Tom 34 1000 255 Low

    Tom 34 1000 355 Medium

    Can any type of such functionality be added as a new column or we shd have done that while creating the table itself ?

    Like we do in Excel.. we add new column and apply a formula to it..

    So how do i do that in SQL Server

    [font="Verdana"]
    Today is the tomorrow you worried about yesterday:-)
    [/font]

  • 1 - Add new column. Check "alter table add column" syntax.

    2 - Write an update query to implement business logic.

    _____________________________________
    Pablo (Paul) Berzukov

    Author of Understanding Database Administration available at Amazon and other bookstores.

    Disclaimer: Advice is provided to the best of my knowledge but no implicit or explicit warranties are provided. Since the advisor explicitly encourages testing any and all suggestions on a test non-production environment advisor should not held liable or responsible for any actions taken based on the given advice.
  • You can do it like this

    ALTER TABLE tablename ADD columnname AS CASE WHEN Bonus < 100 THEN 'Meager'

    WHEN Bonus between 100 and 300 THEN 'Low'

    WHEN Bonus between 300 and 500 THEN 'Medium'

    WHEN Bonus > 500 THEN 'High' END

    Thanks & Regards,
    MC

  • only4mithunc (6/16/2010)


    You can do it like this

    ALTER TABLE tablename ADD columnname AS CASE WHEN Bonus < 100 THEN 'Meager'

    WHEN Bonus between 100 and 300 THEN 'Low'

    WHEN Bonus between 300 and 500 THEN 'Medium'

    WHEN Bonus > 500 THEN 'High' END

    Nice code..seeing update like this the first time.. 😎

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

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