MySQL change text to int - case statement?

Hi all,

I’m very new to php but I’m using it for my senior project which is a database application for my kids’ school.

I have a Grade field that was given to me as text (e.g., “Third Grade”, “High School”) I would like to change this to a numerical field for sorting. I have been trying to do this using a case statement before inserting the data into the permanent table. It’s in a temp table right now, which I pulled it into from a .csv file.

My question is, is there one query I could use to reassign values to the different grades while simultaneously inserting all the data into the new table?

Basically in the query below, Grade is an int and I want Grd to also be an int. Obviously, the below doesn’t work, but it’s just one of the many versions I’ve tried.

…something like this:

[php]$grdconvert = “SELECT CASE Grade
WHEN ‘Kindergarten’ THEN 0
WHEN ‘Third Grade’ THEN 3
WHEN ‘Eighth Grade’ THEN 8
END AS Grd
FROM MathStandards”;

$insertmath = “INSERT INTO StdLvl1(Grade, Description, SubjectID)
SELECT DISTINCT Grd, Level1Std, ‘1’ FROM MathStandards
ORDER BY Grade”;
[/php]
???

Don’t know of a way to do it through a query, but you can do it with a switch, in a round-about way, or, instead of using first, second, etc in the value of the input, use 1, 2, 3, and so on. That would be the easiest. You could also set up an array ($gd = array(‘First’ => 1, ‘Second’ => 2…).

Sponsor our Newsletter | Privacy Policy | Terms of Service