我的字段包含大写和小写字符混合的值。 我试图只返回大写值作为一个结果,同样只返回另一个小写值。我不是试图将一个转换为另一个,只是按原样返回当前数据。
我似乎找不到这样做的声明和" SUBSTRING"只返回我指定的值,即第一个和最后一个字符
因此,例如,如果我有AAbbCCdd并想要返回大写值,我需要的结果是AACC。
答案 0 :(得分:2)
有一个功能:
CREATE FUNCTION [dbo].[GetCased](@BUFFER VARCHAR(MAX), @GETUPPER BIT) RETURNS VARCHAR(MAX) AS
BEGIN
DECLARE @LEN INT = LEN(@BUFFER), @POS INT = 1, @CHAR CHAR(1), @RESULT VARCHAR(MAX) = ''
WHILE @POS <= @LEN BEGIN
SET @CHAR = SUBSTRING(@BUFFER, @POS, 1)
SET @RESULT += CASE WHEN @CHAR COLLATE Latin1_General_CS_AS =
CASE WHEN @GETUPPER = 1 THEN UPPER(@CHAR) ELSE LOWER(@CHAR) END COLLATE Latin1_General_CS_AS THEN @CHAR ELSE '' END
SET @POS += 1
END
RETURN @RESULT
END
...
select
dbo.GetCased('AAbbCCdd', 1) as 'all upper',
dbo.GetCased('AAbbCCdd', 0) as 'all lower'
或者
CREATE FUNCTION [dbo].[fnRemovePatternFromString](@BUFFER VARCHAR(MAX), @PATTERN VARCHAR(128)) RETURNS VARCHAR(MAX) AS
BEGIN
DECLARE @POS INT = PATINDEX(@PATTERN, @BUFFER COLLATE Latin1_General_CS_AS)
WHILE @POS > 0 BEGIN
SET @BUFFER = STUFF(@BUFFER, @POS, 1, '')
SET @POS = PATINDEX(@PATTERN, @BUFFER COLLATE Latin1_General_CS_AS)
END
RETURN @BUFFER
END
...
select
dbo.fnRemovePatternFromString('AAbbCCdd ', '%[ABCDEFGHIJKLMNOPQRSTUVWXYZ]%') as 'all lower'
dbo.fnRemovePatternFromString('AAbbCCdd ', '%[abcdefghijklmnopqrstuvwxyz]%') as 'all upper'
(不能使用[a-z])
答案 1 :(得分:1)
这是使用函数的另一种方式:
CREATE FUNCTION [dbo].returnUppers
(
@str AS varchar(Max)
)
RETURNS varchar(MAX)
AS
BEGIN
DECLARE @len INT
DECLARE @cc INT = 1
DECLARE @return VARCHAR(MAX) = ''
SELECT @len = LEN(@str)
WHILE @len >= @cc
BEGIN
IF UPPER(SUBSTRING(@str,@cc,1)) = SUBSTRING(@str,@cc,1) COLLATE sql_latin1_general_cp1_cs_as
SELECT @return = @return + SUBSTRING(@str,@cc,1)
SET @cc += 1
END
RETURN @return
END
GO
使用:
DECLARE @string VARCHAR(20) = 'AAbbCCdd'
SELECT dbo.returnUppers(@string)
返回AACC。你需要为降低器写一个类似的函数,只需将UPPER()改为LOWER()
即可答案 2 :(得分:0)
正如评论中所提到的 - 这应该在表示层中完成,而不是在SQL中完成。
然而,这并不能阻止它有点乐趣!
关键是使用区分大小写的排序规则。在这个例子中,我已经去了 SQL_Latin1_General_CP1_ CS _AS (“CS”= CaseSensitive。“CI”= CaseInsensitive)
虽然解决方案涉及循环(在这种情况下是递归CTE),但你永远不会在这种情况下获得良好的性能
DECLARE @t table (
a char(10)
);
INSERT INTO @t (a)
VALUES ('AbC')
, ('ABCDEFGHIJ')
, ('aBCdEFghij')
, ('AbcdefhhiJ')
, ('ABcdEFGhij')
;
--SELECT a
-- , a COLLATE SQL_Latin1_General_CP1_CS_AS As case_sensitive_collation
-- , Replace(a COLLATE SQL_Latin1_General_CP1_CS_AS, 'A', '#') As case_sensitive_replace
--FROM @t
--;
; WITH characters_to_replace AS (
SELECT number
, Char(number) As c
, Row_Number() OVER (ORDER BY number) As sequence
FROM dbo.numbers
WHERE number BETWEEN 1 AND 255 -- basic characters
AND number NOT BETWEEN 65 AND 90 -- Exclude capital A-Z
)
, replacements AS (
SELECT a As original_value
, Cast(a COLLATE SQL_Latin1_General_CP1_CS_AS As nvarchar(max)) As new_value
, Cast(0 As bigint) As sequence
FROM @t
UNION ALL
SELECT replacements.original_value
, Cast(Replace(replacements.new_value, characters_to_replace.c, '') As nvarchar(max))
, characters_to_replace.sequence
FROM replacements
INNER
JOIN characters_to_replace
ON characters_to_replace.sequence = replacements.sequence + 1
)
SELECT original_value
, new_value
FROM replacements
WHERE sequence = (SELECT Max(sequence) FROM characters_to_replace)
OPTION (MaxRecursion 255)
;