sql处理字符串: replace替换, substring_index拆分和处理json

replace

语法:
REPLACE ( string_expression , string_pattern , string_replacement )

参数:

  • string_expression 要搜索的字符串表达式。string_expression 可以是字符或二进制数据类型。
  • string_pattern 是要查找的子字符串。string_pattern 可以是字符或二进制数据类型。string_pattern 不能是空字符串 ('')。
  • string_replacement 替换字符串。string_replacement 可以是字符或二进制数据类型。

返回类型:

  • 如果其中的一个输入参数数据类型为 nvarchar,则返回 nvarchar;否则 REPLACE 返回 varchar。
  • 如果任何一个参数为 NULL,则返回 NULL。

上面都是官话,不好懂!翻成白话:REPLACE(String,from_str,to_str) 即:将String中所有出现的from_str替换为to_str。

substring_index

语法:
substring_index(str, delim, count)

参数:

  • str:需要拆分的字符串;
  • delim:分隔符,根据此字符来拆分字符串;
  • count:当 count 为正数,取第 n 个分隔符之前的所有字符; 当 count 为负数,取倒数第 n 个分隔符之后的所有字符
select SUBSTRING_INDEX('mooc_100018252','_',-1); // 结果: 100018252

处理json字段

  1. 获取指定json字符串中指定的属性值,以下三种写法等价:
json_extract(attributes_json,'$.DP') //获取json中指定的值
attributes_json->'$.DP'
attributes_json->>'$.DP' //可以有两个尖括号
  1. 去掉查询结果中首尾的双引号:
json_unquote()
如:
select json_unquote(json_extract(attributes_json,'$.DP')) as column_value from t_demand_point where instance_id=2146
posted @ 2020-06-04 09:09  林宇风  阅读(2683)  评论(0编辑  收藏  举报