Solved

Problem with title case function

Posted on 2009-07-13
3
151 Views
Last Modified: 2012-05-07
Hi i'm trying to make a function that will title case strings, and also deal with capitalising surnames that have Mc's in them properly, ie McIntosh.

I've tried altering a function i found on the internet to have a case statement within it and it errors, when I use the case outside it works fine... is there an issue with my construction?
BEGIN
 

DECLARE @Index          INT

DECLARE @Char           CHAR(1)

DECLARE @PrevChar       CHAR(1)

DECLARE @OutputString   VARCHAR(255)
 

SET @OutputString = LOWER(@InputString)

SET @Index = 1
 

WHILE @Index <= LEN(@InputString)

BEGIN

    SET @Char     = SUBSTRING(@InputString, @Index, 1)

    SET @PrevChar = CASE WHEN @Index = 1 THEN ' '

                         ELSE SUBSTRING(@InputString, @Index - 1, 1)

                    END
 

    IF @PrevChar IN (' ', ';', ':', '!', '?', ',', '.', '_', '-', '/', '&', '''', '(')

    BEGIN

        IF @PrevChar != '''' OR UPPER(@Char) != 'S'

            SET @OutputString = STUFF(@OutputString, @Index, 1, UPPER(@Char))

    END
 

    SET @Index = @Index + 1
 

END

	CASE

		WHEN @OutputString LIKE 'Mc%'

		THEN ('Mc'+ UPPER(SUBSTRING(@OutputString,3,1))+(RIGHT(@OutputString, LEN(@OutputString)-3)))

	END

	

RETURN @OutputString
 

END

GO

Open in new window

0
Comment
Question by:chtru
  • 2
3 Comments
 

Author Comment

by:chtru
ID: 24845856
This problem just got a lot more urgent!
0
 
LVL 31

Expert Comment

by:RiteshShah
ID: 24846617
here you go....


BEGIN

Declare @InputString varchar(255)

set @InputString='mccan' 

DECLARE @Index          INT

DECLARE @Char           CHAR(1)

DECLARE @PrevChar       CHAR(1)

DECLARE @OutputString   VARCHAR(255)

 

SET @OutputString = LOWER(@InputString)

SET @Index = 1

 

WHILE @Index <= LEN(@InputString)

BEGIN

    SET @Char     = SUBSTRING(@InputString, @Index, 1)

    SET @PrevChar = CASE WHEN @Index = 1 THEN ' '

                         ELSE SUBSTRING(@InputString, @Index - 1, 1)

                    END

 

    IF @PrevChar IN (' ', ';', ':', '!', '?', ',', '.', '_', '-', '/', '&', '''', '(')

    BEGIN

        IF @PrevChar != '''' OR UPPER(@Char) != 'S'

            SET @OutputString = STUFF(@OutputString, @Index, 1, UPPER(@Char))

    END

 

    SET @Index = @Index + 1

 

END
 

if @OutputString LIKE 'Mc%'

begin

	set @OutputString=('Mc'+ UPPER(SUBSTRING(@OutputString,3,1))+(RIGHT(@OutputString, LEN(@OutputString)-3)))

end
 

        

print @OutputString

 

END
 

GO

Open in new window

0
 
LVL 31

Accepted Solution

by:
RiteshShah earned 500 total points
ID: 24846630
and if you want to do it with CASE only, no IF than have a look:





BEGIN

Declare @InputString varchar(255)

set @InputString='RITESH' 

DECLARE @Index          INT

DECLARE @Char           CHAR(1)

DECLARE @PrevChar       CHAR(1)

DECLARE @OutputString   VARCHAR(255)

 

SET @OutputString = LOWER(@InputString)

SET @Index = 1

 

WHILE @Index <= LEN(@InputString)

BEGIN

    SET @Char     = SUBSTRING(@InputString, @Index, 1)

    SET @PrevChar = CASE WHEN @Index = 1 THEN ' '

                         ELSE SUBSTRING(@InputString, @Index - 1, 1)

                    END

 

    IF @PrevChar IN (' ', ';', ':', '!', '?', ',', '.', '_', '-', '/', '&', '''', '(')

    BEGIN

        IF @PrevChar != '''' OR UPPER(@Char) != 'S'

            SET @OutputString = STUFF(@OutputString, @Index, 1, UPPER(@Char))

    END

 

    SET @Index = @Index + 1

 

END
 
 

   SELECT @OutputString = CASE

                WHEN @OutputString LIKE 'Mc%'

					THEN ('Mc'+ UPPER(SUBSTRING(@OutputString,3,1))+(RIGHT(@OutputString, LEN(@OutputString)-3)))

                ELSE

					@OutputString

        END
 

        

print @OutputString

 

END
 

GO

Open in new window

0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

I am showing a way to read/import the excel data in table using SQL server 2005... Suppose there is an Excel file "Book1" at location "C:\temp" with column "First Name" and "Last Name". Now to import this Excel data into the table, we will use…
When writing XML code a very difficult part is when we like to remove all the elements or attributes from the XML that have no data. I would like to share a set of recursive MSSQL stored procedures that I have made to remove those elements from …
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now