Skip to content
Loading
  • Dr.Ajay Kashyap
    0
    Ananth sir not working
    0
  • Ananth G
    0

    1. -- Drop the function if it already exists  
    2.   IF OBJECT_ID('dbo.InitCap') IS NOT NULL  
    3.     DROP FUNCTION dbo.InitCap;  
    4.   GO  
    5.    
    6.  -- Implementing Oracle INITCAP function  
    7.  CREATE FUNCTION dbo.InitCap (@inStr VARCHAR(8000))  
    8.   RETURNS VARCHAR(8000)  
    9.   AS  
    10.   BEGIN  
    11.     DECLARE @outStr VARCHAR(8000) = LOWER(@inStr),  
    12.          @char CHAR(1),   
    13.          @alphanum BIT = 0,  
    14.          @len INT = LEN(@inStr),  
    15.                  @pos INT = 1;          
    16.    
    17.     -- Iterate through all characters in the input string  
    18.     WHILE @pos <= @len BEGIN  
    19.    
    20.       -- Get the next character  
    21.       SET @char = SUBSTRING(@inStr, @pos, 1);  
    22.    
    23.       -- If the position is first, or the previous characater is not alphanumeric  
    24.       -- convert the current character to upper case  
    25.       IF @pos = 1 OR @alphanum = 0  
    26.         SET @outStr = STUFF(@outStr, @pos, 1, UPPER(@char));  
    27.    
    28.       SET @pos = @pos + 1;  
    29.    
    30.       -- Define if the current character is non-alphanumeric  
    31.       IF ASCII(@char) <= 47 OR (ASCII(@char) BETWEEN 58 AND 64) OR  
    32.       (ASCII(@char) BETWEEN 91 AND 96) OR (ASCII(@char) BETWEEN 123 AND 126)  
    33.       SET @alphanum = 0;  
    34.       ELSE  
    35.       SET @alphanum = 1;  
    36.    
    37.     END  
    38.    
    39.    RETURN @outStr;           
    40.   END  
    41.   GO  

     Create above function then run below query 
    1. select InitCap(s.Category) from  MasterCategory s  
     Sample Output:
     
     If answer is correct accept it
    0
  • Dr.Ajay Kashyap
    0
    Thks Ananth G sir  and Midhun T P sir
    0