Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to map a value in sql into a string value

Tags:

sql

mysql

I have a column in DB table named type. It have values like 01,78,75,98,02,11 etc. I want to map these values into a string value like, If the value starts with 01 map it into a string value named type01, and if starts with 78 the value (select result value) must be type7 etc. Any idea guys how to do it? Thanks in advance.

SQL will be somehow like below:

Select (if(type left(2) = 07 then type7), t.dept

from team t

LEFT JOIN typetable

ON typetable.id=t.team_id;
like image 585
vellattukudy Avatar asked Apr 24 '26 10:04

vellattukudy


1 Answers

It seems you may want a case statement if I understand correctly what you are asking.

SELECT CASE type WHEN '01' THEN 'type01' WHEN '02' THEN 'type02' WHEN '03' THEN 'type03' WHEN '04' THEN 'type04' END FROM team LEFT JOIN typetable ON typetable.id=t.team_id;

like image 61
Cannon Avatar answered Apr 25 '26 23:04

Cannon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!