Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Trying to use Case Statement to get value into a GL Account with periods in the format Expand / Collapse
Author
Message
Posted Friday, September 28, 2012 9:31 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Tuesday, July 9, 2013 10:28 PM
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
Post #1365963
Posted Friday, September 28, 2012 9:36 AM


Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: 2 days ago @ 7:16 AM
Points: 320, Visits: 1,094
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


Post #1365967
Posted Friday, September 28, 2012 9:52 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Tuesday, July 9, 2013 10:28 PM
Points: 4, Visits: 30
BU is varchar(7)
SUBSIDIARY is varchar(8)
OBJECT is varchar(5)

GL Acct is GL(19)

Post #1365976
Posted Friday, September 28, 2012 10:09 AM


Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: 2 days ago @ 7:16 AM
Points: 320, Visits: 1,094
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


Post #1365991
Posted Sunday, September 30, 2012 11:19 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Tuesday, July 9, 2013 10:28 PM
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

Post #1366275
Posted Sunday, September 30, 2012 11:21 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Tuesday, July 9, 2013 10:28 PM
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.
Post #1366277
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse