I'm running into the "Invalid length parameter passed to the LEFT or SUBSTRING function" error, but it goes away and the query works when I include the column I'm passing into those functions, any clue?
Doesn't work:
SELECT
SQT.QUOTATIONID,
UPPER(LEFT(Email.LOCATOR, CHARINDEX('@', Email.Locator) - 1)) AS Manager
--, Email.LOCATOR
FROM SALESQUOTATIONTABLE AS SQT
INNER JOIN HCMWORKER AS H
ON SQT.WORKERSALESRESPONSIBLE = H.RECID
INNER JOIN DIRPARTYTABLE AS D
ON H.PERSON = D.RECID
INNER JOIN LOGISTICSELECTRONICADDRESS AS Email
ON D.PRIMARYCONTACTEMAIL = Email.RECID
Invalid length parameter passed to the LEFT or SUBSTRING function
Works:
SELECT
SQT.QUOTATIONID,
UPPER(LEFT(Email.LOCATOR, CHARINDEX('@', Email.Locator) - 1)) AS Manager
, Email.LOCATOR
FROM SALESQUOTATIONTABLE AS SQT
INNER JOIN HCMWORKER AS H
ON SQT.WORKERSALESRESPONSIBLE = H.RECID
INNER JOIN DIRPARTYTABLE AS D
ON H.PERSON = D.RECID
INNER JOIN LOGISTICSELECTRONICADDRESS AS Email
ON D.PRIMARYCONTACTEMAIL = Email.RECID
This is continuously repeatable, and the only change I made to the query was included the "Email.LOCATOR" column. This query was working for years and just randomly stopped working today. I'm pretty certain it's a data issue, but am still perplexed why selecting the Email.LOCATOR column fixes the issue.
@. The charindex is calculated as 0 and the LEFT() gets -1 as parameter. – ypercubeᵀᴹ Sep 19 '18 at 20:21SELECTlist, SQL Server retrieves the results in a different order, so you get some results to return first before hitting a failure. – mathewb Sep 19 '18 at 20:29Messages tab says the following:
(1750 rows affected)
(1 row affected)
Note, I verified that 1,750 results is the correct record count too. So I'm definitely getting every record back that I should be.
– J.D. Sep 19 '18 at 20:56Email, you'll hit the error, with or without selecting the column. The list of emails from this query may all have @ but other emails (which should not appear in the results of this query) have not. – ypercubeᵀᴹ Sep 19 '18 at 21:21