SQLTeam.com | Weblogs | Forums

How add calculation field in existing table


#1

Hi,

I am having table with ages. need to categorize the age to 0-5 = Low, 6-10 = Medium, 10= High. and add this new calculation field in the existing field. How to add this? Please help

Thanks,


#2

you need to write a update statement with case statement.


#3

Update TableName
SET
NEWAGEDESC =
(
CASE AGE
WHEN BETWEEN '0' AND '5' THEN 'Low'
WHEN BETWEEN '6' AND '9' THEN 'Medium'
WHEN '10' THEN 'High'
)


#4
  1. Add a column [NEWAGEDESC] to the table, and use @ppatel11282 code to set it. However, you then need to maintain this column, in sync with [AGE], for all future updates.

  2. Add a Calculated Column. Personally I hate these because in the past they have interfered with our ability to add VIEWs with INDEXES and other such side effects

  3. Create a VIEW with the calculated value, and use the VIEW instead of the TABLE when you need the calculated value.

Personally I would do #3 unless you actually want to REPLACE the existing AGE column with the NEWAGEDESC column, in whichc ase I would do #1 and then DROP the original AGE column.


#5

Thanks.

I am able to create the calculation and add the new column but it shows null when export the table.
Any thing missing.


#6

If you show the code that you used then folk here can make suggestions as to what might have gone wrong. Otherwise we are just guessing!


#7

Thank a Lot team. At Last I am able to get the results.