Answered by:
how can I do this?

Question
-
I have a country_name column listing all countries and I want to create another column in the same table called fax_description and populate 'FAX TO ' + country_name so for every country it will add 'FAX TO BAHRAIN', 'FAX TO FRANCE', etc..
Jassim RahmaSunday, November 8, 2009 7:58 PM
Answers
-
Man I hate the EDIT button.
Here is an exampleDECLARE @t TABLE(id INT,country VARCHAR(20),fax_description VARCHAR(50)) INSERT INTO @t SELECT 1,'Bahrain',NULL UNION SELECT 2,'Qatar',NULL UNION SELECT 3,'France',NULL UNION SELECT 1,'USA',null SELECT * FROM @t UPDATE @t SET fax_description = 'Fax to ' + country FROM @t SELECT * FROM @t
Abdallah, PMP, ITIL, MCTS- Proposed as answer by Melissa Suciadi Monday, November 9, 2009 1:24 AM
- Marked as answer by Zongqing Li Friday, November 13, 2009 8:57 AM
Sunday, November 8, 2009 8:11 PM
All replies
-
I don't think you need to keep this as a separate column in your table. What purpose would it serve?
You can always select this information into an extra column in SELECT statement and return back to client, if needed, but there is no point of keeping redundant information in the database.
Premature optimization is the root of all evil in programming. (c) by Donald Knuth
Naomi Nosonovsky, Sr. Programmer-Analyst
My blog Sunday, November 8, 2009 8:06 PM -
I misspoke on the insert. You need to use the UPDATE function as follows
UPDATE TableName
SET fax_description = 'Fax to ' + country
FROM TableName
Abdallah, PMP, ITIL, MCTSSunday, November 8, 2009 8:07 PM -
Man I hate the EDIT button.
Here is an exampleDECLARE @t TABLE(id INT,country VARCHAR(20),fax_description VARCHAR(50)) INSERT INTO @t SELECT 1,'Bahrain',NULL UNION SELECT 2,'Qatar',NULL UNION SELECT 3,'France',NULL UNION SELECT 1,'USA',null SELECT * FROM @t UPDATE @t SET fax_description = 'Fax to ' + country FROM @t SELECT * FROM @t
Abdallah, PMP, ITIL, MCTS- Proposed as answer by Melissa Suciadi Monday, November 9, 2009 1:24 AM
- Marked as answer by Zongqing Li Friday, November 13, 2009 8:57 AM
Sunday, November 8, 2009 8:11 PM -
I agree that it is best not to store redundant information in the table. My first choice would be to add "FAX TO" in the application code when displaying data to the client. You could also add the text using a SELECT statement or computed column.
Dan Guzman, SQL Server MVP, http://weblogs.sqlteam.com/dang/Sunday, November 8, 2009 8:43 PM