sqlserver資料庫中字元串分割函數:
使用語句1查詢出來的結果為張三,李四,王五。這個receivername字段裡存放了多個人的姓名。
1.select v.receivername from T_SimpleFlow_MainInfo where v.id=52;
使用語句2查詢出來的結果是按 ‘,’ 分割之後的結果,顯示為3列 分别為 張三 李四 王五 。
2.select sp.value from T_SimpleFlow_MainInfo v
cross apply string_split(v.receivername,',') sp where v.id=52 ;
使用sql查詢為空時指定傳回參數
文法:ISNULL ( check_expression , replacement_value )
check_expression** :将被檢查是否為 NULL的表達式。check_expression 可以是任何類型的。
replacement_value:在 check_expression 為 NULL時将傳回的表達式。replacement_value 必須與check_expresssion 具有相同的類型。
select ISNULL(null, '456'); 查詢結果為456
select ISNULL('123', '456'); 查詢結果為123
SQL LEN() 文法
SELECT LEN(column_name) FROM table_name
select len(ISNULL(null, '456')); 查詢結果為3
select len(isnull('1234','')); 查詢結果為4
CONCAT 函數 和 CHARINDEX 的組合使用
一 concat()函數
1、功能:将多個字元串連接配接成一個字元串。
2、 文法1:concat(str1, str2,...),傳回結果為連接配接參數産生的字元串沒有有分隔符
3.、文法2:concat(str1, seperator,str2,seperator,...),傳回結果為連接配接參數産生的字元串并且有分隔符
select concat(列名稱,',','123') from tableName;
二 charindex()函數
一、文法
CHARINDEX ( char ,string, [ start_location ] )
char 一個表達式,其中包含要查找的字元的序列
string 一個表達式,通常是一個為指定序列搜尋的列,string 屬于字元串資料類别。
start_location 開始在 string 中搜尋 char 時的字元位置
select * from tablename where id=1 and CHARINDEX('aaa', column)>0;
查詢列中column 是否包含'aaa' 字元串,存在的話>0 可以查詢出結果,如果不存在>0 則查不出結果(也可以使用=0)。
三 concat和charindex配合使用
update tableName set name=CONCAT(name, ',',#{name}) where id=#{id} and CHARINDEX(#{name},name)=0
sql查詢語句 如果字段為空,則給定預設值
select
case when nextActionName is null then '送出' else nextActionName end as nextActionName,
case when backActionName is null then '駁回' else backActionName end as backActionName,
case when wasterActionName is null then '廢棄' else wasterActionName end as wasterActionName
from Status where id=#{id} and StatusId = #{statusId}