Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
gj0519
Starting Member
13 Posts |
Posted - 2008-07-08 : 12:27:49
|
I am working on creating a function that will take a name that has a comma for the delimiter and propercase the last and first name.I can get the last name to work but I am having problems getting the first name set to propercase.Original name: SHORT, GALEAfter I run my function: Short, galeCreate Function dbo.fn_ProperCaseName(@FullName VarChar(30))Returns VarChar(30)AsBegin Declare @CommaIndex TinyInt, @ReturnName VarChar(30)Set @CommaIndex = CharIndex(',',@FullName)If @CommaIndex > 0 Set @ReturnName = Upper(Left(@FullName,1)) + Lower(Substring(@FullName, 2, @CommaIndex -1)) + Upper(Substring(@FullName, @CommaIndex + 1, 1)) + Lower(Substring(@FullName, @CommaIndex +2, Len(@FullName)))Else Set @ReturnName = Upper(Left(@FullName,1)) + Lower(Substring(@FullName, 2, Len(@FullName)))Return @ReturnNameEndThanks,GJ |
|
|
jimf
Master Smack Fu Yak Hacker
2875 Posts |
Posted - 2008-07-08 : 12:52:35
|
| Here's a quick fix, but it won't handle any name that is notformatted as LastName(one word),(space) FirstName. You need to take the space between the names into account.Declare @CommaIndex TinyInt, @ReturnName VarChar(30)DECLARE @fullname varchar(50)SET @fullname = 'smith, john'Set @CommaIndex = CharIndex(',',@FullName)If @CommaIndex > 0 Set @ReturnName = Upper(Left(@FullName,1)) + Lower(Substring(@FullName, 2, @CommaIndex)) + Upper(Substring(@FullName, @CommaIndex + 2, 1)) + Lower(Substring(@FullName, @CommaIndex +3, Len(@FullName)))Else Set @ReturnName = Upper(Left(@FullName,1)) + Lower(Substring(@FullName, 2, Len(@FullName)))select @ReturnNameJim |
 |
|
|
gj0519
Starting Member
13 Posts |
Posted - 2008-07-08 : 13:06:28
|
| Thanks for the reply Jim,I just figured out where I needed the change. I am still working on the handling of different formatted names, currently the databases I work with are all formatted the same, but the case can be anyway.GJ |
 |
|
|
jimf
Master Smack Fu Yak Hacker
2875 Posts |
Posted - 2008-07-08 : 13:09:07
|
It's been my experience that when names are formatted like this there is always a 'Thornton, billy bob' or 'de la hoya,oscar' just to throw a wrench in things! Jim |
 |
|
|
gj0519
Starting Member
13 Posts |
Posted - 2008-07-08 : 13:39:14
|
| I was incorrect on my last post about the name formatting, sometimes there will be a middle initial. So I am going to have to figure out how to handle those names.Ex. FISHER, HANNA M.My function currently returns: Fisher,Hanna m.GJ |
 |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
|
|
|
|
|
|
|