tbmabry (1/2/2013)
I need to get the emails from the n.Note field, which is a nVarchar(max) column.I have the following, but I get an error saying "Invalid length parameter passed to the LEFT or SUBSTRING Function.
Select s.FirstName, s.LastName, c.CustomerNo, h.OppNo, n.Note,
SUBSTRING(SUBSTRING(n.Note,1,CHARINDEX(' ',n.Note,CHARINDEX('@', n.Note))-1),CHARINDEX('@', n.Note) - CHARINDEX(' ',REVERSE(SUBSTRING(n.Note,1,CHARINDEX('@', n.Note)-1))) + 1, 255)
from SalesLink_SalesRep s,
SalesLink_Prospect_SalesRepCustomerXrf c,
Opportunity_Header h,
Opportunity_Note n
where c.SalesRepId = s.SalesRepId
and c.CustomerNo = h.CustomerNo
and h.OppNo = n.OppNo
and s.SalesRepId in (238, 328)
and n.Note like '%@%'
order by CustomerNo;
Split each of the function in a separate query and run, then you know which function is not working. Alternately filter the records by customer number till you get error batch then check the data for that batch.