midrange.com code scratchpad
Name:
Camel Case in SQL
Scriptlanguage:
Plain Text
Tabwidth:
4
Date:
03/13/2025 08:23:13 pm
IP:
Logged
Description:
originally: https://www.itjungle.com/fhg/fhg020707-story02.zip

http://www.itjungle.com/fhg/fhg020707-story02.html
Code:
  1. CREATE FUNCTION Proper 
  2. (@tcString VARCHAR(100)) 
  3. RETURNS VARCHAR(100) 
  4. Language SQL
  5. Deterministic 
  6. Returns Null On Null Input 
  7. Set Option Commit=*None 
  8. Begin 
  9.  
  10. /* Scratch variables used for processing */ 
  11. DECLARE @outputString VARCHAR(100) Not Null Default ''; 
  12. DECLARE @stringLength INT Not Null Default 0; 
  13. DECLARE @loopCounter  INT Not Null Default 1; 
  14. DECLARE @charAtPos    CHAR(1) Not Null Default ''; 
  15. DECLARE @wordStart    INT Not Null Default 1; 
  16.  
  17. -- If the incoming string is NULL, return an error 
  18. --IF (@tcString IS NULL) Then 
  19. --    RETURN (null) 
  20.  
  21. -- Initialize the scratch variables 
  22. SET @stringLength = LENGTH(@tcString); 
  23. --SET @loopCounter = 1; 
  24. --SET @wordStart = 1; 
  25.  
  26. -- Loop over the string 
  27. WHILE (@loopCounter <= @stringLength) DO 
  28.     -- Get the single character off the string 
  29.     SET @charAtPos = LOWER(SUBSTR(@tcString, @loopCounter, 1)); 
  30.  
  31.     -- If we are the start of a word, uppercase the character 
  32.     -- and reset the word indicator 
  33.     If (@wordStart = 1) Then     
  34.         SET @charAtPos = UPPER(@charAtPos); 
  35.         SET @wordStart = 0; 
  36.     End If; 
  37.  
  38.     -- If we encounter a white space, indicate that we 
  39.     -- are about to start a word 
  40.     IF (@charAtPos = ' ') Then 
  41.         SET @wordStart = 1; 
  42.         End If; 
  43.  
  44.     -- Form the output string 
  45.     SET @outputString = @outputString || @charAtPos; 
  46.  
  47.     SET @loopCounter = @loopCounter + 1; 
  48. END WHILE; 
  49.  
  50. -- Return the final output 
  51. RETURN @outputString; 
  52.  
  53. END 
  54.  
© 2004-2019 by midrange.com generated in 0.006s valid xhtml & css