如何在Kusto Query Language中截取指定子字符串?
在KQL中提取指定子串起始的部分内容
你可以结合使用substring()和indexof()函数来实现需求:
- 先用
indexof()定位目标子串的起始位置 - 再用
substring()从该位置开始截取到字符串末尾
示例代码:
let errorString = 'Execution fail against sql server. Please contact SQL Server team if you need further support. Sql error number: 50000. Error Message: Invalid length parameter passed to the LEFT or SUBSTRING function.'; print extractedPart = substring(errorString, indexof(errorString, 'Error Message:'))
执行这段代码后,输出结果就是你需要的:
Error Message: Invalid length parameter passed to the LEFT or SUBSTRING function.
如果担心目标子串不存在导致报错,可以用iif()做容错处理:
let errorString = 'Execution fail against sql server. Please contact SQL Server team if you need further support. Sql error number: 50000.'; print extractedPart = iif(indexof(errorString, 'Error Message:') != -1, substring(errorString, indexof(errorString, 'Error Message:')), '未找到Error Message部分')
内容的提问来源于stack exchange,提问作者AndreasB
相关产品推荐
相关产品推荐

