var sidebar_align = 'right';
var content_container_margin = parseInt('290px');
var sidebar_width = parseInt('270px');
Unanswered: Get Middle Initial from a Name field-SQL Server 2008
I need to display the middle initial from a name field that contains the last name, comma, and the middle name or initial.
Mary Jane,Dow Welsh
The result I am looking for is to capture only the first letter of the middle name. In this data example, I would need to display the following on a separate column:
Any help will be greatly appreciated.
Don't have a test environment so this code is completely untested.
Should point you in the right direction though
, CharIndex(' ', Reverse(name))
, Right(name, CharIndex(' ', Reverse(name)) - 1)
, Left(Right(name, CharIndex(' ', Reverse(name)) - 1), 1)
Thank you for your prompt response.
I ran the query and got the following error message:
Msg 536, Level 16, State 2, Line 1
Invalid length parameter passed to the RIGHT function.
, CharIndex(' ', Reverse(ALIAS))
, Right(ALIAS, CharIndex(' ', ALIAS)- 1)
, Left(Right(ALIAS, CharIndex(' ', Reverse(ALIAS)) - 1), 1)
Did you really need to post 3 times? Did I take too long to respond?
Why don't you comment out one of the lines at a time, starting with the last one in the SELECT statement and work backwards. Once you stop getting error messages look at the VALUES that are returned and see if you can understand why they are what they are.
Then look at what we do on the next line of the select. I wonder what the -1 could do to provide an invalid value to that function.
I apologize for the duplicate postings. I am fairly new at this and did not see my post show up the first and second time.
See this post:
Using the attached function, you can simply call:
select dbo.FormatName([Your Name String], 'm')
...to return the middle initial.