我有一个包含大写行的数据库,我需要使用TSQL中的存储过程转换为句子大小写。 例如: 原始字符串:
THIS IS THE FIRST SENTENCE. THIS IS THE SECOND SENTENCE! THIS IS THE THIRD SENTENCE? THIS IS THE FOURTH SENTENCE.
转换字符串:
This is the first sentence. This is the second sentence! This is the third sentence? This is the fourth sentence.
答案 0 :(得分:3)
好吧,在CLR函数或表示层中可以更好地处理这种事情,但这里只有一种方法可以使用tsql:
<强>更新强>
我已经改进了我的代码以检测以数字开头的句子,以便忽略它们(即如果句子是4th sentence
,t
之后的4
仍然是小写的。)
DECLARE @OriginalString varchar(200) = 'THIS IS THE FIRST SENTENCE. THIS IS THE SECOND SENTENCE! THIS IS THE THIRD SENTENCE? 4TH SENTENCE.'
DECLARE @TargetString varchar(200),
@Index int = 2,
@CurrentChar CHAR(1),
@FoundBeginingOfSentence bit = 0,
@BeginingOfSentenceIsANumber bit = 0
SET @TargetString = LEFT(@OriginalString, 1) + LOWER(RIGHT(@OriginalString, LEN(@OriginalString)-1))
WHILE @Index < LEN(@OriginalString)
BEGIN
SET @CurrentChar = SUBSTRING(@OriginalString, @Index, 1)
SET @Index = @Index + 1
IF @CurrentChar IN('.', '!', '?')
BEGIN
WHILE @FoundBeginingOfSentence = 0 AND @Index < LEN(@OriginalString)
BEGIN
SET @CurrentChar = SUBSTRING(@OriginalString, @Index, 1)
IF PATINDEX('[0-9]', @CurrentChar) = 1
BEGIN
SELECT @FoundBeginingOfSentence = 1,
@BeginingOfSentenceIsANumber = 1
END
IF @BeginingOfSentenceIsANumber = 0
BEGIN
IF PATINDEX('[A-Z]', @CurrentChar) = 1 BEGIN
SET @FoundBeginingOfSentence = 1
SET @TargetString = LEFT(@TargetString, @Index-1) + UPPER(SUBSTRING(@TargetString, @Index, 1)) + SUBSTRING(@TargetString, @Index+1, LEN(@TargetString))
END
SET @Index = @Index + 1
END
END
END
SELECT @FoundBeginingOfSentence = 0,
@BeginingOfSentenceIsANumber = 0
END
注意:您可以将所需的任何句子分隔符添加到第一个if
语句中。