-2

I am stuck with this error and unable to progress.

Any idea on this error?

CASE WHEN mm.[Moodle Courses Category] + '_' +ISNULL(RIGHT(ttg.TTGP_Group_Code, LEN(ttg.TTGP_Group_Code)-11),'V1') like '%/%' THEN
        mm.[Moodle Courses Category] + '_' +ISNULL(RIGHT(ttg.TTGP_Group_Code, LEN(ttg.TTGP_Group_Code)-11),'V1')
      ELSE
        mm.[Moodle Courses Category] + '_' +ISNULL(RIGHT(ttg.TTGP_Group_Code, LEN(ttg.TTGP_Group_Code)-11),'V1') END AS GROUP_ID

Error: Msg 536, Level 16, State 2, Line 1 Invalid length parameter passed to the RIGHT function.

Aruna Raghunam
  • 903
  • 7
  • 22
  • 43
  • Why are you are unable to progress? Do you understand the words in the error message? Do you know what a "parameter" is? What a "function" is? – Blorgbeard Jun 30 '16 at 21:12
  • `LEN(ttg.TTGP_Group_Code)-11` appear to be negative. This is your error. – Alex Kudryashev Jun 30 '16 at 21:21
  • Don't be lazy. Learn to debug and troubleshoot. You can literally copy the error "Invalid length parameter passed to the RIGHT function." into google and get your answer. – dfundako Jun 30 '16 at 21:24

1 Answers1

0

You have:

RIGHT(ttg.TTGP_Group_Code, LEN(ttg.TTGP_Group_Code)-11)

The error indicates that for the field "ttg.TTGP_Group_Code" there is at least one record in which the length of ttg.TTGP_Group_Code is less than 11 characters. You cannot take the RIGHT of a negative number. So if the length of the field is 10 and you subtract 11 you get -1.

So you need to have a condition to addresses where the length of ttg.TTGP_Group_Code is less than 11 such as:

CASE

    WHEN LEN(ttg.TTGP_Group_Code) <= 11 AND mm.[Moodle Courses Category] + '_' + ISNULL(tg.TTGP_Group_Cod,'V1') like '%/%' THEN
            THEN mm.[Moodle Courses Category] + '_' + ISNULL(tg.TTGP_Group_Cod,'V1')


    WHEN mm.[Moodle Courses Category] + '_' +ISNULL(RIGHT(ttg.TTGP_Group_Code, LEN(ttg.TTGP_Group_Code)-11),'V1') like '%/%' THEN
        mm.[Moodle Courses Category] + '_' +ISNULL(RIGHT(ttg.TTGP_Group_Code, LEN(ttg.TTGP_Group_Code)-11),'V1')
    ELSE
        mm.[Moodle Courses Category] + '_' +ISNULL(RIGHT(ttg.TTGP_Group_Code, LEN(ttg.TTGP_Group_Code)-11),'V1') 
END AS GROUP_ID
Arleigh Reyna
  • 319
  • 1
  • 6