深度发掘SQL Server 2000 UDF(下)

2008-04-02 10:31:00来源:互联网 阅读 ()

新老客户大回馈,云服务器低至5折

  大小写转换函数

  该函数有两个参数:@String和@Capitalize_What。

  依据 @Capitalize_What的值,函数有不同的功能:

  ¨ @Capitalize_What = ‘string’“

  函数将 @string的第一个非空字符转换成大写, 其余部分改为小写。

  ¨ @Capitalize_What = ‘sentence’

  函数将 @string中的每一句的首个非空字符转换为大写,句子其余部分转换为小写。断句的依据是’.’、’!’、’?’

  ¨ @Capitalize_What = ‘word’

  函数将 @string中的每个词都转换成首字符大写,其余小写的形式。

  CREATE FUNCTION dbo.Capitalize (

  -- Capitalize the first character of every word,

  -- sentence, or the whole string. Put the rest to lowercase.

  @String VARCHAR (8000),

  @Capitalize_What VARCHAR (8) = ’string’

  -- String: Capitalize the first letter of the string

  -- Sentence: Capitalize the first letter of every sentence.

  -- Delimiters: ./!/?

  -- Word: Capitalize the first letter of every word.

  -- Delimiters: any characters other than letters and digits.

  )

  RETURNS VARCHAR(8000)

  AS

  BEGIN

  DECLARE @Position SMALLINT,

  @Char CHAR(1),

  @First_Char CHAR (1),

  @Word_Start SMALLINT


  SET @Capitalize_What = LOWER( @Capitalize_What )

  SET @Word_Start = 0

  IF @Capitalize_What IN (‘word’, ‘sentence’)

  BEGIN

  SET @Position = DATALENGTH( @String )

  WHILE @Position >= 0 BEGIN

  SET @Char = CASE @Position

  WHEN 0 THEN ’.’

  ELSE UPPER( SUBSTRING(

  @String, @Position,

  1 ) )

  END

  IF @Char BETWEEN ’A’ AND ’Z’

  OR @Char BETWEEN ’0’ and ’9’ BEGIN

  SET @Word_Start = @Position

  SET @First_Char = UPPER( @Char )

  END

  ELSE BEGIN

  IF @Capitalize_What = ’word’

  OR @Char in ( ’.’, ’!’, ’?’ ) BEGIN

  IF @Word_Start > 0

  AND @First_Char BETWEEN ’A’

  AND ’Z’

  SET @String = STUFF(

  @String, @Word_Start,

  1, @First_Char )

  SET @Word_Start = 0

  END

  END

  SET @Position = @Position - 1

  END

  END

  ELSE BEGIN -- Capitalize the first character

  SET @Position = 0

  WHILE @Position < DATALENGTH( @String )

  BEGIN

  SET @Position = @Position 1

  SET @Char = UPPER( SUBSTRING( @String,

  @Position, 1 ) )

  IF @Char BETWEEN ’A’ AND ’Z’

  OR @Char BETWEEN ’0’ AND ’9’ BEGIN

  SET @String = STUFF( @String,

  @Position, 1, @Char )

  SET @Position = 9999

  END

  END

  END

  RETURN( @String )

  END

  go

共2页。 1 2 :

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇: 数据库人员手边系列:SQL语句导入导出大全

下一篇: SQL SERVER中字段类型及说明