Trying to use Case Statement to get value into a GL Account with periods in the format

  • I am new to SQL and trying to migrate data into a GL account field which has the following format 0000000.00000.00000

    The case statement I wrote is as follows

    CASE

    WHEN [ORDER_TYPE] = 'SS' or [ORDER_TYPE] = 'SP'

    THEN CONVERT(INT,[BU]) + '.' + CONVERT(INT,[OBJECT])

    ELSE

    CONVERT(INT,[BU]) + '.'+ 14350 + '.' + CONVERT(INT,[SUBSIDIARY])

    END

    I got an error stating conversion failed when converting the varchar value '.' to data type int. I have tried to place the . without quotation marks and it does not pass. Alternatively when I used convert(int,'.') it adds up the three values instead of placing them in the GL format.

    I tried this as well

    CASE

    WHEN [ORDER_TYPE] = 'SS' or [ORDER_TYPE] = 'SP'

    THEN CAST([BU] AS VARCHAR(10)) + '.' + CAST([OBJECT] AS VARCHAR(10))

    ELSE

    CASTS([BU] AS VARCHAR(10)) + '.'+ 14350 + '.' + CAST([SUBSIDIARY] AS VARCHAR(10))

    END

    I got the error "conversion failed when converting the varchar value '1003602' to data type int.

    That value is the first BU value from the list.

    Please point me to the right direction. Probably a rookie mistake or I am just totally off.

    Thanks

  • In both of them you're mixing character & numeric data joined by +.

    You need to CAST/CONVERT it all to character.

    What data type are [BU], [SUBSIDUARY] & [OBJECT]?

    CASE

    WHEN [ORDER_TYPE] = 'SS' or [ORDER_TYPE] = 'SP'

    THEN CAST([BU] AS VARCHAR(10)) + '.' + CAST([OBJECT] AS VARCHAR(10))

    ELSE

    CAST([BU] AS VARCHAR(10)) + '.'+ '14350' + '.' + CAST([SUBSIDIARY] AS VARCHAR(10))

    END

    Like this for instance:

    declare @a as table ([BU] Varchar(10), [ORDER_TYPE] Char(2), [SUBSIDIARY] Varchar(10), [OBJECT] Varchar(10));

    insert @a values ( 'BU1', 'SS', 'Slumpco', 'OBJECT1' );

    insert @a values ( 'BU2', 'AA', 'Slumpco2', 'OBJECT2' );

    select

    CASE

    WHEN [ORDER_TYPE] = 'SS' or [ORDER_TYPE] = 'SP'

    THEN [BU] + '.' + [OBJECT]

    ELSE

    [BU] + '.'+ '14350' + '.' + [SUBSIDIARY]

    END

    from @a

  • BU is varchar(7)

    SUBSIDIARY is varchar(8)

    OBJECT is varchar(5)

    GL Acct is GL(19)

  • How about this then:

    If GL Code is 19, the max possible length of the data will be 22 (7+8+5 + 2 full stops)!

    --BU is varchar(7)

    --SUBSIDIARY is varchar(8)

    --OBJECT is varchar(5)

    --GL Acct is GL(19)

    declare @a as table ([BU] Varchar(7), [ORDER_TYPE] Char(2), [SUBSIDIARY] Varchar(8), [OBJECT] Varchar(5));

    insert @a values ( 'BU1', 'SS', 'Slumpco', 'OBJ1' );

    insert @a values ( 'BU2', 'AA', 'Slumpco2', 'OBJ2' );

    select

    CASE

    WHEN [ORDER_TYPE] = 'SS' or [ORDER_TYPE] = 'SP'

    THEN [BU] + '.' + [OBJECT]

    ELSE

    [BU] + '.'+ '14350' + '.' + [SUBSIDIARY]

    END

    from @a

  • I found the solution. I was misreading the error from the very start. I should have started to explain the problem from the root. I was trying to populate a field in maximo from JDE with data from 2 fields in JDE concatenated with a period between them. The first line of code I wrote looked like this

    CASE WHEN [ORDER_TYPE] = 'SS' or [ORDER_TYPE] = 'SP' THEN [BU] + '.' + [OBJECT]

    ELSE [BU] + '.'+ 14350 + '.' [SUBSIDIARY] END

    Message that came was:

    Error converting Varchar value '101202. ' to data type int. 101202 is one of the BU's but what I did not notice is the period after then the single quote. I then went about converting the BU, OBJECT and SUBSIDIARY to int and then all hell begun. What I needed to do was put single quotes on the 14350 and that could have solved the problem. After reading the posted replies, trying several times on Friday, I gave up and slept on it. Played a round of golf on Saturday and it came to me on the 11th hole (which I parred). Got home, tried it out and viola! So this is the final line that populated the GL account field as needed. Long way to do something but with it I learnt more things.

    CASE WHEN [ORDER_TYPE] = 'SS' or [ORDER_TYPE] = 'SP' THEN [BU] + '.' + [OBJECT]

    ELSE [BU] + '.'+ '14350' + '.' [SUBSIDIARY] END

  • Additionally the maximum data length in BU is 4, so GL could hold all the values coming in. Thanks to Laurie.

Viewing 6 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic. Login to reply