473,395 Members | 1,526 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

how to parse a name

ja
Hello,

I have a table that has a name field with the following data

john doe
john doe smith
john d smith

I need to separate the first, middle and last names into separate
fields. ex. first varchar (20), middle varchar (20) and last varchar
(20).

I am testing the process that I have to follow by doing the following

create table names (
name varchar (40),
first varchar (20),
middle varchar (20),
last varchar (20)
)

insert into names (name)
values (john doe smith)

I get an error message when I run the following query:

update b
set first = substring(name, 1, (charindex('', name)-1))

The error message is:
Server: Msg 536, Level 16, State 3, Line 1
Invalid length parameter passed to the substring function.
The statement has been terminated.

Does anybody have any suggestions?

TIA

ja
Jul 20 '05 #1
2 10618
ja (ja@aol.com) writes:
I get an error message when I run the following query:

update b
set first = substring(name, 1, (charindex('', name)-1))

The error message is:
Server: Msg 536, Level 16, State 3, Line 1
Invalid length parameter passed to the substring function.
The statement has been terminated.

Does anybody have any suggestions?


Change '' to ' ' to actually search for a space.

However, I would do this in a client language that is more powerful to
string manipulation than T-SQL. For instance, you may need to handle
multiple spaces.
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 20 '05 #2
Hi
It looks like there is no space in the string being tested for in the
charindex call try:

update b
set first = CASE WHEN charindex(SPACE(1), name) > 1
THEN substring(name, 1, charindex(SPACE(1), name)-1)
ELSE name
END,
last = CASE WHEN charindex(SPACE(1), REVERSE(name)) > 1
THEN RIGHT(name, charindex(SPACE(1), REVERSE(name))-1)
END as LastName,
Middle = CASE WHEN LEN(REPLACE(name, SPACE(1), SPACE(0) )) < LEN(name) -1
THEN substring(name, charindex(SPACE(1), name)+1, LEN(name) -
charindex(SPACE(1), name) - charindex(SPACE(1), REVERSE(name)))
END

John

"ja" <ja@aol.com> wrote in message
news:3h********************************@4ax.com...
Hello,

I have a table that has a name field with the following data

john doe
john doe smith
john d smith

I need to separate the first, middle and last names into separate
fields. ex. first varchar (20), middle varchar (20) and last varchar
(20).

I am testing the process that I have to follow by doing the following

create table names (
name varchar (40),
first varchar (20),
middle varchar (20),
last varchar (20)
)

insert into names (name)
values (john doe smith)

I get an error message when I run the following query:

update b
set first = substring(name, 1, (charindex('', name)-1))

The error message is:
Server: Msg 536, Level 16, State 3, Line 1
Invalid length parameter passed to the substring function.
The statement has been terminated.

Does anybody have any suggestions?

TIA

ja

Jul 20 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Marten van Urk | last post by:
I got the following error in my page Parse error: parse error, unexpected T_ELSE in line 25 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>Club</title>...
6
by: Dave Kuhlman | last post by:
Suppose that I have content that looks like what I've included at the end of this message. Is there something in the standard Python library that will help me parse it, break into the parts...
0
by: Chad Whitacre | last post by:
Hey all, I've been playing around with the parser module, and based on the documentation I would expect all symbols in a parse tree to be part of the grammar. For example, I find this line in...
1
by: H.L Bai | last post by:
hi, everybody i meet a parse error when i used the xml4c. any proposal is helpful. The error is following .../XMLRegionHandler.h:59 parse error before '*' .../XMLRegionHandler.h:60 parse...
1
by: Charlie T | last post by:
hello, I need a little guidance here... I am tring to parse out an XML file, but with some restrictions. here is my XML FILE: ---------XML----------- <XML> <Cam name="01">...
6
by: Ehartwig | last post by:
I recently created a script for user verification, solved my emailing issues, and then re-created the script in order to work well with the new PHP 5 that I installed on my server. After...
3
by: Jon Davis | last post by:
The date string: "Thu, 17 Jul 2003 12:35:18 PST" The problem: // this fails on PST DateTime myDate = DateTime.Parse("Thu, 17 Jul 2003 12:35:18 PST"); Help? Jon
14
by: Jon Davis | last post by:
I have put my users through so much crap with this bug it is an absolute shame. I have a product that reads/writes RSS 2.0 documents, among other things. The RSS 2.0 spec mandates an en-US style...
11
by: Peter Pei | last post by:
One bad design about elementtree is that it has different ways parsing a string and a file, even worse they return different objects: 1) When you parse a file, you can simply call parse, which...
5
by: goldtech | last post by:
SAX XML Parse Python error message Hi, My first attempt at SAX, but have an error message I need help with. I cite the error message, code, and xml below. Be grateful if anyone can tell me...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.