SQL case statement works for one view and SSRS report and not another view and SSRS report

I have used the exact case statement (copied from one view to another and one SSRS report to another) and it works in the first view or report and not the second view or report.  I've listed the case statement first and then the table values that are being used.  I get the error:  invalid length parameter passed to the left or substring function.  What is wrong??  As you can see all of the values have a - in them so that isn't the issue.  

CASE WHEN LEFT(FundTypeDescription, CHARINDEX('-', FundTypeDescription) - 2) = 'Educators' THEN 'Faculty' WHEN LEFT(FundTypeDescription, CHARINDEX('-', FundTypeDescription) - 2)  = 'Programs' THEN 'Academic Programs' ELSE LEFT(FundTypeDescription, CHARINDEX('-', FundTypeDescription) - 2) END AS Tenet

Students - Impact
Priority Needs - UND
Priority Needs - College
Educators - Lectureship
Students - Scholarships
Students - Recruitment
Priority Needs - Unrestricted
Facilities - Building
Educators - Enhancement
Students - Research
Educators - Fellowship
Educators - Chair
Educators - Research
Priority Needs - Department
Facilities - Technology
Students - Student Loans
Educators - Professorship
Students - High Achiever
Programs - Programs
cindyfillerAsked:
Who is Participating?
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.

PortletPaulfreelancerCommented:
ADD the dash to the value. If the dash already exists it won't make any difference. If the dash does not exist it will protect against trying LEFT('text',-2) which is what happened for that error.

SELECT
      FundTypeDescription
    , CASE
            WHEN LEFT( FundTypeDescription, CHARINDEX( '-', FundTypeDescription + '-' ) - 2 ) = 'Educators' THEN 'Faculty'
            WHEN LEFT( FundTypeDescription, CHARINDEX( '-', FundTypeDescription + '-' ) - 2 ) = 'Programs' THEN 'Academic Programs'
            WHEN LEN(FundTypeDescription) > 2 THEN LEFT( FundTypeDescription, CHARINDEX( '-', FundTypeDescription + '-' ) - 2 )
      END AS Tenet
FROM ...

Open in new window


by the way, tiny/minor point: that is a "case expression" not a "case statement"
they are quite different

Here's a good reference for the difference: http://infocenter.sybase.com/help/index.jsp?topic=/com.sybase.infocenter.dc00801.1510/html/iqrefso/X315691.htm
0

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
cindyfillerAuthor Commented:
This worked beautifully.  I still don't understand why the old code worked in an old view/report and not the new, but that isn't important!  I'll make note of this solution so I can use it in the future.  Thanks!

Also thanks for providing an explanation for case statement vs expression.  I learn something new every day.
0
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 2008

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.