SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


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


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

Author
Message
Ras P
Ras P
Forum Newbie
Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)

Group: General Forum Members
Points: 4 Visits: 30
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
laurie-789651
laurie-789651
SSChasing Mays
SSChasing Mays (634 reputation)SSChasing Mays (634 reputation)SSChasing Mays (634 reputation)SSChasing Mays (634 reputation)SSChasing Mays (634 reputation)SSChasing Mays (634 reputation)SSChasing Mays (634 reputation)SSChasing Mays (634 reputation)

Group: General Forum Members
Points: 634 Visits: 1272
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



Ras P
Ras P
Forum Newbie
Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)

Group: General Forum Members
Points: 4 Visits: 30
BU is varchar(7)
SUBSIDIARY is varchar(8)
OBJECT is varchar(5)

GL Acct is GL(19)
laurie-789651
laurie-789651
SSChasing Mays
SSChasing Mays (634 reputation)SSChasing Mays (634 reputation)SSChasing Mays (634 reputation)SSChasing Mays (634 reputation)SSChasing Mays (634 reputation)SSChasing Mays (634 reputation)SSChasing Mays (634 reputation)SSChasing Mays (634 reputation)

Group: General Forum Members
Points: 634 Visits: 1272
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



Ras P
Ras P
Forum Newbie
Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)

Group: General Forum Members
Points: 4 Visits: 30
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


Ras P
Ras P
Forum Newbie
Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)

Group: General Forum Members
Points: 4 Visits: 30
Additionally the maximum data length in BU is 4, so GL could hold all the values coming in. Thanks to Laurie.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search