mysql函数2--字符串函数

-- 字符串函数
-- 字符串函数
-- 1.26计算字符串字符数的函数和字符串长度的函数
select char_length('char'),char_length('string'),char_length('egg');
-- 1.27 使用length函数计算字符串长度
select length('char'),length('string'),length('egg'),length('chicken');

-- 1.28合并字符串函数concat(s1,s2...),concat_ws(x,s1,s2...)
select concat('I','miss','you'),concat('where','are','you'),concat('hello',null);
-- 1.29使用concat_ws()函数连接带分隔符的字符串
select concat_ws(' ','I', 'miss' , 'you'),concat_ws(',', 'where', 'are' , 'you'),concat_ws('null','ss','dd'),concat_ws(' ','dd','fd',null);

-- 1.30替换字符串的函数insert(s1,x,len,s2)
select insert ('icomefromchina',2,4,'goto') coll1,insert('quest',-1,4,'what') coll2,insert ('quest',3,100,'wh') coll3;

-- 1.31 使用lower函数或者lcase函数将字符串中所有字母字符转换为小写
select lower('QUERY'),lcase('Well');
-- 1.32 使用upper函数或者ucase函数将所有字符串中所有字母字符转换为大写
select upper('query'),ucase('Well');

--  获取指定长度的字符串中的函数left(s,n)和right(s,n)
-- 1.33使用left(s,n)函数
select left('query',3),left('美丽的祖国',2);
-- 1.34 使用right函数返回字符串中右边的字符
select right('query',2),right('魅力的男人',3);

-- 1.35填充字符串的函数lpad(s1,len,s2)和rpad(s1,len,s2)
select lpad('hello',3,'??'),lpad('hello',10,'??');
-- 1.36 使用rpad
select rpad('hello',3,'??'),rpad('hello',10,'??');

-- 1.37 删除空格的函数ltrim(s),rtrim(s),和trim(s)
select '  cook  ',ltrim('  cook  ');
-- 1.38 删除右边空格的函数
select concat('(' ,rtrim('  cook  '),')');
-- 1.39 删除空格
select concat('(',trim('  cook  '),')');

-- 1.40删除指定字符串的函数trim(s1 from s)
select trim('xy' from 'xyxboxyokxxyxy');
-- 1.41 重复生成字符串的函数repeat(s,n)
select repeat('hello',3);

-- 空格函数space(n)和替换函数replace(s,s1,s2)
-- 1.42使用space(n)函数生成由空格组成的字符串
select concat('hello',space(1),'i',space(2),'miss',space(3),'you');
-- 1.43使用replace函数进行字符串替代操作
select replace('xxx.baidu.com','x','w');

-- 比较字符串大小函数strcmp(s1,s2)
-- 1.44 使用strcmp函数比较大小
select strcmp('txt','txt2'), strcmp('txt2','tsdsft2'),strcmp('txt','txt');

-- 获取子串的函数substring(s,n,len)和mid(s,n,len)
-- 1.45使用substring函数获取指定位置处的子字符串
select substring('breakfast',5) coll1;
select substring('breakfast',5,3) coll2;
select substring('lunch',-3) coll3;
select substring('lunch',-5,3) coll4;
-- 1.46 使用mid()函数
select mid('breakfast',5) col1;
select mid('breadfast',5,3)col2;
select mid('lunch',-3)col3;
select mid('lunch',-5,3)col4;

-- 1.47 使用locate,position,instr函数查找字符串中指定字符串的开始位置
select locate('ball','football'),position('ball' in 'football'),instr('football','ball');

-- 1.48使用reverse函数翻转字符串
select reverse('football');

-- 1.49使用elt函数返回指定位置字符串
select elt(3,'hello','world','byebye'),elt(5,'hello','world','byebye');

-- 1.50使用field函数返回指定字符串第一次出现的位置
select field('e','hello','e','fj');

-- 1.51使用find_in_set()函数返回字符串在字符串列表中的位置
select find_in_set('s','strlist,s,ss');

-- 1.52 使用make_set根据二进制位选取指定字符串
select make_set(1,'a','b','c')col1,make_set(1|4,'hello','nice','world')col2;











猜你喜欢

转载自blog.csdn.net/weixin_42321963/article/details/81004228