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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Select only 1st 15 characters of name field 1

Status
Not open for further replies.

Mbroad

Programmer
Feb 26, 2004
49
US
Help!! I am using the following code to divide a NAME field (containing both first and last name) into separate first and last name fields. However, I need to also only select the leftmost 15 positions of the first name and last name before writing them.

How do I do this?

Thanks--Mark

Government Coder Mark
 
LEFT(fieldname,15)
Google SQL string functions and you will find lots of other useful built in functions.
 
You might want to install SQL Server Books On Line (BOL) from Microsoft. It's a great reference to have on hand.
 
Thanks for the posts...however it is more complex than this:

I have a 36 character NAME field on TABLE-1 (containing first and last name)

I need to separate the first-name & last-name from NAME writing them to TABLE-2 FNAME and LNAME fields

the "sticking "point is that I can only use the first 15 characters of each first or last name

I am not sure how or IF this can be done.
Please advise,
thanks,
Mark

Government Coder Mark
 
will something like this work for you??
Code:
INSERT INTO [table2]
SELECT RTRIM(LEFT(SUBSTRING(name, 1, CHARINDEX(' ', name) - 1), 15)) FirstName,
       LTRIM(LEFT(SUBSTRING(name, CHARINDEX(' ', name) + 1, LEN(name)), 15)) LastName
  FROM [table1]
 WHERE [table1].[name] = 'Yakoff Smirnoff'
this code breaks the name field apart where it finds the first space. it then takes the 15 left most characters from this and trims off any trailing spaces and leaves you with the first name. the last name is basically from where the first space is found for 15 more characters or until the last character whichever is less. obviously substitute your field/table names with what is listed here. does this explanation make sense?? i'm sure that there are better ways to do it but this works for me
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top