SQL gets the first letter of the string, loops

Source: Internet
Author: User

//string First LetterCREATE FUNCTION Getinitialletter (@ChineseString NVARCHAR (4000)) RETURNS NVARCHAR (4000) Asbegin DECLARE @SingleCharacter NCHAR (1), @ReturnString NVARCHAR (4000) SET @ReturnString="'While LEN (@ChineseString)>0BEGIN--take a single character set @SingleCharacter= Left (@ChineseString,1) ----kanji characters, return characters corresponding to the first letter, non-kanji characters, return the original character if (UNICODE (@SingleCharacter) between19968and19968+20901) SET @ReturnString= @ReturnString +(SELECT TOP1PY from (SELECT'A'As Py,n'ao'As chinesecharacters UNION all SELECT'B'N'Book'UNION all SELECT'C'N'wrong'UNION all SELECT'D'N'鵽'UNION all SELECT'E'N'樲'UNION all SELECT'F'N'Leiognathi'UNION all SELECT'G'N'Hiker'UNION all SELECT'H'N'inceѕt'UNION all SELECT'J'N'攈'UNION all SELECT'K'N'穒'UNION all SELECT'L'N'鱳'UNION all SELECT'M'N'Temperature'UNION all SELECT'N'N'桛'UNION all SELECT'O'N'retting'UNION all SELECT'P'N'Exposure'UNION all SELECT'Q'N'囕'UNION all SELECT'R'N'鶸'UNION all SELECT'S'N'蜶'UNION all SELECT'T'N'Sheath'UNION all SELECT'W'N'Mo'UNION all SELECT'X'N'鑂'UNION all SELECT'Y'N'Rhyme'UNION all SELECT'Z'N' the') spellingtablewhereChinesecharacters > =@SingleCharacter COLLATE chinese_prc_cs_as_ks_ws ORDER by PY ASC) Elseset @ReturnString= @ReturnString +@SingleCharacter SET @ChineseString= Right (@ChineseString, LEN (@ChineseString)-1) END RETURN @ReturnString Endgo//Invocation ModeSELECT dbo. Getinitialletter ('Zhong Kai')
//LoopsDECLARE @id varchar ( -) declare @name varchar ( -) DECLARE my_cursor cursor for(SelectId,name fromcommunity) Open My_cursor;fetch next frommy_cursor into @id, @name; while@ @FETCH_STATUS =0begin print @name; Update CommunitySetEnglish_name= (dbo. Getinitialletter (@name))whereId=@id; FETCH NEXT frommy_cursor into @id, @name; Endclose my_cursor;deallocate My_cursor;go

SQL gets the first letter of the string, loops

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.