To extract a section of a string we could use substr() in this way:
select substr(name, 1, 2) from pastry;
select substr(name, -2) from pastry;
The first first call shows us the first two character of all the pastry names; the second one all the last two characters.
We can combine the result to another useful string function, upper(), to convert to uppercase the string passed, and we can use the result to populate another field in the table:
update pastry
set description = upper(substr(name, 1, 2));
Here we'll have in description just the first two characters of the name in the same table row.
Post written while having fun reading Head First SQL
No comments:
Post a Comment