如何显示数据字符的某些百分比。假设我需要显示名称中50%的字符,例如“讲师”。
考试中有人问我这个问题,我没有这样做。
create table employee(
e_id int(10) ,
empname char(10),
designation char(10),
salary int(10),
age int(10),
deptid int(10),
job varchar(30));
insert into employee values(101,'Hassan','TA',46000,20,10,"manager");
insert into employee values(102,'Tahir','Poem',43000,45,10,"manager");
insert into employee values(103,'Ahmed','Hand',36000,27,20,"Analyst");
insert into employee values(104,'Imran','lecturer',24000,27,30,"Manager");
insert into employee values(105,'Furqan','teacher',36000,22,30,"Salesman");
select empname
from employee
where length(empname/2);
在尝试时,我得到了雇员的全名,而不是其姓名的半角字符。
答案 0 :(得分:1)
可以使用LEFT()
和LENGTH()
:
SELECT LEFT(empname, LENGTH(empname)/2)
FROM employee
答案 1 :(得分:1)
希望获得帮助,
50
是您的百分比。您可以根据需要更改百分比。
SELECT SUBSTRING(empname, 1, ROUND( (length(empname)/100) * 50 ) ) AS ExtractString from employee