Sql query in vb.net project.

I need to get the fristname and lastname  from the ownername1 field.  I know it is something like this but I'm not sure how to incorporate this in the query.  I tried but I keep getting sql errors:

(SELECT     LEFT([ownername1], CHARINDEX(',', [ownername1]) - 1) LastName,
     SUBSTRING([ownername1], CHARINDEX(',', [ownername1]) + 1, LEN([ownername1])) FirstName

 If source = "PROPERTYTAX" Then
                'Need to get lastname and frist and last name from ownername field
                MySQL = "Select ownerename1 as firstname, ownername1 as lastname, locationaddress as address, locationcity as city, locationzip as zip from " & source & " where ownername1 =  " & firstname
            End If

Thank you for your help.
running32Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Brian CroweDatabase AdministratorCommented:
You might be better off parsing the name field on the client side instead of in the query.  You might be running into errors in the case that there is no comma in the field which would give you a zero length for the LEFT function.
0
PierreBeukesCommented:
Hi, I ran this query against my db... and it works - gives me the LastName and Firstname in 2 fields.


SELECT LEFT([ownername1], CHARINDEX(',', [ownername1]) - 1) LastName,
SUBSTRING([ownername1], CHARINDEX(',', [ownername1]) + 1, LEN([ownername1])) FirstName From TestTable


What are you trying here?

If source = "PROPERTYTAX" Then
'Need to get lastname and frist and last name from ownername field

MySQL = "Select ownerename1 as firstname, ownername1 as lastname, locationaddress as address, locationcity as city, locationzip as zip from " & source & " where ownername1 = " & firstname

You can go

MySQL = "Select SUBSTRING([ownername1], CHARINDEX(',', [ownername1]) + 1, LEN([ownername1])) as firstname, LEFT([ownername1], CHARINDEX(',', [ownername1]) - 1) as lastname, locationaddress as address, locationcity as city, locationzip as zip from " & source & " where SUBSTRING([ownername1], CHARINDEX(',', [ownername1]) + 1, LEN([ownername1])) = " & firstname From TestTable


End If

Hope that helps.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
running32Author Commented:
I get the error IErrorInfo.GetDescription failed on the above query  
0
PierreBeukesCommented:
Im still not exactly sure what you are trying to do?

Can you give more detail?
0
running32Author Commented:
worked it out.  Thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.