select dbo.sjGetPreStr('1234^5*6/-33_','[]-_=+*&^') ===> 1234
Create function [dbo].[sjGetPreStr](@str varchar(1000),@pointArr varchar(100)) returns varchar(1000) as begin declare @res varchar(1000) = '' declare @i int = 1 declare @c varchar(4) = '' declare @j int = 0 declare @min varchar(1000) = '' declare @temp varchar(1000) = '' set @str = rtrim(ltrim(@str)) set @pointArr = rtrim(ltrim(@pointArr)) set @min = @str if len(@str) = 0 or len(@pointArr) = 0 begin set @res = 'Parameters cannot be empty' end else begin while @i <= len(@pointArr) begin set @c = substring(@pointArr,@i,1) set @j = charindex(@c,@str) if @j > 0 begin set @temp = substring(@str,1,@j - 1) if len(@temp) < len(@min) begin set @min = @temp end end set @i = @i + 1 set @j = 0 end set @res = @min end return @res end标签:begin,set,varchar,数据源,sqlserver,str,end,declare,特殊字符 From: https://www.cnblogs.com/pandora2050/p/17662357.html