您好,登錄后才能下訂單哦!
這篇文章主要講解了“SQL Server提取數字、英文以及中文的sql語句”,文中的講解內容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“SQL Server提取數字、英文以及中文的sql語句”吧!
--SQL 判斷字段值是否有中文create function fun_getCN(@str nvarchar(4000)) returns nvarchar(4000) as begin declare @word nchar(1),@CN nvarchar(4000) set @CN='' while len(@str)>0 begin set @word=left(@str,1) if unicode(@word) between 19968 and 19968+20901 set @CN=@CN+@word set @str=right(@str,len(@str)-1) end return @CN end select dbo.fun_getCN('ASDKG論壇KDL')--論壇select dbo.fun_getCN('ASDKG論壇KDL')--論壇select dbo.fun_getCN('ASDKDL')--空---------------------------------------提取數字IF OBJECT_ID('DBO.GET_NUMBER2') IS NOT NULLDROP FUNCTION DBO.GET_NUMBER2GOCREATE FUNCTION DBO.GET_NUMBER2(@S VARCHAR(100))RETURNS VARCHAR(100)ASBEGINWHILE PATINDEX('%[^0-9]%',@S) > 0BEGINset @s=stuff(@s,patindex('%[^0-9]%',@s),1,'')ENDRETURN @SENDGO--測試PRINT DBO.GET_NUMBER('呵呵ABC123ABC')GO--123----------------------------------------------------------------------提取英文IF OBJECT_ID('DBO.GET_STR') IS NOT NULLDROP FUNCTION DBO.GET_STRGOCREATE FUNCTION DBO.GET_STR(@S VARCHAR(100))RETURNS VARCHAR(100)ASBEGINWHILE PATINDEX('%[^a-z]%',@S) > 0BEGINset @s=stuff(@s,patindex('%[^a-z]%',@s),1,'')ENDRETURN @SENDGO--測試PRINT DBO.GET_STR('呵呵ABC123ABC')GO----------------------------------------------------------------------提取中文IF OBJECT_ID('DBO.CHINA_STR') IS NOT NULLDROP FUNCTION DBO.CHINA_STRGOCREATE FUNCTION DBO.CHINA_STR(@S NVARCHAR(100))RETURNS VARCHAR(100)ASBEGINWHILE PATINDEX('%[^吖-座]%',@S) > 0SET @S = STUFF(@S,PATINDEX('%[^吖-座]%',@S),1,N'')RETURN @SENDGOPRINT DBO.CHINA_STR('呵呵ABC123ABC')GO----------------------------------------------------------------------過濾重復字符IF OBJECT_ID('DBO.DISTINCT_STR') IS NOT NULLDROP FUNCTION DBO.DISTINCT_STRGOCREATE FUNCTION DBO.DISTINCT_STR(@S NVARCHAR(100),@SPLIT VARCHAR(50))RETURNS VARCHAR(100)ASBEGINIF @S IS NULL RETURN(NULL)DECLARE @NEW VARCHAR(50),@INDEX INT,@TEMP VARCHAR(50)IF LEFT(@S,1)<>@SPLITSET @S = @SPLIT+@SIF RIGHT(@S,1)<>@SPLITSET @S = @S+@SPLITWHILE CHARINDEX(@SPLIT,@S)>0 AND LEN(@S)<>1BEGINSET @INDEX = CHARINDEX(@SPLIT,@S)SET @TEMP = LEFT(@S,CHARINDEX(@SPLIT,@S,@INDEX+LEN(@SPLIT)))IF @NEW IS NULL SET @NEW = ISNULL(@NEW,'')+@TEMPELSESET @NEW = ISNULL(@NEW,'')+REPLACE(@TEMP,@SPLIT,'')+@SPLITWHILE CHARINDEX(@TEMP,@S)>0BEGINSET @S=STUFF(@S,CHARINDEX(@TEMP,@S)+LEN(@SPLIT),CHARINDEX(@SPLIT,@S,CHARINDEX(@TEMP,@S)+LEN(@SPLIT))-CHARINDEX(@TEMP,@S),'')ENDENDRETURN RIGHT(LEFT(@NEW,LEN(@NEW)-1),LEN(LEFT(@NEW,LEN(@NEW)-1))-1)ENDGOPRINT DBO.DISTINCT_STR('A,A,B,C,C,B,C,',',')--A,B,CGO----------------------------------------------------------------------過濾重復字符2IF OBJECT_ID('DBO.DISTINCT_STR2') IS NOT NULLDROP FUNCTION DBO.DISTINCT_STR2GOCREATE FUNCTION DBO.DISTINCT_STR2(@S varchar(8000))RETURNS VARCHAR(100)ASBEGINIF @S IS NULL RETURN(NULL)DECLARE @NEW VARCHAR(50),@INDEX INT,@TEMP VARCHAR(50)WHILE LEN(@S)>0BEGINSET @NEW=ISNULL(@NEW,'')+LEFT(@S,1)SET @S=REPLACE(@S,LEFT(@S,1),'')ENDRETURN @NEWENDGOSELECT DBO.DISTINCT_STR2('AABCCD')--ABCDGO--------------------------------------------------------------------IF OBJECT_ID('DBO.SPLIT_STR') IS NOT NULLDROP FUNCTION DBO.SPLIT_STRGOCREATE FUNCTION DBO.SPLIT_STR(@S varchar(8000), --包含多個數據項的字符串@INDEX int, --要獲取的數據項的位置@SPLIT varchar(10) --數據分隔符)RETURNS VARCHAR(100)ASBEGINIF @S IS NULL RETURN(NULL)DECLARE @SPLITLEN intSELECT @SPLITLEN=LEN(@SPLIT+'A')-2WHILE @INDEX>1 AND CHARINDEX(@SPLIT,@S+@SPLIT)>0SELECT @INDEX=@INDEX-1,@S=STUFF(@S,1,CHARINDEX(@SPLIT,@S+@SPLIT)+@SPLITLEN,'')RETURN(ISNULL(LEFT(@S,CHARINDEX(@SPLIT,@S+@SPLIT)-1),''))ENDGOPRINT DBO.SPLIT_STR('AA|BB|CC',2,'|')--GO
感謝各位的閱讀,以上就是“SQL Server提取數字、英文以及中文的sql語句”的內容了,經過本文的學習后,相信大家對SQL Server提取數字、英文以及中文的sql語句這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關知識點的文章,歡迎關注!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。