select address (first 20 character) as Address1 ,
address (characters after first 20 if less then 20 then NULL) as Address2
from customer
如何在20个字符后选择字符串?
答案 0 :(得分:4)
获取前20个字符后的字符(请注意,如果没有20个字符,该函数将返回一个空字符串):
SELECT SUBSTRING('Some Random Address That is Longer than 20 characters' FROM 20);
现在,如果您需要地址2为NULL,则首先检查字符长度:
SELECT if(char_length(address) > 20, SUBSTRING(address FROM 20), NULL);
要获取前20个字符,可以使用子字符串函数,如下所示:
SELECT SUBSTRING('Some Random Address', 1, 20);
现在最终查询可能如下所示:
SELECT SUBSTRING(address, 1, 20) as Address1,
IF(CHAR_LENGTH(address) > 20, SUBSTRING(address FROM 20), NULL) as Address2
FROM customer
答案 1 :(得分:3)
您正在寻找substring
:
SELECT SUBSTRING(address, 1, 20) AS Address1,
SUBSTRING(address FROM 20) AS Address2
FROM customer