SQL 字符串截取左边指定长度字符串中文、英文
2010-07-14 18:38
495 查看
代码
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go
ALTER FUNCTION [dbo].[substring2] (@rescontent varchar(100), @nlength int,@thchar varchar(100) ) Returns varchar(255)
AS
BEGIN
declare @rest varchar(255)
if(datalength(@rescontent)>34)
set @rest=(substring(@rescontent,0,17)+''+@thchar)
else
set @rest=@rescontent
return @rest
END
select dbo.FN_MYSUBSTRING(infocontent_title,34) from t_p_infocontent
creater FUNCTION [dbo].[FN_MYSUBSTRING](
@Content nvarchar(4000),
@Size int
@suffix varchar(200)
)
RETURNS nvarchar(4000)
AS
BEGIN
-- SET @Content = N'Office of Commissioner Insurance - Establishment of an Independent Insurance Authority in Hong Kong (2009)'
-- SET @Size = 26
SET @Content = LTRIM(RTRIM(REPLACE( REPLACE(@Content,CHAR(10),' '),CHAR(13),' ')))
if @Size > 0 AND @Size < 2 * LEN (@Content)
begin
declare @Pos INT --Current pointer
DECLARE @sPos INT --Current stay pointer
DECLARE @PrePos INT --Previous stay pointer
declare @tempS nvarchar(4)
DECLARE @tempNextS nvarchar(4)
declare @tempSize INT
DECLARE @unicodeS INT
DECLARE @unicodeNextS INT
set @Pos = 1
set @sPos = 0
SET @prePos = 0
SET @tempS = N''
SET @tempNextS = N''
SET @tempSize = 0
WHILE @Pos < len(@Content)
BEGIN
IF @tempSize < @Size
BEGIN
SET @tempS = SUBSTRING(@Content,@Pos,1)
SET @tempNextS = SUBSTRING(@Content,@Pos+ 1,1)
SET @unicodeS = UNICODE(@tempS)
SET @unicodeNextS = UNICODE(@tempNextS)
IF @unicodeS > 254
BEGIN
SET @tempSize = @tempSize + 2
SET @sPos = @Pos
END
ELSE
BEGIN
SET @tempSize = @tempSize + 1
-- Do not turncate the word just length as @Size by add symbol before and after the word
IF @unicodeS < 48 OR (@unicodeS > 57 AND @unicodeS < 65) OR (@unicodeS > 90 AND @unicodeS < 97) OR @unicodeS > 122
OR @unicodeNextS < 48 OR (@unicodeNextS > 57 AND @unicodeNextS < 65) OR (@unicodeNextS > 90 AND @unicodeNextS < 97) OR @unicodeNextS > 122
BEGIN
SET @sPos = @Pos
END
END
END
IF @tempSize > @Size - 2 AND @unicodeNextS > 254 -- if next letter is Chinese then turncate the string
BEGIN
SET @tempSize = @Size
SET @sPos = @Pos
END
ELSE IF @tempSize = @Size AND @PrePos = @sPos -- else if the English word length is large than the @Size then turncate the word
BEGIN
SET @sPos = @Pos
END
IF @tempSize >= @Size
BEGIN
set @Content = left(@Content, @sPos)+@suffix
-- +'@'
-- + nchar(13)
-- + right(@Content, len(@Content) - @sPos)
SET @tempSize = 0
SET @PrePos = @sPos
set @Pos = @sPos + 1
END
set @Pos = @Pos + 1
SET @unicodeS = 0
SET @unicodeNextS = 0
END
END
RETURN @Content
END
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go
ALTER FUNCTION [dbo].[substring2] (@rescontent varchar(100), @nlength int,@thchar varchar(100) ) Returns varchar(255)
AS
BEGIN
declare @rest varchar(255)
if(datalength(@rescontent)>34)
set @rest=(substring(@rescontent,0,17)+''+@thchar)
else
set @rest=@rescontent
return @rest
END
select dbo.FN_MYSUBSTRING(infocontent_title,34) from t_p_infocontent
creater FUNCTION [dbo].[FN_MYSUBSTRING](
@Content nvarchar(4000),
@Size int
@suffix varchar(200)
)
RETURNS nvarchar(4000)
AS
BEGIN
-- SET @Content = N'Office of Commissioner Insurance - Establishment of an Independent Insurance Authority in Hong Kong (2009)'
-- SET @Size = 26
SET @Content = LTRIM(RTRIM(REPLACE( REPLACE(@Content,CHAR(10),' '),CHAR(13),' ')))
if @Size > 0 AND @Size < 2 * LEN (@Content)
begin
declare @Pos INT --Current pointer
DECLARE @sPos INT --Current stay pointer
DECLARE @PrePos INT --Previous stay pointer
declare @tempS nvarchar(4)
DECLARE @tempNextS nvarchar(4)
declare @tempSize INT
DECLARE @unicodeS INT
DECLARE @unicodeNextS INT
set @Pos = 1
set @sPos = 0
SET @prePos = 0
SET @tempS = N''
SET @tempNextS = N''
SET @tempSize = 0
WHILE @Pos < len(@Content)
BEGIN
IF @tempSize < @Size
BEGIN
SET @tempS = SUBSTRING(@Content,@Pos,1)
SET @tempNextS = SUBSTRING(@Content,@Pos+ 1,1)
SET @unicodeS = UNICODE(@tempS)
SET @unicodeNextS = UNICODE(@tempNextS)
IF @unicodeS > 254
BEGIN
SET @tempSize = @tempSize + 2
SET @sPos = @Pos
END
ELSE
BEGIN
SET @tempSize = @tempSize + 1
-- Do not turncate the word just length as @Size by add symbol before and after the word
IF @unicodeS < 48 OR (@unicodeS > 57 AND @unicodeS < 65) OR (@unicodeS > 90 AND @unicodeS < 97) OR @unicodeS > 122
OR @unicodeNextS < 48 OR (@unicodeNextS > 57 AND @unicodeNextS < 65) OR (@unicodeNextS > 90 AND @unicodeNextS < 97) OR @unicodeNextS > 122
BEGIN
SET @sPos = @Pos
END
END
END
IF @tempSize > @Size - 2 AND @unicodeNextS > 254 -- if next letter is Chinese then turncate the string
BEGIN
SET @tempSize = @Size
SET @sPos = @Pos
END
ELSE IF @tempSize = @Size AND @PrePos = @sPos -- else if the English word length is large than the @Size then turncate the word
BEGIN
SET @sPos = @Pos
END
IF @tempSize >= @Size
BEGIN
set @Content = left(@Content, @sPos)+@suffix
-- +'@'
-- + nchar(13)
-- + right(@Content, len(@Content) - @sPos)
SET @tempSize = 0
SET @PrePos = @sPos
set @Pos = @sPos + 1
END
set @Pos = @Pos + 1
SET @unicodeS = 0
SET @unicodeNextS = 0
END
END
RETURN @Content
END
相关文章推荐
- ASP截取字符串左边的限定长度(区别英文和中文)
- lua 含中文的字符串处理--分离字符、计算字符数、截取指定长度
- 汉字英文混合字符串 截取指定字符串长度
- 在C#中截取指定长度的中文字符串(效率提高2500倍)
- 从一个包含汉字和其他字符的字符串中截取指定字节长度的字符串,不可以出现中文乱码
- 在C#中截取指定长度的中文字符串(效率提高2500倍)
- 在C#中截取指定长度的中文字符串
- 字符串长度及截取(中文2字符,英文1字符)
- Sql版本除去html并截取指定长度的字符串
- 在C#中截取指定长度的中文字符串
- sql中从指定位置截取指定长度字符串
- 截取指定的字符长度(其中字符串中有中文,如果有半个中文则去掉)
- sql计算字符串中包含中文、英文字符长度
- 不区分汉字还是英文和数字,按指定字节长度截取字符串
- 【转载】C# 字符串截取指定长度的中文字符--精点
- [1]字符串按中文符占3位进行指定长度剪切[2]Double类型截取指定长度(指定长度=整数位+小数位)
- 写了一个字符串截取一定长度的代码,中文两个字符,英文一个字符,如果截断了自动用一个.或两个.来对齐,用于标题的显示
- 在C#中截取指定长度的中文字符串
- 在C#中截取指定长度的中文字符串.
- 用JavaScript截取一定长度字符串(中文长度为2,英文长度为1)