MariaDB社区
标题:
一句截短数据的SQL:把字符串中的其中一位去掉
[打印本页]
作者:
kider
时间:
2007-11-29 15:52
标题:
一句截短数据的SQL:把字符串中的其中一位去掉
数据如下:
select uid,username,password from cdb_members where username like 'GTO%';
uid username password
2 GTO0000000 e10adc3949ba59abbe56e057f20f883e
3 GTO0000001 e10adc3949ba59abbe56e057f20f883e
4 GTO0000002 e10adc3949ba59abbe56e057f20f883e
5 GTO0000003 e10adc3949ba59abbe56e057f20f883e
6 GTO0000004 e10adc3949ba59abbe56e057f20f883e
7 GTO0000005 e10adc3949ba59abbe56e057f20f883e
8 GTO0000006 e10adc3949ba59abbe56e057f20f883e
......
111 GTO0000109 e10adc3949ba59abbe56e057f20f883e
112 GTO0000110 e10adc3949ba59abbe56e057f20f883e
复制代码
目的:缩短username列的一个数据长度,即去掉GTO0000001中间的一个‘0’, 成 GTO000001 。
方法/脚本:
select CONCAT(left(username,5),LPad(right(username,3),3,'0')) from cdb_members where username like 'GTO%';
说明:
CONCAT(
str1
,
str2
,...)
返回结果为连接参数产生的字符串。如有任何一个参数为NULL ,则返回值为 NULL。或许有一个或多个参数。 如果所有参数均为非二进制字符串,则结果为非二进制字符串。 如果自变量中含有任一二进制字符串,则结果为一个二进制字符串。一个数字参数被转化为与之相等的二进制字符串格式;若要避免这种情况,可使用显式类型 cast, 例如: SELECT CONCAT(CAST(int_col AS CHAR), char_col)
mysql>
SELECT CONCAT('My', 'S', 'QL');
-> 'MySQL'
LPAD(
str
,
len
,
padstr
)
返回字符串
str
, 其左边由字符串
padstr
填补到
len
字符长度。假如
str
的长度大于
len
, 则返回值被缩短至
len
字符。
mysql>
SELECT LPAD('hi',4,'??');
LEFT(
str
,
len
)
返回从字符串
str
开始的
len
最左字符。
mysql>
SELECT LEFT('foobarbar', 5);
-> 'fooba'
-> '??hi'
时间仓促,随手写的,供参考。
作者:
boxman
时间:
2007-11-29 18:03
补充一个方法:
concat(substring(username from 1 for 8),substring(username from -1 for 1))
substring(username from 1 for 8) 取username开始的头8个字符
substring(username from -1 for 1) 取username末尾一个字符
欢迎光临 MariaDB社区 (http://123.56.88.72/)
Powered by Discuz! X3.2