Complete intercept string function (SQL)

xiaoxiao2021-03-06  104

Many places need to use a function of intercept strings. Many places are implemented in the background code. Here this is a function implemented in the database.

Set quoted_identifier on goset ANSI_NULLS ON GO

ALTER FUNCTION [DBO] .substr (@string varchar (500), - A character @Length INT to be intercepted, the number of Chinese characters to display, one Chinese character is equal to two English characters) ®returns varchar (500) AsbegindeClare @str varchar (500) - Save Return Strings Declare @count int declare @While Int Declare @strlen int - Delivery of the string Declare @char varchar (2) - Save the temporary character - initialization variable Set @ count = 0set @ while = 1set @ Strlen = LEN (@string) set @str = '' if (@Strlen> @Length) - Decision string is more than the long Begin While @Count <(@ length) -2) * 2 - Cyclic cut string begin set @ char = Substring (@ String, @ While, 1) set @ While = @ While 1 if (Len (@char) = 0) - Deconstive to end BREAK ; If (unicode (@char)> 128) - If the Unicode code is more than 128, it is considered to be Chinese beGin set @ count = @ count 2 end else begin set @ count = @ count 1 end set @ str = @ STR @Char if ((@ length * 2) - @ count) = 5 - Begin set @ char = substring (@ string, @ While, 1) if (unicode (@char) <128 or len (@char) = 0) Begin set @ str = @ Str @ Char set @ While = @ While 1 END ELSE BEGIN SET @ Str = @ Str '.' end set @ count = @ count 1 end endif@count> = (@ length-2) * 2 and @

CountSet @ str = @ Str '....'

end

Else

Begin set @ str = @ stringend - * / return @strend

Goset quoted_identifier off goset ANSI_NULLS ON Go

转载请注明原文地址:https://www.9cbs.com/read-126914.html

New Post(0)