--字典对照转换函数
CREATE FUNCTION dbo.GetValueByKey (@input NVARCHAR(MAX), @key NVARCHAR(255),@defaultVal NVARCHAR(255))
RETURNS NVARCHAR(1000)
AS
BEGIN
DECLARE @start INT, @end INT, @value NVARCHAR(255);
SET @start = CHARINDEX(@key + ':', @input);
IF @start > 0
BEGIN
SET @start = @start + LEN(@key) + 1;
SET @end = CHARINDEX(',', @input, @start);
IF @end = 0
SET @end = LEN(@input) + 1;
SET @value = SUBSTRING(@input, @start, @end - @start);
END
RETURN ISNULL(@value,ISNULL(@defaultVal,''));
END
----使用示例,参数1:对照字符串,参数2:需要转换字符,参数3:默认值
SELECT dbo.GetValueByKey('1:双乳都检,2:左乳,3:右乳,4:未作检查', '5','-1') AS Value1;