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:
- CREATE FUNCTION Proper
- (@tcString VARCHAR(100))
- RETURNS VARCHAR(100)
- Language SQL
- Deterministic
- Returns Null On Null Input
- Set Option Commit=*None
- Begin
-
- /* Scratch variables used for processing */
- DECLARE @outputString VARCHAR(100) Not Null Default '';
- DECLARE @stringLength INT Not Null Default 0;
- DECLARE @loopCounter INT Not Null Default 1;
- DECLARE @charAtPos CHAR(1) Not Null Default '';
- DECLARE @wordStart INT Not Null Default 1;
-
- -- If the incoming string is NULL, return an error
- --IF (@tcString IS NULL) Then
- -- RETURN (null)
-
- -- Initialize the scratch variables
- SET @stringLength = LENGTH(@tcString);
- --SET @loopCounter = 1;
- --SET @wordStart = 1;
-
- -- Loop over the string
- WHILE (@loopCounter <= @stringLength) DO
- -- Get the single character off the string
- SET @charAtPos = LOWER(SUBSTR(@tcString, @loopCounter, 1));
-
- -- If we are the start of a word, uppercase the character
- -- and reset the word indicator
- If (@wordStart = 1) Then
- SET @charAtPos = UPPER(@charAtPos);
- SET @wordStart = 0;
- End If;
-
- -- If we encounter a white space, indicate that we
- -- are about to start a word
- IF (@charAtPos = ' ') Then
- SET @wordStart = 1;
- End If;
-
- -- Form the output string
- SET @outputString = @outputString || @charAtPos;
-
- SET @loopCounter = @loopCounter + 1;
- END WHILE;
-
- -- Return the final output
- RETURN @outputString;
-
- END
-
|
|
|