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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM