[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Proper/Title Case Correction on Name Fields

Posted on 2006-06-02
10
Medium Priority
?
1,450 Views
Last Modified: 2008-03-10
Hi,

Below is a function I found on the web for converting a given field to Proper/Title case. As you'd expect it converts the first letter of each word to uppercase. The problem I have is that we have a number of customers with names like AA Legal Practise and Some Law Firm LLP and rather stupidly I forgot to test the script before running it on the live database.

It has also been run on some address fields and has converted values like 1st and 4th into 1St and 4Th.

Can anyone help me amend the function so I can maybe add a list of exceptions/rules that should rename lowecase or uppercase?

This is quite important!

Many thanks,
Danny


CREATE  Function ProperCase(@Text as varchar(8000))
returns varchar(8000)
as
begin
   declare @Reset bit;
   declare @Ret varchar(8000);
   declare @i int;
   declare @c char(1);

   select @Reset = 1, @i=1, @Ret = '';
   
   while (@i <= len(@Text))
         select @c= substring(@Text,@i,1),
               @Ret = @Ret + case when @Reset=1 then UPPER(@c) else LOWER(@c) end,
               @Reset = case when @c like '[a-zA-Z]' then 0 else 1 end,
               @i = @i +1
   return @Ret
end



0
Comment
Question by:LFMSupport
  • 5
  • 5
10 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 2000 total points
ID: 16815000
your function is helpful for a SuperProperCase function, which could be like this:

CREATE Function SuperProperCase(@Text as varchar(8000))
returns varchar(8000)
as
begin
  declare @r VARCHAR(8000)
  declare @p INT
  declare @l varchar(8000)

  while @text <> ''
  begin
     set @p = instr(@text, ' ')
     if coalesce(@p,0) = 0
     begin
        set @l = @text
        set @text = ''
     end
     else
     begin
        set @l = left(@text, @p)
        set @text = substring(@text, @p+1, len(@p))
     end
   
     --handle the exceptions
     if (@l = 'AA') or (@l = 'LLP' )
       set @l = UPPER(@l)
     else
     begin
       if (@l like '[0-9]%')
         set @l = LOWER(@l)
       else
       begin
          set @l = dbo.ProperCase(@l)
       end
     end
 
     set @r = @r + @l
  end
 
  return (@r)
end

0
 
LVL 1

Author Comment

by:LFMSupport
ID: 16815020
Thanks for the prompt response!

Am I right in thinking I just list the uppercase exceptions here:

--handle the exceptions
if (@l = 'AA') or (@l = 'LLP' )
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 16815039
yes
0
 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

 
LVL 1

Author Comment

by:LFMSupport
ID: 16815167
I've just attempted to run the script you posted but am being told that INSTR isn't a valid SQL function. Any ideas what the SQL equivilent is?

Danny
0
 
LVL 1

Author Comment

by:LFMSupport
ID: 16815195
Also,  should the line:

if (@l = 'AA') or (@l = 'LLP' )

Not be:

if (UPPER(@l) = 'AA') or (UPPER(@l) = 'LLP' )

Danny
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 16815206
sorry, should be CHARINDEX instead of INSTR, messed up sql server and oracle
0
 
LVL 1

Author Comment

by:LFMSupport
ID: 16815258
Cheers
0
 
LVL 1

Author Comment

by:LFMSupport
ID: 16815467
Sorry to rehash this after accepting the answer...

This is only returning NULL values? Any ideas why?
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 16815966
working on it, please standby ...
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 16815985
here we go:

alter Function SuperProperCase(@Text as varchar(8000))
returns varchar(8000)
as
begin
  declare @r VARCHAR(8000)
  declare @p INT
  declare @l varchar(8000)

  set @r = ''
  while @text <> ''
  begin
     set @p = charindex(' ',@text)
     if coalesce(@p,0) = 0
     begin
        set @l = @text
        set @text = ''
     end
     else
     begin
        set @l = left(@text, @p)
        set @text = substring(@text, @p+1, len(@text)-@p)
     end
   
     -- set @r = @r + '<' + @l + '>'
     --handle the exceptions
     if (@l = 'AA') or (@l = 'LLP' )
       set @l = rtrim(UPPER(@l))
     else
     begin
       if (@l like '[0-9]%')
         set @l = LOWER(@l)
       else
         set @l = dbo.ProperCase(@l)
     end
 
     set @r = ltrim(@r + ' ') + @l
  end
 
  return (@r)
end


go
select dbo.ProperCase('abc and l LLP sq%'), dbo.SuperProperCase('abc and l LLP sq%'), charindex(' ','abc%')
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
One of the most important things in an application is the query performance. This article intends to give you good tips to improve the performance of your queries.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
Suggested Courses

834 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