Domanda

I keep getting this pesky error whenever I try this code:

 select 
    gam.acid,
        gam.foracid,
        gam.acct_name,
        gam.sol_id,
        sol.sol_desc,
        gam.gl_sub_head_code,
        case gsh.gl_code
             when cast(gl_code as int) >= 01 and cast(gl_code as int) <=13
                      or cast(gl_code as int) >= 15 and cast(gl_code as int) <=24 then 'A'
                 when cast(gl_code as int) >= 28 and cast(gl_code as int) <= 31
                     or cast(gl_code as int) >= 33 and cast(gl_code as int) <= 39
                     or cast(gl_code as int) = 41
                     or cast(gl_code as int) >= 43 and cast(gl_code as int) <= 48 then 'L'
                 when cast(gl_code as int) = 51
                     or cast(gl_code as int) = 53
                     or cast(gl_code as int) >= 55 and cast(gl_code as int) <= 57 then 'C'
                 when cast(gl_code as int) >= 61 and cast(gl_code as int) <= 70
                     or cast(gl_code as int) >= 72 and cast(gl_code as int) <= 81 then 'I'
                 when cast(gl_code as int) = 84 or cast(gl_code as int) = 85
                     or cast(gl_code as int) >= 87 and cast(gl_code as int) <= 94 then 'E'
                 when cast(gl_code as int) >= 97 and cast(gl_code as int) <= 99 then 'S'
                 else null
          end as gl_classification
    from tbaadm.gam

I have the feeling that some things are out of place in the code, or I forgot to do/add something. Any help would be appreciated.

È stato utile?

Soluzione

The CASE construct has two alternatives syntaxes:

CASE foo WHEN 1 THEN 'blah' END
CASE WHEN foo=1 THEN 'blah' END

You're trying to use both at the same time:

case gsh.gl_code
when cast(gl_code as int) >= 01

It should be:

  select case
         --gsh.gl_code  remove this 
            when cast(gl_code as int) >= 01 and cast(gl_code as int) <=13
                or cast(gl_code as int) >= 15 and cast(gl_code as int) <=24 then 'A'
            when cast(gl_code as int) >= 28 and cast(gl_code as int) <= 31
                or cast(gl_code as int) >= 33 and cast(gl_code as int) <= 39
                or cast(gl_code as int) = 41
                or cast(gl_code as int) >= 43 and cast(gl_code as int) <= 48 then 'L'
            when cast(gl_code as int) = 51
                or cast(gl_code as int) = 53
                or cast(gl_code as int) >= 55 and cast(gl_code as int) <= 57 then 'C'
            when cast(gl_code as int) >= 61 and cast(gl_code as int) <= 70
                or cast(gl_code as int) >= 72 and cast(gl_code as int) <= 81 then 'I'
            when cast(gl_code as int) = 84 or cast(gl_code as int) = 85
                or cast(gl_code as int) >= 87 and cast(gl_code as int) <= 94 then 'E'
            when cast(gl_code as int) >= 97 and cast(gl_code as int) <= 99 then 'S'
            else null
        end as gl_classification
   from tbaadm.gam

Altri suggerimenti

You could try using brackets to give a priority to the AND/OR conditions. For example:

when (cast(gl_code as int) >= 01 and cast(gl_code as int) <=13)
    or (cast(gl_code as int) >= 15 and cast(gl_code as int) <=24) then 'A'

it might help...

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top