Database Research & Development

  • Home
  • NoSQL
    • NoSQL
    • Cassandra
  • Databases
    • Database Theory
    • Database Designing
    • SQL Server Coding Standards
    • SQL Server
    • PostgreSQL
    • MySQL
    • Greenplum
    • Linux
  • Interviews
    • SQL Server Interviews
    • MySQL Interviews
    • SQL Puzzles
  • DBA Scripts
    • SQL Server DBA Scripts
    • PostgreSQL DBA Scripts
    • MySQL DBA Scripts
    • Greenplum DBA Scripts
  • Home
  • Blog Archives !
  • (: Laugh@dbrnd :)
  • Contact Me !
sqlserverinterviews
Home 2017 November SQL Puzzle: SQL Advance Query – Create a function to get the string in InitCap Format

SQL Puzzle: SQL Advance Query – Create a function to get the string in InitCap Format

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...

Nov 27, 2017Anvesh Patel
Greenplum: Script to find information for Long running Queries with occupied resourceGreenplum: How to Start and Stop Database?
Comments: 3
  1. Ashutosh Srivastava
    January 5, 2018 at 6:08 am

    HI Avnesh, Thanks for your explanation. I have one situation i.e. There is one column where 100 records are available. Value is like accountnumber, subaccountnumber etc. I need an output like: Account_Number, Sub_Account_Number.

    Could you please help me regarding this.

    Regards,
    Ashutosh Srivastava

    ReplyCancel
  2. sqlguru
    July 14, 2019 at 6:43 am

    create function dbo.fn_GetInitCap(@inputStr varchar(255)) returns varchar(255) as
    begin
    declare @newstr varchar(255);

    set @inputStr = replace(@inputStr, ‘ ‘,’~’);
    with cte as (
    select @inputStr inpt, cast(” as varchar(255)) items
    union all
    select cast(stuff(inpt,1,iif(PATINDEX(‘%[^a-zA-Z]%’,inpt) > 0, PATINDEX(‘%[^a-zA-Z]%’,inpt),len(inpt) ),”) as varchar(255)), substring(inpt, 1, iif(PATINDEX(‘%[^a-zA-Z]%’,inpt) > 0, PATINDEX(‘%[^a-zA-Z]%’,inpt),len(inpt) )) from cte where inpt ”
    ) –select * from cte;
    , cte2 as(
    select items, row_number() over (order by (select null)) rn
    from cte
    where items ”
    ) –select * from cte2;
    , cte3 as(
    select items, rn, cast(” as varchar(1)) ltr, 0 idx from cte2
    union all
    select stuff(items,1,1,”), rn, substring(items,1,1), idx + 1 from cte3 where items ”
    )
    select @newstr = replace(
    (select ltr2+” from (
    select cte3.*, iif(idx=1, upper(ltr),lower(ltr)) ltr2 from cte3
    where ltr ”
    –order by rn, idx
    ) t order by rn, idx for xml path(”)),’~’,’ ‘);

    return @newstr;

    end
    go

    select dbo.fn_GetInitCap(‘Hi I Am Anvesh Patel Owner Of Dbrnd.Com’);

    ReplyCancel
  3. Samrat
    October 24, 2019 at 12:37 pm

    CREATE FUNCTION InitCap(@Value VARCHAR(2000))
    RETURNS VARCHAR(2000)
    AS
    BEGIN
    DECLARE @R_VALUE VARCHAR(2000)=”

    SELECT @R_VALUE+=STRING_AGG(UPPER(SUBSTRING(VALUE,1,1))+LOWER(SUBSTRING(VALUE,2,LEN(VALUE))),’ ‘)
    FROM STRING_SPLIT(@Value,’ ‘)

    RETURN @R_VALUE
    END

    ReplyCancel

Leave a Reply Cancel reply

CAPTCHA
Refresh

*

Anvesh Patel
Anvesh Patel

Database Engineer

November 27, 2017 3 Comments SQL PuzzleAnvesh Patel, database, database research and development, dbrnd, Initcap, SQL Advance Query, SQL Interview, SQL Problem, SQL Programming, SQL Puzzle, SQL Query, SQL Tips and Tricks
About Me!

I'm Anvesh Patel, a Database Engineer certified by Oracle and IBM. I'm working as a Database Architect, Database Optimizer, Database Administrator, Database Developer. Providing the best articles and solutions for different problems in the best manner through my blogs is my passion. I have more than six years of experience with various RDBMS products like MSSQL Server, PostgreSQL, MySQL, Greenplum and currently learning and doing research on BIGData and NoSQL technology. -- Hyderabad, India.

About DBRND !

dbrnd

This is a personal blog (www.dbrnd.com).

Any views or opinions represented in this blog are personal and belong solely to the blog owner and do not represent those of people, institutions or organizations that the owner may or may not be associated with in professional or personal capacity, unless explicitly stated.

Feel free to challenge me, disagree with me, or tell me I’m completely nuts in the comments section of each blog entry, but I reserve the right to delete any comment for any reason whatsoever (abusive, profane, rude, or anonymous comments) - so keep it polite.

The content of this website is protected by copyright. No portion of this website may be copied or replicated in any form without the written consent of the website owner.

Recent Comments !
  • Anvesh Patel { Sure will do... } – May 27, 12:43 PM
  • Anvesh Patel { Great... } – May 27, 12:41 PM
  • Anvesh Patel { Great... } – May 27, 12:39 PM
  • Anvesh Patel { Great... } – May 27, 12:36 PM
  • Anvesh Patel { Great... } – May 27, 12:28 PM
  • Anvesh Patel { Great... } – May 27, 12:27 PM
  • Anvesh Patel { Great... } – May 27, 12:16 PM
  • Older »
Follow Me !
  • facebook
  • linkedin
  • twitter
  • youtube
  • google
  • flickr
© 2015 – 2019 All rights reserved. Database Research & Development (dbrnd.com)
Posting....