943,844 Members | Top Members by Rank

Ad:
  • MS SQL Discussion Thread
  • Unsolved
  • Views: 2585
  • MS SQL RSS
Nov 15th, 2006
0

Altering data when reading it from a database

Expand Post »
Let's say I have a column within a table that contains codes {0,1,2,3}. These codes are for what color shirt are in stock. We can say 0 is for black, 1 is for white, 2 is for red, and 3 is for blue.

Then I run a SELECT query on this table and want to change the code to a more user friendly description like red or blue. This will be used for a View. How can I alter the data when it is read in?
Similar Threads
Reputation Points: 12
Solved Threads: 6
Junior Poster
Geek-Master is offline Offline
156 posts
since Dec 2004
Nov 16th, 2006
1

Re: Altering data when reading it from a database

Well, the bad way would be to do something like:

select case color when 0 then 'black' when 1 then 'white' when 2 then 'red' when 4 then 'blue' end as color from myTable

ideally, you would create a table with 2 columns, one an id {0,1,2,3} and another with the name, and then you would just join (outer join if you have nulls in your base table). This will allow you to expand your colour offerings by just adding rows to the color table, as opposed to modifying your view, or views if you will hardcode this in more than one place.

Let's say I have a column within a table that contains codes {0,1,2,3}. These codes are for what color shirt are in stock. We can say 0 is for black, 1 is for white, 2 is for red, and 3 is for blue.

Then I run a SELECT query on this table and want to change the code to a more user friendly description like red or blue. This will be used for a View. How can I alter the data when it is read in?
MCP
Reputation Points: 14
Solved Threads: 3
Light Poster
MCP is offline Offline
44 posts
since Sep 2006
Nov 16th, 2006
0

Re: Altering data when reading it from a database

The project I'm working on involves using a vendor made database. So I don't have an option of editing their structures. The case statement is probably my best bet.

So when you use the "select case color" statement, color is a column from the "physical" table right? And not just a temporary column created just for the case statement.
Reputation Points: 12
Solved Threads: 6
Junior Poster
Geek-Master is offline Offline
156 posts
since Dec 2004
Nov 17th, 2006
0

Re: Altering data when reading it from a database

So when you use the "select case color" statement, color is a column from the "physical" table right? And not just a temporary column created just for the case statement.
Yup, that's correct, color is the "physical" name of the column.
MCP
Reputation Points: 14
Solved Threads: 3
Light Poster
MCP is offline Offline
44 posts
since Sep 2006
Nov 21st, 2006
0

Re: Altering data when reading it from a database

MS SQL Syntax (Toggle Plain Text)
  1.  
  2. SQL> CREATE TABLE colors(color_id number(1));
  3. TABLE created
  4.  
  5. SQL> INSERT INTO colors VALUES(1);
  6. 1 row inserted
  7.  
  8. SQL> INSERT INTO colors VALUES(2);
  9. 1 row inserted
  10.  
  11. SQL> INSERT INTO colors VALUES(3);
  12. 1 row inserted
  13.  
  14. SQL> commit;
  15.  
  16. Commit complete
  17.  
  18. SQL> SELECT * FROM colors;
  19. COLOR_ID
  20. --------
  21. 1
  22. 2
  23. 3
  24.  
  25. SQL> SELECT decode(color_id,1,'Red',2,'Blue',3,'Green') FROM colors;
  26.  
  27. DECODE(COLOR_ID,1,'RED',2,'BLU
  28. ------------------------------
  29. Red
  30. Blue
  31. Green
  32.  


Is this what you wanted?

If no then just ignore :o Just wanted to help ..

All the best ..
Reputation Points: 529
Solved Threads: 10
Posting Pro in Training
Sulley's Boo is offline Offline
450 posts
since Dec 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in MS SQL Forum Timeline: SQL Queries/Report
Next Thread in MS SQL Forum Timeline: generate database maintenence plan script





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC