Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using PatIndex 1

Status
Not open for further replies.

ElJayWilson

Programmer
Oct 31, 2008
19
US
I have tried using PatIndex in a query, but can't seem to get a grasp on it. What I am trying to do is generate a string that contains the first initial and then the last name from a field that contains the full name formatted like this:

WILSON,LARRY JAY

What I want to wind up with is L. WILSON

The comma will be consistently the next character after the last name and the first name will be consistently right after that.

Thanks,

LJ
 
Here's an example:
Code:
DECLARE @Val VARCHAR(100)
SELECT @Val = 'WILSON,LARRY JAY'
SELECT RIGHT(LEFT(@Val, CHARINDEX(',', @Val, 1) + 1), 1) + '. ' + LEFT(@Val, CHARINDEX(',', @Val, 1) -1)
 
Works like a charm!!! Plus I can see what I need to be doing now :).

Thanks RiverGuy!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top