Reputation: 321
I have fields with values of a mix of both Upper case and Lower Case characters. I am trying to return just the Upper case values as one result and likewise return just the lower case values in another. I am not trying to convert one to the other, just return the current data as is.
I cant seem to find a statement to do this and "SUBSTRING" will only return the value I specify i.e. the first and last characters
So for example if I have AAbbCCdd and want to return the upper case values, the result I need is AACC.
Upvotes: 3
Views: 1667
Reputation: 176016
With a function:
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'
Or
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'
(Cannot use [a-z])
Upvotes: 2
Reputation: 17171
As has been mentioned in the comments - this should really be done in the presentation layer, not in SQL.
However, that doesn't stop it being a bit of fun!
The key is to use a case-sensitive collation. In this example I've gone for SQL_Latin1_General_CP1_CS_AS (the "CS" = CaseSensitive. "CI" = CaseInsensitive)
You're never going to get good performance with this kind of thing though as the solution involves looping (recursive CTE in this case)
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)
;
Upvotes: 0
Reputation: 6791
Here's another way using functions:
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
To use:
DECLARE @string VARCHAR(20) = 'AAbbCCdd'
SELECT dbo.returnUppers(@string)
Returns AACC. You need to write a similar function for lowers just change UPPER() to LOWER()
Upvotes: 1