In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
Today, I will talk to you about how to customize the function in Sql Server. Many people may not know much about it. In order to make you understand better, the editor has summarized the following for you. I hope you can get something according to this article.
First, determine whether the field value is in Chinese.
-- SQL determines whether the field value has a Chinese 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 set @ CN=@CN+@word set @ str=right (@ str) Len (@ str)-1) end return @ CN end select dbo.fun_getCN ('ASDKG Forum KDL')-- Forum select dbo.fun_getCN (' ASDKG Forum KDL')-- Forum select dbo.fun_getCN ('ASDKDL')-- empty
Second, extract figures
IF OBJECT_ID ('DBO.GET_NUMBER2') IS NOT NULLDROP FUNCTION DBO.GET_NUMBER2GOCREATE FUNCTION DBO.GET_NUMBER2 (@ S VARCHAR) RETURNS VARCHAR (100) ASBEGINWHILE PATINDEX ('% [^ 0-9]%', @ S) > 0BEGINset @ s=stuff (@ s ABC123ABC' index ('% [^ 0-9]%', @ s),) ENDRETURN @ SENDGO-- Test PRINT DBO.GET_NUMBER ('hehe ABC123ABC') GO--123
Third, extract English
-- extract English IF OBJECT_ID ('DBO.GET_STR') IS NOT NULLDROP FUNCTION DBO.GET_STRGOCREATE FUNCTION DBO.GET_STR (@ S VARCHAR) RETURNS VARCHAR (100) ASBEGINWHILE PATINDEX ('% [^ an ABC123ABC']%', @ S) > 0BEGINset @ s=stuff (@ s) (@ s) ENDRETURN @ SENDGO-- test PRINT DBO.GET_STR ('hehe ABC123ABC') GO
Fourth, extract Chinese
-- extraction of Chinese IF OBJECT_ID ('DBO.CHINA_STR') IS NOT NULLDROP FUNCTION DBO.CHINA_STRGOCREATE FUNCTION DBO.CHINA_STR (@ S NVARCHAR) RETURNS VARCHAR (100) ASBEGINWHILE PATINDEX ('% [^ A-seat]%', @ S) > 0SET @ S = STUFF (@ S PATINDEX ('% [^ A-seat]%', @ S), 1 RETURN @ SENDGOPRINT DBO.CHINA_STR ('hehe ABC123ABC') GO
Fifth, filter duplicate fields (multiple methods)
-- filter repeating characters 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 (@ Stem1) @ SPLITSET @ S = @ SPLIT+@SIF RIGHT (@ SMag1) @ SPLITSET @ S = @ S+@SPLITWHILE CHARINDEX (@ SPLIT) @ S) > 0 AND LEN (@ S) 1BEGINSET @ INDEX = CHARINDEX (@ SPLIT,@S) SET @ TEMP = LEFT (@ SMagneCHARINDEX (@ SPLIT,@S,@INDEX+LEN (@ SPLIT) IF @ NEW IS NULLSET @ NEW = ISNULL (@ NEW,'') + @ TEMPELSESET @ NEW = ISNULL (@ NEW,'') + REPLACE (@ TEMP,@SPLIT,'') + @ SPLITWHILE CHARINDEX (@ TEMP,@S) > 0BEGINSET @ S=STUFF (@ SMagna 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 CGO-filter repeating characters 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 (@ SMagne1) SET @ S=REPLACE (@ SGram1) ENDRETURN @ NEWENDGOSELECT DBO.DISTINCT_STR2 ('AABCCD')-- ABCDGO
6. Split field values according to specific strings
IF OBJECT_ID ('DBO.SPLIT_STR') IS NOT NULLDROP FUNCTION DBO.SPLIT_STRGOCREATE FUNCTION DBO.SPLIT_STR (@ S varchar (8000),-- string containing multiple data items @ INDEX int,-- location of data items to get @ SPLIT varchar (10)-- data separator) 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 (@ SPLIT,@S+@SPLIT (@ SPLIT,@S+@SPLIT) + @ SPLITLEN,'') RETURN (ISNULL (LEFT (@ Smage (@ SPLIT,@S+@SPLIT)-1),') ENDGOPRINT DBO.SPLIT_STR ('AA | BB | CC',2,' |')-GO read the above Do you have any further understanding of how to customize functions in Sql Server? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.