I got the error
Invalid length parameter passed to the left or substring function
I understand that negative value is passed and this is reason for the error.
Substring query is to fetch the data between first dash and second dash
Ex: sample data "ABC-123-ABCDEF", expected result "123"
The error is not occurring consistently for the same data, error occur at times and working fine most of the times. I want to understand why the query behaves differently for the same data.
SELECT
item.tril_gid,
CAST(SUBSTRING(comp.fr_productnumber,
CHARINDEX('-', comp.fr_productnumber, 1) + 1,
CHARINDEX('-', comp.fr_productnumber,
CHARINDEX('-', comp.fr_productnumber, 1) + 1) -
CHARINDEX('-', comp.fr_productnumber, 1) - 1) AS INT) AS comp.ProNum
FROM
SCQLI comp WITH(nolock)
LEFT JOIN
ffc ffc WITH(nolock) ON ffc.sc_quote_line_item = comp.tril_gid
LEFT JOIN
ffa ffa WITH(nolock) ON ffa.tril_gid = ffc.ffassembly
LEFT JOIN
scq item WITH(nolock) ON item.tril_gid = ffa.sc_quote_line_item
INNER JOIN
jde jde WITH(nolock) ON jde.tril_gid = item.fr_jde_order
INNER JOIN
frq frq WITH(nolock) ON frq.tril_gid = jde.frquoterevision
WHERE
comp.fr_jde_order = 'QFXZZBSHH1YRFZULBEBS3C4HULDV42VR'
AND comp.FR_ProductNumber <> ''