如何从字段mysql的前20个字符之后选择字符 [英] how to select characters after first 20 characters from field mysql
本文介绍了如何从字段mysql的前20个字符之后选择字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
select address (first 20 character) as Address1 ,
address (characters after first 20 if less then 20 then NULL) as Address2
from customer
如何选择20个字符后的字符串?
How do I select string after 20 characters?
推荐答案
要获取前20个字符之后的字符(请注意,如果不存在20个字符,该函数将返回一个空字符串):
To get characters after the first 20 characters (note that if there are not twenty characters, the function will return an empty string):
SELECT SUBSTRING('Some Random Address That is Longer than 20 characters' FROM 20);
现在,如果您需要地址2为NULL,则首先检查字符长度:
Now if you need address 2 to be NULL, you check character length first:
SELECT if(char_length(address) > 20, SUBSTRING(address FROM 20), NULL);
要获取前20个字符,可以使用以下子字符串函数:
To get the first 20 characters, you can use the substring function like this:
SELECT SUBSTRING('Some Random Address', 1, 20);
现在,最终查询可能如下所示:
Now the final query could look like this:
SELECT SUBSTRING(address, 1, 20) as Address1,
IF(CHAR_LENGTH(address) > 20, SUBSTRING(address FROM 20), NULL) as Address2
FROM customer
这篇关于如何从字段mysql的前20个字符之后选择字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文