SQL Stored Procedure - GetDataView Length parameter passed to SUBSTRING function error

I am receiving the fairly common "GetDataView Length parameter passed to SUBSTRING function" error when my server side code calls the respective SQL Stored proc to save the data.

The code in the SP is as follows:
Employee_First_Nm = case
                 When @sEventType = 'EI'
                  (select em.First_Name_Display_Tx from vw_HSRS_People em where em.AD_Account_Tx = @sEmployee)
                         SUBSTRING(@sEmployeeCd, CHARINDEX(',', @sEmployeeCd)  + 1, LEN(@sEmployeeCd)) End,  
Employee_Last_Nm = Case
                 When @sEventType = 'EI'
                      (select em.Last_Name_Tx from vw_HSRS_People em where em.AD_Account_Tx = @sEmployee)
                      SUBSTRING(@sEmployeeCd, 0, CHARINDEX(',', @sEmployeeCd)) End,

As you can see I am negating the substring if the @EventType = 'EI'.   I verified in debug mode that @EventType is in fact equal 'EI'...
So why am I getting the Substring error?  It should be going to the THEN logic in the case statement, correct?
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

lcohanDatabase AnalystCommented:
What exact error are you getting? Did you considered the NULL's returned by whatever reason that will mess-up your SUBSTRING function?
66chawgerAuthor Commented:

Exact error is:

GetDataView: Invalid Length parameter passed to SUBSTRING function.  

Why would the SUBSTRING function even come into focus in this situation because the condition for the case statement should process the THEN logic.... Am I missing something?   Yes, I understand about the nulls, but what exactly is occurring here to cause the SUBSTRING portion of the case statement to be validated???
lcohanDatabase AnalystCommented:
It is definitely going on the ELSE branch for whatever reason and either a negative values (most common) or some NULL gets into the SUBSTRING function evaluation.
I deal with these kind of issues by (old kindergarten school) using PRINT 'Now I am here bla-bla....'; commands instead of the actual SUBSTRING or whatever to see what your SELECT returns.
Active Protection takes the fight to cryptojacking

While there were several headline-grabbing ransomware attacks during in 2017, another big threat started appearing at the same time that didn’t get the same coverage – illicit cryptomining.

66chawgerAuthor Commented:
I agree.... ironic you should mention using print... I use that outside the select, but when I put a print inside the select sql won't parse....  Are you saying comment the substring and put a print below?

I did do a print on the Event_type_cd before the select and it is definitely = 'EI'..... Also picking at straws, I tried using both != and <>...   <> is standard, but both are accepted.
lcohanDatabase AnalystCommented:
I'm not sure where/how you use the statement you posted (is in an UPDATE or you need to fill in variables via a SELECT ,which is wrong btw...>) but I just used your query and flipped it as a SELECT to see where you are...
SELECT       case
        When @sEventType = 'EI'
        THEN 'here on Employee_First_Nm - THEN...'
         --(select em.First_Name_Display_Tx from vw_HSRS_People em where em.AD_Account_Tx = @sEmployee)
        Else 'here on Employee_First_Nm - ELSE...'
         --SUBSTRING(@sEmployeeCd, CHARINDEX(',', @sEmployeeCd)  + 1, LEN(@sEmployeeCd))
        End AS Employee_First_Nm,  
                  When @sEventType = 'EI'
                  Then 'here on Employee_Last_Nm - THEN...'
                        --(select em.Last_Name_Tx from vw_HSRS_People em where em.AD_Account_Tx = @sEmployee)
                  Else 'here on Employee_Last_Nm - ELSE...'
                --SUBSTRING(@sEmployeeCd, 0, CHARINDEX(',', @sEmployeeCd))
                  End AS Employee_Last_Nm,

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
66chawgerAuthor Commented:
This is an insert into using the select.

" (is in an UPDATE or you need to fill in variables via a SELECT ,which is wrong btw...>)" ?
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2005

From novice to tech pro — start learning today.