This article is half-done without your Comment! *** Please share your thoughts via Comment ***
Check the below sample function execution and expected output to get the given string data in InitCap format.
Sample function execution:
1 |
SELECT [dbo].[fn_GetInitCap]('HI I AM ANVESH PATEL OWNER OF dbrnd.com') AS Result |
Expected Output:
1 2 3 |
Result ------------------------------------------- Hi I Am Anvesh Patel Owner Of Dbrnd.Com |
Solution:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
CREATE FUNCTION [dbo].[fn_GetInitCap] (@InputString varchar(200)) RETURNS VARCHAR(200) AS BEGIN DECLARE @i INT DECLARE @Char CHAR(1) DECLARE @PrevChar CHAR(1) DECLARE @OutputString VARCHAR(255) SET @OutputString = LOWER(@InputString) SET @i = 1 WHILE @i <= LEN(@InputString) BEGIN SET @Char = SUBSTRING(@InputString, @i, 1) SET @PrevChar = CASE WHEN @i = 1 THEN ' ' ELSE SUBSTRING(@InputString, @i - 1, 1) END IF @PrevChar IN (' ', ';', ':', '!', '?', ',', '.', '_', '-', '/', '&', '''', '(') BEGIN IF @PrevChar != '''' OR UPPER(@Char) != 'S' SET @OutputString = STUFF(@OutputString, @i, 1, UPPER(@Char)) END SET @i = @i + 1 END RETURN @OutputString END GO |
Please try the different solution for this puzzle and share it via comment...