By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,483 Members | 1,052 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,483 IT Pros & Developers. It's quick & easy.

Pulling first name out of a name column in DB

P: n/a
I built a webpage using vb.net (.net 2.0) that creates a form letter. This
letter pulls data from a database. Although I populate the address with the
person's full name, which comes from the name column of a customer table, I
need to just get the first name for the Dear part of the letter (ie: Dear
John instead of Dear John Smith). It would be easy if the name was in two
parts in the table (First name and Last name column) but, that isn't the
case.

I've been using a datalist to pull data out of the database and populate it
to the webpage. How could I just get the first name instead of the whole
name?

TIA,
Jim
Jan 4 '08 #1
Share this Question
Share on Google+
5 Replies


P: n/a

"Jim in Arizona" <ti*******@hotmail.comwrote in message
news:Oc**************@TK2MSFTNGP03.phx.gbl...
>I built a webpage using vb.net (.net 2.0) that creates a form letter. This
letter pulls data from a database. Although I populate the address with the
person's full name, which comes from the name column of a customer table, I
need to just get the first name for the Dear part of the letter (ie: Dear
John instead of Dear John Smith). It would be easy if the name was in two
parts in the table (First name and Last name column) but, that isn't the
case.

I've been using a datalist to pull data out of the database and populate
it to the webpage. How could I just get the first name instead of the
whole name?

TIA,
Jim
Jim,
Your first decision is where to do the extraction of the name. It will
be done either on the database server or by the application. If you are
using SQL Server you can create a computed colum which can be used to return
the name. If you are going to do it in the application then you will
extract the name from the returned database row.

Big problem is this: From this list of names what is the first name:

Jean Francois De Poulet
John Smith
Jean De Poulet

As you can see there is an exception for each rule. In over 30 years of
working databases I see this problem all the time. The simplest although
somewhat error prone is to split the name using space as the delimiter.
Then use the first member of the split. At least in the above list 2 of the
3 would be correct.

Living in Canada as you see makes this almost impossible.

You would do this either as a function if you are using the <%#
Eval("Name") %type binding you can create a function to return the value
and using this in the Eval expression or you can do the processing in the
ItemDataBound event of the datalist.

Hope this helps
Lloyd Sheen

Jan 4 '08 #2

P: n/a
You would do this either as a function if you are using the <%#
Eval("Name") %type binding you can create a function to return the value
and using this in the Eval expression or you can do the processing in the
ItemDataBound event of the datalist.

Hope this helps
Lloyd Sheen
Hi Lloyd.

I am using an SQL2K server. I am using Eval("Name") within the aspx page for
other data (address and amounts due). I also figured that I would somehow
have to use the space as the seperator between first and last name. I'm in
Arizona and so far I haven't seen any case in the database where there would
be a French style name (luckily) so I'm not too concerned about it.

I just don't know how I would make the function and incorporate that into
the Eval method. What about this other option you mentioned by using the SQL
instead ("using SQL Server you can create a computed colum ") Can you give
me an example of that? Perhaps that would be the easier way to go?

Thanks!!
Jan 4 '08 #3

P: n/a
On Jan 4, 1:41*pm, "Jim in Arizona" <tiltow...@hotmail.comwrote:
* *You would do this either as a function if you are using the <%#
Eval("Name") %type binding you can create a function to return the value
and using this in the Eval expression or you can do the processing in the
ItemDataBound event of the datalist.
Hope this helps
Lloyd Sheen

Hi Lloyd.

I am using an SQL2K server. I am using Eval("Name") within the aspx page for
other data (address and amounts due). I also figured that I would somehow
have to use the space as the seperator between first and last name. I'm in
Arizona and so far I haven't seen any case in the database where there would
be a French style name (luckily) so I'm not too concerned about it.

I just don't know how I would make the function and incorporate that into
the Eval method. What about this other option you mentioned by using the SQL
instead ("using SQL Server you can create a computed colum ") Can you give
me an example of that? Perhaps that would be the easier way to go?
To use User Defined Functions you must be at version SQL 2000 or
greater.

Transact-SQL to create the function would be something like:

create function [dbo].GetFirstName (@name
varchar(sizeofyourfullnamefield))
returns varchar(maxsizeforthefirstname)
as
declare @i int
set @i = charindex(' ', @name)
if (@i = 0)
return @name
else
return(left(@name, @i - 1)

Then in the EVAL (I assume the name of the name column is Name), just
use EVAL(dbo.getfirstname(Name)).

Note: This code is untested.
Jan 4 '08 #4

P: n/a
Jim,

For this problem I would not think one minute and just use the Split in VB
for Net. It is then the first row in the resulting table.

However about the problem Loyd is showing us. What do you think of our Dutch
names, which are used in Canada and the USA as well. Active in the dotNet
general newsgroup is Peter van der Goes an american MVP, who uses his
correct in Dutch written original name. (Some years ago somebody was faking
him. And used "Peter Van Der Goes", it was for all Dutch after a short
moment clear that it was a fake. We will never write original Dutch names
like that).

However, we have some use where we will write this as "Goes, Peter van der".
Because "van der" is wide common here. What is then the first name (the
comma can be as well a dot or whatever or just nothing) ?

Cor
"Jim in Arizona" <ti*******@hotmail.comschreef in bericht
news:Oc**************@TK2MSFTNGP03.phx.gbl...
>I built a webpage using vb.net (.net 2.0) that creates a form letter. This
letter pulls data from a database. Although I populate the address with the
person's full name, which comes from the name column of a customer table, I
need to just get the first name for the Dear part of the letter (ie: Dear
John instead of Dear John Smith). It would be easy if the name was in two
parts in the table (First name and Last name column) but, that isn't the
case.

I've been using a datalist to pull data out of the database and populate
it to the webpage. How could I just get the first name instead of the
whole name?

TIA,
Jim
Jan 5 '08 #5

P: n/a

"Jim in Arizona" <ti*******@hotmail.comwrote in message
news:Oc**************@TK2MSFTNGP03.phx.gbl...
>I built a webpage using vb.net (.net 2.0) that creates a form letter. This
letter pulls data from a database. Although I populate the address with the
person's full name, which comes from the name column of a customer table, I
need to just get the first name for the Dear part of the letter (ie: Dear
John instead of Dear John Smith). It would be easy if the name was in two
parts in the table (First name and Last name column) but, that isn't the
case.

I've been using a datalist to pull data out of the database and populate
it to the webpage. How could I just get the first name instead of the
whole name?

What's wrong with the Split statement in VB?

Jan 5 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.