-5

How can I make the function that change all the uppercase to lowercase and lowercase to uppercase in a string e.g. "HelloW world" to "hELLOw WORLD"

FLICKER
  • 6,439
  • 4
  • 45
  • 75
xixixixi
  • 1
  • 1

3 Answers3

1

You can write a function that loops through each character in the string one at a time, and if it is in the set of lower case characters, apply the UPPER function, otherwise apply the LOWER function, and build your function's output string.

Tab Alleman
  • 31,483
  • 7
  • 36
  • 52
  • using the ascii to distinguish the upper and lower? – xixixixi Oct 06 '17 at 18:59
  • That's one way. Another is PATINDEX with a case-sensitive collation. (note that the range specification wouldn't work though. Have to type out all the letters of the alphabet in the pattern to be matched): https://stackoverflow.com/questions/4212110/range-wildcard-pattern-matching-behaviour-with-case-sensitive-collations – Tab Alleman Oct 06 '17 at 19:03
  • 1
    The range specification will work with a binary collation. – Martin Smith Oct 06 '17 at 19:07
  • OK, thank you so much – xixixixi Oct 06 '17 at 19:13
0

You can create a function where cursor will iterate and will pick/fetch the String value and if its a lower case you can use the SELECT UPPER('test string') to change the String to upper case .If the String value is coming as Upper Case you can use the function SELECT lOWER('test string') to convert to lowercase.

0

You didn't state any version. For SQL Server 2017 you can use TRANSLATE as suggested by Gordon.

WITH T(S) AS
(
SELECT 'HelloW world'
)
SELECT TRANSLATE(S COLLATE Latin1_General_CS_AS, 
                'ABCDEFGHIJLKMNOPQRSTUVWXYZabcdefghijlkmnopqrstuvwxyz', 
                'abcdefghijlkmnopqrstuvwxyzABCDEFGHIJLKMNOPQRSTUVWXYZ')
FROM T
Martin Smith
  • 438,706
  • 87
  • 741
  • 845