-- ======================================== -- Author:<Oliver QIN> -- Date :<2016/01/13> -- Desc :<字符串提取数字/汉字/英文字母> -- ======================================== --提取数字 IF OBJECT_ID('dbo.Fn_Get_Number') is not null drop function dbo.Fn_Get_Number GO CREATE FUNCTION dbo.Fn_Get_Number(@str VARCHAR(100)) RETURNS VARCHAR(100) AS BEGIN WHILE PATINDEX('%[^0-9]%',@str) > 0 BEGIN set @str=stuff(@str,patindex('%[^0-9]%',@str),1,'') END RETURN @str END --数据测试 go select dbo.Fn_Get_Number('sg12jjf34djs') as '字符串中的所有数字' ------------------------------------------------------------- ---提取汉字 IF OBJECT_ID('DBO.CHINA_STR') IS NOT NULL DROP FUNCTION DBO.CHINA_STR GO CREATE FUNCTION DBO.CHINA_STR(@S NVARCHAR(100)) RETURNS VARCHAR(100) AS BEGIN WHILE PATINDEX('%[^吖-座]%',@S) > 0 SET @S = STUFF(@S,PATINDEX('%[^吖-座]%',@S),1,N'') RETURN @S END GO PRINT DBO.CHINA_STR('呵呵ABC123ABC') GO ------------------------------提取英文 IF OBJECT_ID('DBO.GET_STR') IS NOT NULL DROP FUNCTION DBO.GET_STR GO CREATE FUNCTION DBO.GET_STR(@S VARCHAR(100)) RETURNS VARCHAR(100) AS BEGIN WHILE PATINDEX('%[^a-z]%',@S) > 0 BEGIN set @s=stuff(@s,patindex('%[^a-z]%',@s),1,'') END RETURN @S END GO --测试 PRINT DBO.GET_STR('ABC123g') GO