SQL Query

I'm trying to understand this piece of code. The part that is confusing me is the Else Case. I understand that the Substring function extract data from an expression, and the CharIndex returns a number, which i think represents the length of characters that the Substring function will output.

1. What is the -1 doing to the Charindex function.
2. What does the code do to this value in the table. What would the 90A- become after the code has been executed. And why?:

RSPF_PROD_N
        90A-





SELECT DISTINCT
CASE
    WHEN CHARINDEX('-',RSPF.RSPF_PROD_N) = 0 THEN LTRIM(RTRIM(RSPF.RSPF_PROD_N))
    ELSE CASE
            WHEN RSPF.RSPF_R_OR_S = 'R' THEN SUBSTRING(LTRIM(RTRIM(RSPF.RSPF_PROD_N)), 1, CHARINDEX('-',LTRIM(RTRIM(RSPF.RSPF_PROD_N)))-1)
            ELSE LTRIM(RTRIM(RSPF.RSPF_PROD_N))
    END
END AS PRODUCT_NUM
LVL 1
isamesAsked:
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.

UnifiedISCommented:
CHARINDEx represents the position of a character, the -1 moves the value back one spot to capture everything up to but not including the found value.
The first condition would fail (CHARINDEX('-'...) does not = 0) so "90A-" would fall to the second case statement.
The value of RSPF.RSPF_R_OR_S  determines what is returned.  What is that value?
0
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
>CHARINDEX('-',LTRIM(RTRIM(RSPF.RSPF_PROD_N)))-1)
LTRIM(RTRIM(x)) means remove any leading and trailing spaces from x
CHARINDEX('-', x) means search x for the first instance of '-', and return that character position

For example, the below code returns 4, as the dash is the 4th character from the start
SELECT CHARINDEX('-', '123-456') 

Open in new window


So -1 would be 3.
0
UnifiedISCommented:
I replaced your database fields with variables so you can fiddle with values to see how the results change

DECLARE @RSPF_PROD_N varchar(50)
DECLARE @RSPF_R_OR_S varchar(50)
SET @RSPF_PROD_N = '90A-'
SET @RSPF_R_OR_S = 'R'

SELECT DISTINCT
CASE
    WHEN CHARINDEX('-',@RSPF_PROD_N) = 0 THEN LTRIM(RTRIM(@RSPF_PROD_N))
    ELSE CASE
            WHEN @RSPF_R_OR_S = 'R' THEN SUBSTRING(LTRIM(RTRIM(@RSPF_PROD_N)), 1, CHARINDEX('-',LTRIM(RTRIM(@RSPF_PROD_N)))-1)
            ELSE LTRIM(RTRIM(@RSPF_PROD_N))
    END
END AS PRODUCT_NUM
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
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

From novice to tech pro — start learning today.