Dec 17, 2007
I am experiencing some frustration in regards to parsing an email that I need to work with.
(I don't have control over the format of the email I am receiving)
Any help would be appreciated.
Thank you
Jake
Here is the error I get, when I use the SQL Query Analyzer to process my Stored Procedure. (Not every email gets this error, just some, others process just fine.)
String or binary data would be truncated.
The statement has been terminated.
Stored Procedure: eResponse.dbo.spParser
Return Code = -6
My Code is as follows:
CREATE PROCEDURE spParser
@_id nvarchar(50)
AS
--assumes the email is no longer than 4000 characters. if it might be longer, bump this number up.
-- Common Varriables
DECLARE @body nvarchar(4000)
DECLARE @i int
DECLARE @phone nvarchar(50)
DECLARE @phonestart int
DECLARE @phoneend int
DECLARE @email nvarchar(50)
DECLARE @emailstart int
DECLARE @emailend int
DECLARE @comments nvarchar(3000)
DECLARE @commentsstart int
DECLARE @commentsend int
DECLARE @OrigID varchar(50)
-- Jose Ole Variables
DECLARE @firstname nvarchar(50)
DECLARE @firstnamestart int
DECLARE @firstnameend int
DECLARE @lastname nvarchar(50)
DECLARE @lastnamestart int
DECLARE @lastnameend int
DECLARE @address1 nvarchar(50)
DECLARE @address1start int
DECLARE @address1end int
DECLARE @address2 nvarchar(50)
DECLARE @address2start int
DECLARE @address2end int
DECLARE @city nvarchar(50)
DECLARE @citystart int
DECLARE @cityend int
DECLARE @state nvarchar(5)
DECLARE @statestart int
DECLARE @stateend int
DECLARE @zip nvarchar(15)
DECLARE @zipstart int
DECLARE @zipend int
DECLARE @country nvarchar(50)
DECLARE @countrystart int
DECLARE @countryend int
DECLARE @phone2 nvarchar(50)
DECLARE @phone2start int
DECLARE @phone2end int
DECLARE @productname nvarchar(50)
DECLARE @productnamestart int
DECLARE @productnameend int
DECLARE @upccode nvarchar(50)
DECLARE @upccodestart int
DECLARE @upccodeend int
DECLARE @datecode nvarchar(50)
DECLARE @datecodestart int
DECLARE @datecodeend int
DECLARE @purchaseat nvarchar(50)
DECLARE @purchaseatstart int
DECLARE @purchaseatend int
DECLARE @purchasecity nvarchar(50)
DECLARE @purchasecitystart int
DECLARE @purchasecityend int
DECLARE @datepurchased nvarchar(50)
DECLARE @datepurchasedstart int
DECLARE @datepurchasedend int
DECLARE @dateopened nvarchar(50)
DECLARE @dateopenedstart int
DECLARE @dateopenedend int
DECLARE @subject nvarchar(50)
DECLARE @subjectstart int
DECLARE @subjectend int
-- Windosr Variables
DECLARE @name nvarchar(50)
DECLARE @namestart int
DECLARE @nameend int
DECLARE @company nvarchar(50)
DECLARE @companystart int
DECLARE @companyend int
--First replace all ASCII carriage returns and line feeds and assign the whole chunk of data to a variable.
SET @body = (select replace( REPLACE(convert(varchar(4000), message),CHAR(10),' '),CHAR(13),'') from message where mailid = @_id)
--below is the sample syntax for the individual replacements of line feeds and carriage returns, but it's combined into one statement above.
--SELECT REPLACE(@jb,CHAR(10),'') as firstpass
--SELECT REPLACE(@jb,CHAR(13),'') as secondpass
IF left(@body,47) = 'Fromubject:contact info sent from samplesite.com'
--If this is true.. then it is a sample company.
BEGIN
--The 12 in the start is compensating for the 12 Characters of "First Name: "
--Below is collecting First Name
SET @firstnamestart = (CHARINDEX('First name:',@body) + 11)
SET @firstnameend = (CHARINDEX('Last name:',@body) - 1)
SET @firstname = SUBSTRING(@body,@firstnamestart,@firstnameend-@firstnamestart)
----Below is collecting Last Name
SET @lastnamestart = (CHARINDEX('Last name:',@body) + 10)
SET @lastnameend = (CHARINDEX('Address 1:',@body) - 1)
SET @lastname = SUBSTRING(@body,@lastnamestart,@lastnameend-@lastnamestart)
--Below is collecting Address1
SET @address1start = (CHARINDEX('Address 1:',@body) + 10)
SET @address1end = (CHARINDEX('Address 2:',@body) - 1)
SET @address1 = SUBSTRING(@body,@address1start,@address1end-@address1start)
--Below is collecting Address2
SET @address2start = (CHARINDEX('Address 2:',@body) + 10)
SET @address2end = (CHARINDEX('City:',@body) - 1)
SET @address2 = SUBSTRING(@body,@address2start,@address2end-@address2start)
--Below is collecting city information
SET @citystart = (CHARINDEX('City:',@body) + 5)
SET @cityend = (CHARINDEX('State/Prov',@body) -1)
SET @city = SUBSTRING(@body,@citystart,@cityend-@citystart)
--Below is collecting state information
SET @statestart = (CHARINDEX('State/Province:',@body) + 15)
SET @stateend = (CHARINDEX('Zip/Postal',@body) -1)
SET @state = SUBSTRING(@body,@statestart,@stateend-@statestart)
--Below is collecting zip information
SET @zipstart = (CHARINDEX('Zip/Postal Code:',@body) + 16)
SET @zipend = (CHARINDEX('Country',@body) -1)
SET @zip = SUBSTRING(@body,@zipstart,@zipend-@zipstart)
--Below is collecting country information
SET @countrystart = (CHARINDEX('Country:',@body) + 8)
SET @countryend = (CHARINDEX('E-mail address',@body) -1)
SET @country = SUBSTRING(@body,@countrystart,@countryend-@countrystart)
--Below is collecting email information
SET @emailstart = (CHARINDEX('E-mail address:',@body) + 15)
SET @emailend = (CHARINDEX('Daytime',@body) -1)
SET @email = SUBSTRING(@body,@emailstart,@emailend-@emailstart)
--Below is collecting phone information
SET @phonestart = (CHARINDEX('Daytime Phone:',@body) + 14)
SET @phoneend = (CHARINDEX('Evening Phone:',@body) -1)
SET @phone = SUBSTRING(@body,@phonestart,@phoneend-@phonestart)
--Below is collecting phone2 information
SET @phone2start = (CHARINDEX('Evening Phone:',@body) + 14)
SET @phone2end = (CHARINDEX('Product Name',@body) -1)
SET @phone2 = SUBSTRING(@body,@phone2start,@phone2end-@phone2start)
--Below is collecting Product Name information
SET @productnamestart = (CHARINDEX('Product Name:',@body) + 12)
SET @productnameend = (CHARINDEX('UPC Code:',@body) -1)
SET @productname = SUBSTRING(@body,@productnamestart,@productnameend-@productnamestart)
--Below is collecting UPC Code information
SET @upccodestart = (CHARINDEX('UPC Code:',@body) + 9)
SET @upccodeend = (CHARINDEX('Date Code:',@body) -1)
SET @upccode = SUBSTRING(@body,@upccodestart,@upccodeend-@upccodestart)
--Below is collecting Date Code information
SET @datecodestart = (CHARINDEX('Date Code:',@body) + 10)
SET @datecodeend = (CHARINDEX('Purchased At',@body) -1)
SET @datecode = SUBSTRING(@body,@datecodestart,@datecodeend-@datecodestart)
--Below is collecting Purchase At information
SET @purchaseatstart = (CHARINDEX('Purchased At',@body) + 26)
SET @purchaseatend = (CHARINDEX('Purchase City:',@body) -1)
SET @purchaseat = SUBSTRING(@body,@purchaseatstart,@purchaseatend-@purchaseatstart)
--Below is collecting Purchase City information
SET @purchasecitystart = (CHARINDEX('Purchase City:',@body) + 14)
SET @purchasecityend = (CHARINDEX('Date Purchased',@body) -1)
SET @purchasecity = SUBSTRING(@body,@purchasecitystart,@purchasecityend-@purchasecitystart)
--Below is collecting Date Purchased information
SET @datepurchasedstart = (CHARINDEX('Date Purchased:',@body) + 15)
SET @datepurchasedend = (CHARINDEX('Date Opened',@body) -1)
SET @datepurchased = SUBSTRING(@body,@datepurchasedstart,@datepurchasedend-@datepurchasedstart)
--Below is collecting Date Opened information
SET @dateopenedstart = (CHARINDEX('Date Opened:',@body) + 12)
SET @dateopenedend = (CHARINDEX('E-mail Subject:',@body) -1)
SET @dateopened = SUBSTRING(@body,@dateopenedstart,@dateopenedend-@dateopenedstart)
--Below is collecting Email Subject information
SET @subjectstart = (CHARINDEX('E-mail Subject:',@body) + 15)
SET @subjectend = (CHARINDEX('Comments:',@body) -1)
SET @subject = SUBSTRING(@body,@subjectstart,@subjectend-@subjectstart)
--Below is collecting message information
SET @commentsstart = (CHARINDEX('Comments:',@body) + 10)
SET @commentsend = len(@body)
SET @comments = SUBSTRING(@body,@commentsstart,@commentsend-@commentsstart)
--below is the part that actually writes the data out to the Output table
INSERT INTO OutputEmails
(FirstName, LastName, Address1, Address2, City, State, Zip, Country, eMail, Phone, Phone2, ProductName, UPCCode, DateCode, PurchaseAt, PurchaseCity, DatePurchased, DateOpened, Subject, Comments, OrigID)
SELECT
ltrim(SUBSTRING(@body,@firstnamestart,@firstnameend-@firstnamestart)) as FirstName,
ltrim(SUBSTRING(@body,@lastnamestart,@lastnameend-@lastnamestart)) as LastName,
ltrim(SUBSTRING(@body,@address1start,@address1end-@address1start)) as Address1,
ltrim(SUBSTRING(@body,@address2start,@address2end-@address2start)) as Address2,
ltrim(SUBSTRING(@body,@citystart,@cityend-@citystart)) as City,
ltrim(SUBSTRING(@body,@statestart,@stateend-@statestart)) as State,
ltrim(SUBSTRING(@body,@zipstart,@zipend-@zipstart)) as Zip,
ltrim(SUBSTRING(@body,@countrystart,@countryend-@countrystart)) as Country,
ltrim(SUBSTRING(@body,@emailstart,@emailend-@emailstart)) as eMail,
ltrim(SUBSTRING(@body,@phonestart,@phoneend-@phonestart)) as Phone,
ltrim(SUBSTRING(@body,@phone2start,@phone2end-@phone2start)) as Phone2,
ltrim(SUBSTRING(@body,@productnamestart,@productnameend-@productnamestart)) as ProductName,
ltrim(SUBSTRING(@body,@upccodestart,@upccodeend-@upccodestart)) as UPCCode,
ltrim(SUBSTRING(@body,@datecodestart,@datecodeend-@datecodestart)) as DateCode,
ltrim(SUBSTRING(@body,@purchaseatstart,@purchaseatend-@purchaseatstart)) as PurchaseAt,
ltrim(SUBSTRING(@body,@purchasecitystart,@purchasecityend-@purchasecitystart)) as PurchaseCity,
ltrim(SUBSTRING(@body,@datepurchasedstart,@datepurchasedend-@datepurchasedstart)) as DatePurchased,
ltrim(SUBSTRING(@body,@dateopenedstart,@dateopenedend-@dateopenedstart)) as DateOpened,
ltrim(SUBSTRING(@body,@subjectstart,@subjectend-@subjectstart)) as Subject,
ltrim(SUBSTRING(@body,@commentsstart,@commentsend-@commentsstart)) as Comments,
@_id as OrigID
FROM dbo.message
WHERE (mailId = @_id)
END
ELSE BEGIN
--below prints the whole value in the debugger (SQL Query Analyzer)
--print @body
--below prints the length of the whole value
--print len(@body)
--below prints the location of 'Callers Name:'
--Print CHARINDEX('Name: ',@body)
--Below is collecting the Name information
--The 6 in the start is compensating for the 6 Characters of "Name: "
SET @namestart = (CHARINDEX('Name:',@body) + 5)
SET @nameend = (CHARINDEX('Email:',@body) - 1)
SET @name = SUBSTRING(@body,@namestart,@nameend-@namestart)
--Below is collecting email information
SET @emailstart = (CHARINDEX('Email:',@body) + 6)
SET @emailend = (CHARINDEX('Title:',@body) -1)
SET @email = SUBSTRING(@body,@emailstart,@emailend-@emailstart)
--Below is collecting Company information
SET @companystart = (CHARINDEX('Company:',@body) + 8)
SET @companyend = (CHARINDEX('Phone Number:',@body) -1)
SET @company = SUBSTRING(@body,@companystart,@companyend-@companystart)
--Below is collecting phone information
SET @phonestart = (CHARINDEX('Phone Number:',@body) + 13)
SET @phoneend = (CHARINDEX('Comments:',@body) -1)
SET @phone = SUBSTRING(@body,@phonestart,@phoneend-@phonestart)
--Below is collecting message information
SET @commentsstart = (CHARINDEX('Comments:',@body) + 9)
SET @commentsend = len(@body)
SET @comments = SUBSTRING(@body,@commentsstart,@commentsend-@commentsstart)
--below is the part that actually writes the data out to the Output table
INSERT INTO OutputEmails
(Name, eMail, Company, Phone, Comments, OrigID)
SELECT
ltrim(SUBSTRING(@body,@namestart,@nameend-@namestart)) as Name,
ltrim(SUBSTRING(@body,@emailstart,@emailend-@emailstart)) as eMail,
ltrim(SUBSTRING(@body,@companystart,@companyend-@companystart)) as Company,
ltrim(SUBSTRING(@body,@phonestart,@phoneend-@phonestart)) as Phone,
ltrim(SUBSTRING(@body,@commentsstart,@commentsend-@commentsstart)) as Comments,
@_id as OrigID
FROM dbo.message
WHERE (mailId = @_id)
END
--At the very end when you're satisfied that the data has been processed properly, delete the appropriate message record from the message table
DELETE FROM MESSAGE Where message.mailID = @_ID
GO
Sample Data that has problems
Fromubject:contact info sent from samplesite.comBody:First name: Sample
Last name: Name
Address 1: 123 Testing Road
Address 2: Don't filled
City: Park Forest
State/Province: FL
Zip/Postal Code: 12345-1234
Country: USA
E-mail address: validemail@email.com
Daytime Phone: 123-123-1234
Evening Phone: Don't filled
Product Name: Sample Product with Cheese
UPC Code: 12345-89521
Date Code: 1251237 D PST . 5290
Purchased At (Store Name): StoreName
Purchase City: Store City
Date Purchased: 12/8/2007
Date Opened: 12/14/2007
E-mail Subject: nail in product
Comments: when I purchased your product, there was something in it. Obviously I am not happy about this. Please contact me. Thank you John Q Public
View 6 Replies
View Related