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!

Error while parsing name field

Status
Not open for further replies.

gertNo1

Technical User
Sep 30, 2007
42
Hey Guys, I have a large field (named Details) that contains names and other information. I am trying to parse the first and last names.

Code:
select
LEFT(SUBSTRING(Details, 1, 26), 
CHARINDEX('', SUBSTRING(Details, 1, 26) - 1)) AS LastName
LEN(LEFT(SUBSTRING(Details, 1, 26), (CHARINDEX('F', 
SUBSTRING(Details, 1, 26))) + 1) AS FirstName
FROM dbo.tblImportAppliedBankDaily
WHERE TransactionID = 'A' and RecordID = 'C1'

The data is:
THOMAS MICHAEL TF

The “F” in the last byte is a new field

Errors:
Conversion failed when converting the varchar value 'THOMAS MICHAEL T' to data type int.
Incorrect syntax near the keyword 'FROM'.
 
LEN(LEFT(SUBSTRING(Details, 1, 26), (CHARINDEX('F',
SUBSTRING(Details, 1, 26))) + 1)

you have the len as the first_name

Hopefully you are just trying to parse this nonsense to get it out of this format. You aren't really storing your infomation like this are you? If you are you need to revisit your database design.

"NOTHING is more important in a database than integrity." ESquared
 
This part prodoces the error:
SUBSTRING(Details, 1, 26) - 1

-------------------------------------------------------------------------------------------------------------------------
"Now I can look at you in peace; I don't eat you any more." Franz Kafka, while admiring fish in an aquarium
 
It looks to me like this may be fixed width data, so, perhaps this....

Code:
select SUBSTRING(Details, 1, 16) As LastName,
       SubString(Details, 17, 9) As FirstName,
       SubString(Details, 26, 1) As MiddleInitial
FROM   dbo.tblImportAppliedBankDaily
WHERE  TransactionID = 'A' and RecordID = 'C1'

-George

"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom
 
Try this query:

SELECT
LEFT(Details, CHARINDEX(' ', Details)) AS FirstName,
LTRIM(SUBSTRING(Details, CHARINDEX(' ', Details),
CHARINDEX('F', Details) -
CHARINDEX(' ', Details))) AS LastName
FROM tblImportAppliedBankDaily
WHERE TransactionID = 'A' and RecordID = 'C1'

It will return:
First Name: "THOMAS"
Last Name: "MICHAEL T"

-------------------------------------------------------------------------------------------------------------------------
"Now I can look at you in peace; I don't eat you any more." Franz Kafka, while admiring fish in an aquarium
 
And this query:

SELECT
LEFT(Details, CHARINDEX(' ', Details)) AS FirstName,

LEFT(LTRIM(SUBSTRING(Details, CHARINDEX(' ', Details),
CHARINDEX('F', Details) -
CHARINDEX(' ', Details))),
CHARINDEX(' ', LTRIM(SUBSTRING(Details, CHARINDEX(' ', Details),
CHARINDEX('F', Details) -
CHARINDEX(' ', Details)))))
FROM tblImportAppliedBankDaily
WHERE TransactionID = 'A' and RecordID = 'C1'

will return:
First Name: "THOMAS"
Last Name: "MICHAEL"

-------------------------------------------------------------------------------------------------------------------------
"Now I can look at you in peace; I don't eat you any more." Franz Kafka, while admiring fish in an aquarium
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top