Goal is to replace a integer value that is returned in a SQL query with the char value that the number represents. For example:
A table attribute labeled ‘Sport’ is defined as a integer value between 1-4. 1 = Basketball, 2 = Hockey, etc. Below is the database table and then the desired output.
Database Table:
Player Team Sport
--------------------------
Bob Blue 1
Roy Red 3
Sarah Pink 4
Desired Outputs:
Player Team Sport
------------------------------
Bob Blue Basketball
Roy Red Soccer
Sarah Pink Kickball
What is best practice to translate these integer values for String values? Use SQL to translate the values prior to passing to program? Use scripting language to change the value within the program? Change database design?
MySQL has a
CASE
statement. The following works in SQL Server:definitely have the DB hold the string values. I am not a DB expert by any means, but I would recommend that you create a table that holds the strings and their corresponding integer values. From there, you can define a relationship between the two tables and then do a JOIN in the select to pull the string version of the integer.
Man, it's late - I hope I got that right. by the way, you should read up on the different types of Joins - this is the simplest example of one.
The
CASE
expression could help. However, it may be even faster to have a small table with an int primary key and aname
string such asetc, and
JOIN
it appropriately in the query.The database should hold the values and you should perform a join to another table which has that data in it.
So you should have a table which has say a list of people
And then another table which has a list of the sports
Then you would do a join between these tables
which would give you back
You could also use a case statement eg. just using the people table from above you could write something like
But that is certainly not best practice.
In oracle you can use the DECODE function which would provide a solution where the design of the database is beyond your control.
Directly from the oracle documentation:
Example: This example decodes the value warehouse_id. If warehouse_id is 1, then the function returns 'Southlake'; if warehouse_id is 2, then it returns 'San Francisco'; and so forth. If warehouse_id is not 1, 2, 3, or 4, then the function returns 'Non domestic'.
Do you think it would be helpful to store these relationships between integers and strings in the database itself? As long as you have to store these relationships, it makes sense to store it close to your data (in the database) instead of in your code where it can get lost. If you use this solution, this would make the integer a foreign key to values in another table. You store integers in another table, say
sports
, withsport_id
andsport
, and join them as part of your query.Instead of
SELECT * FROM my_table
you wouldSELECT * from my_table
and use the appropriate join. If not every row in your main column has a corresponding sport, you could use a left join, otherwise selecting from both tables and using = in the where clause is probably sufficient.