| | |
Altering data when reading it from a database
Please support our MS SQL advertiser: Intel Parallel Studio Home
![]() |
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?
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?
If in doubt, reach into the trash can and remove the user guide.
•
•
Join Date: Sep 2006
Posts: 44
Reputation:
Solved Threads: 3
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.
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?
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.
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.
If in doubt, reach into the trash can and remove the user guide.
MS SQL Syntax (Toggle Plain Text)
SQL> CREATE TABLE colors(color_id number(1)); TABLE created SQL> INSERT INTO colors VALUES(1); 1 row inserted SQL> INSERT INTO colors VALUES(2); 1 row inserted SQL> INSERT INTO colors VALUES(3); 1 row inserted SQL> commit; Commit complete SQL> SELECT * FROM colors; COLOR_ID -------- 1 2 3 SQL> SELECT decode(color_id,1,'Red',2,'Blue',3,'Green') FROM colors; DECODE(COLOR_ID,1,'RED',2,'BLU ------------------------------ Red Blue Green
Is this what you wanted?
If no then just ignore :o Just wanted to help ..
All the best ..
![]() |
Similar Threads
- Extracting data from large SINGLE-table database to MULT-table relational database (MySQL)
- Read data from Access Database through PHP (PHP)
- data sorts in reverse order!! help!! (C)
- How to Hash and Store in Database (ASP.NET)
- Filtering data from an access database (Visual Basic 4 / 5 / 6)
Other Threads in the MS SQL Forum
- Previous Thread: SQL Queries/Report
- Next Thread: generate database maintenence plan script
| Thread Tools | Search this Thread |





