pardeshirahul
asked on
need to convert decode to case
I have a query
SELECT RPAD (' ', 2) "FILLER1",
RPAD (gcc.segment5, 6, ' ') "BANK_CODE",
--RPAD(,6) "BANK_CODE",
RPAD (' ', 4) "FILLER2",
RPAD (SUBSTR (TRIM (glh.name), -2), 2) "FINANCIAL_CODE",
RPAD (' ', 4) "FILLER3",
SUBSTR (
RPAD (
( glh.name
|| '-'
|| gll.je_line_num
|| DECODE (gll.description, NULL, NULL, '-')
|| gll.description),
40,
' '),
1,
40)
"WORDING",
RPAD (' ', 2) "FILLER4",
RPAD (NVL (gll.reference_4, ' '), 7, ' ') "CHECK_NUM",
RPAD (NVL (gll.reference_4, ' '), 12, ' ') "NUM",
NVL (gll.reference_4, ' ') "NUM1",
gll.reference_2 "CASH_RECEIPT_ID",
RPAD (' ', 16) "FILLER5",
--(LPAD(TO_CHAR(DECODE,GLH .CURRENCY_ CODE,'USD' ,(DECODE(G LL.ENTERED _DR,NULL,G LL.ENTERED _CR,GLL.EN ETERED_DR) ,(DECODE(G LL.ACCOUNT ED_DR,NULL ,GLL.ACCOU NTED_CR,GL L.ACCOUNTE D_DR)),'99 9999999.00 '),20,' ') "AMOUNT",
DECODE (
glh.currency_code,
'USD', LPAD (
TO_CHAR (
DECODE (gll.entered_dr,
NULL, gll.entered_cr,
gll.entered_dr),
'999999999.00'),
20,
' '),
LPAD (
TO_CHAR (
DECODE (gll.accounted_dr,
NULL, gll.accounted_cr,
gll.accounted_dr),
'999999999.00'),
20,
' '))
"AMOUNT",
RPAD (' ', 2) "FILLER6",
RPAD (TO_CHAR (gll.effective_date, 'DDMMYYYY'), 8, ' ') "DATE",
RPAD (' ', 3) "FILLER7",
RPAD (DECODE (gll.accounted_dr, NULL, 'C', 'D'), 1, ' ')
"DEBIT_CREDIT",
glh.je_header_id "HEADER_ID",
gll.je_line_num "LINE_ID",
glh.je_source "SOURCE",
glh.je_category "CATEGORY",
gll.gl_sl_link_id "GL_SL_LINK_ID",
gll.reference_3 "REFERENCE_3"
FROM apps.gl_je_headers glh,
apps.gl_je_lines gll,
apps.gl_code_combinations gcc
WHERE 1 = 1
AND glh.je_header_id = gll.je_header_id
--AND GLH.JE_HEADER_ID = 2021
AND gcc.code_combination_id = gll.code_combination_id
AND gll.set_of_books_id = p_sob_id
AND gll.period_name = p_period
AND gcc.segment5 = NVL (p_acct, gcc.segment5)
-- AND (GLH.CURRENCY_CODE = DECODE(P_CURRENCY,'LOCAL CURRENCY','EUR')
--OR (GLH.CURRENCY_CODE = DECODE(P_CURRENCY,'FOREIGN CURRENCY',GLH.CURRENCY_COD E) AND GLH.CURRENCY_CODE<>'EUR'))
AND gcc.segment5 INAND (glh.currency_code =
DECODE (p_currency, 'FOREIGN CURRENCY', 'USD')
OR (glh.currency_code =
DECODE (p_currency,
'LOCAL CURRENCY', glh.currency_code)
AND glh.currency_code <> 'USD'))
('10011',
'10012',
'10013',
'10014',
'10015',
'10016',
'10017',
'10018',
'10019',
'10021',
'10023',
'10024',
'10037',
'10036',
'10038',
'10039')
ORDER BY glh.je_source;
this part i have to change
AND (glh.currency_code =
DECODE (p_currency, 'FOREIGN CURRENCY', 'USD')
OR (glh.currency_code =
DECODE (p_currency,
'LOCAL CURRENCY', glh.currency_code)
AND glh.currency_code <> 'USD'))
if foreign curreny then consider 'USD' and 'GBP, instead of just 'USD'
so to do it in decode
or do i have use case statement
SELECT RPAD (' ', 2) "FILLER1",
RPAD (gcc.segment5, 6, ' ') "BANK_CODE",
--RPAD(,6) "BANK_CODE",
RPAD (' ', 4) "FILLER2",
RPAD (SUBSTR (TRIM (glh.name), -2), 2) "FINANCIAL_CODE",
RPAD (' ', 4) "FILLER3",
SUBSTR (
RPAD (
( glh.name
|| '-'
|| gll.je_line_num
|| DECODE (gll.description, NULL, NULL, '-')
|| gll.description),
40,
' '),
1,
40)
"WORDING",
RPAD (' ', 2) "FILLER4",
RPAD (NVL (gll.reference_4, ' '), 7, ' ') "CHECK_NUM",
RPAD (NVL (gll.reference_4, ' '), 12, ' ') "NUM",
NVL (gll.reference_4, ' ') "NUM1",
gll.reference_2 "CASH_RECEIPT_ID",
RPAD (' ', 16) "FILLER5",
--(LPAD(TO_CHAR(DECODE,GLH
DECODE (
glh.currency_code,
'USD', LPAD (
TO_CHAR (
DECODE (gll.entered_dr,
NULL, gll.entered_cr,
gll.entered_dr),
'999999999.00'),
20,
' '),
LPAD (
TO_CHAR (
DECODE (gll.accounted_dr,
NULL, gll.accounted_cr,
gll.accounted_dr),
'999999999.00'),
20,
' '))
"AMOUNT",
RPAD (' ', 2) "FILLER6",
RPAD (TO_CHAR (gll.effective_date, 'DDMMYYYY'), 8, ' ') "DATE",
RPAD (' ', 3) "FILLER7",
RPAD (DECODE (gll.accounted_dr, NULL, 'C', 'D'), 1, ' ')
"DEBIT_CREDIT",
glh.je_header_id "HEADER_ID",
gll.je_line_num "LINE_ID",
glh.je_source "SOURCE",
glh.je_category "CATEGORY",
gll.gl_sl_link_id "GL_SL_LINK_ID",
gll.reference_3 "REFERENCE_3"
FROM apps.gl_je_headers glh,
apps.gl_je_lines gll,
apps.gl_code_combinations gcc
WHERE 1 = 1
AND glh.je_header_id = gll.je_header_id
--AND GLH.JE_HEADER_ID = 2021
AND gcc.code_combination_id = gll.code_combination_id
AND gll.set_of_books_id = p_sob_id
AND gll.period_name = p_period
AND gcc.segment5 = NVL (p_acct, gcc.segment5)
-- AND (GLH.CURRENCY_CODE = DECODE(P_CURRENCY,'LOCAL CURRENCY','EUR')
--OR (GLH.CURRENCY_CODE = DECODE(P_CURRENCY,'FOREIGN
AND gcc.segment5 INAND (glh.currency_code =
DECODE (p_currency, 'FOREIGN CURRENCY', 'USD')
OR (glh.currency_code =
DECODE (p_currency,
'LOCAL CURRENCY', glh.currency_code)
AND glh.currency_code <> 'USD'))
('10011',
'10012',
'10013',
'10014',
'10015',
'10016',
'10017',
'10018',
'10019',
'10021',
'10023',
'10024',
'10037',
'10036',
'10038',
'10039')
ORDER BY glh.je_source;
this part i have to change
AND (glh.currency_code =
DECODE (p_currency, 'FOREIGN CURRENCY', 'USD')
OR (glh.currency_code =
DECODE (p_currency,
'LOCAL CURRENCY', glh.currency_code)
AND glh.currency_code <> 'USD'))
if foreign curreny then consider 'USD' and 'GBP, instead of just 'USD'
so to do it in decode
or do i have use case statement
ASKER
when foreign currency i need to add 'USD', 'GBP'
more generically
decode(value,
acheck,aresult,
bcheck,bresult,
ccheck,cresult,
defaultresult)
is
case value
when acheck then aresult
when bcheck then bresult
when ccheck then cresult
else defaultresult
end
or
case
when value = acheck then aresult
when value = bcheck then bresult
when value = ccheck then cresult
else defaultresult
end
if you don't specify a default result in the decode or case then any value that doesn't find a match will result in NULL being returned
decode(value,
acheck,aresult,
bcheck,bresult,
ccheck,cresult,
defaultresult)
is
case value
when acheck then aresult
when bcheck then bresult
when ccheck then cresult
else defaultresult
end
or
case
when value = acheck then aresult
when value = bcheck then bresult
when value = ccheck then cresult
else defaultresult
end
if you don't specify a default result in the decode or case then any value that doesn't find a match will result in NULL being returned
>>> when foreign currency i need to add 'USD', 'GBP'
what does that mean? you listed one "when" but two results.
what does that mean? you listed one "when" but two results.
>>if foreign curreny then consider 'USD' and 'GBP, instead of just 'USD'
I do not fully inderstand what this means but to guess:
glh.currency_code in (case when p_currency = 'FOREIGN CURRENCY' then 'USD' end,
case when p_currency = 'FOREIGN CURRENCY' then 'GBP' end)
Case or decode will work here.
I do not fully inderstand what this means but to guess:
glh.currency_code in (case when p_currency = 'FOREIGN CURRENCY' then 'USD' end,
case when p_currency = 'FOREIGN CURRENCY' then 'GBP' end)
Case or decode will work here.
ASKER
if foreign currency then take both like in ( 'USD', 'GBP')
else if 'LOCAL_CURRENCY' then dont consider 'USD'
take all the currencies except 'USD'
else if 'LOCAL_CURRENCY' then dont consider 'USD'
take all the currencies except 'USD'
Why use decode at all?
WHERE
...
(
(p_currency ='FOREIGN CURRENCY' and glh.currency_code in ('USD','GBP'))
or
(p_currency ='LOCAL CURRENCY' and glh.currency_code != 'USD')
)
...
WHERE
...
(
(p_currency ='FOREIGN CURRENCY' and glh.currency_code in ('USD','GBP'))
or
(p_currency ='LOCAL CURRENCY' and glh.currency_code != 'USD')
)
...
ASKER
AND (CASE
WHEN p_currency = 'FOREIGN CURRENCY'
THEN glh.currency_code in ( 'USD', 'GBP')
WHEN p_currency = 'LOCAL CURRENCY'
THEN
glh.currency_code != 'USD'
ELSE
NULL
END)
WHEN p_currency = 'FOREIGN CURRENCY'
THEN glh.currency_code in ( 'USD', 'GBP')
WHEN p_currency = 'LOCAL CURRENCY'
THEN
glh.currency_code != 'USD'
ELSE
NULL
END)
ASKER
but it not working am i missing something
your AND doesn't check anything.
a CASE returns a value. you're not doing anything with that value.
have you tried not using decode or case and just use simple and/or as slightwv suggested?
a CASE returns a value. you're not doing anything with that value.
have you tried not using decode or case and just use simple and/or as slightwv suggested?
ASKER
AND (CASE
WHEN p_currency = 'FOREIGN CURRENCY'
THEN glh.currency_code in ( 'USD', 'GBP')
WHEN p_currency = 'LOCAL CURRENCY'
THEN
glh.currency_code != 'USD'
ELSE
NULL
END)
i was just pasting a small part of the query thats why there is AND
the full query is
SELECT RPAD (' ', 2) "FILLER1",
RPAD (gcc.segment5, 6, ' ') "BANK_CODE",
--RPAD(,6) "BANK_CODE",
RPAD (' ', 4) "FILLER2",
RPAD (SUBSTR (TRIM (glh.name), -2), 2) "FINANCIAL_CODE",
RPAD (' ', 4) "FILLER3",
SUBSTR (
RPAD (
( glh.name
|| '-'
|| gll.je_line_num
|| DECODE (gll.description, NULL, NULL, '-')
|| gll.description),
40,
' '),
1,
40)
"WORDING",
RPAD (' ', 2) "FILLER4",
RPAD (NVL (gll.reference_4, ' '), 7, ' ') "CHECK_NUM",
RPAD (NVL (gll.reference_4, ' '), 12, ' ') "NUM",
NVL (gll.reference_4, ' ') "NUM1",
gll.reference_2 "CASH_RECEIPT_ID",
RPAD (' ', 16) "FILLER5",
--(LPAD(TO_CHAR(DECODE,GLH .CURRENCY_ CODE,'USD' ,(DECODE(G LL.ENTERED _DR,NULL,G LL.ENTERED _CR,GLL.EN ETERED_DR) ,(DECODE(G LL.ACCOUNT ED_DR,NULL ,GLL.ACCOU NTED_CR,GL L.ACCOUNTE D_DR)),'99 9999999.00 '),20,' ') "AMOUNT",
DECODE (
glh.currency_code,
'USD', LPAD (
TO_CHAR (
DECODE (gll.entered_dr,
NULL, gll.entered_cr,
gll.entered_dr),
'999999999.00'),
20,
' '),
LPAD (
TO_CHAR (
DECODE (gll.accounted_dr,
NULL, gll.accounted_cr,
gll.accounted_dr),
'999999999.00'),
20,
' '))
"AMOUNT",
RPAD (' ', 2) "FILLER6",
RPAD (TO_CHAR (gll.effective_date, 'DDMMYYYY'), 8, ' ') "DATE",
RPAD (' ', 3) "FILLER7",
RPAD (DECODE (gll.accounted_dr, NULL, 'C', 'D'), 1, ' ')
"DEBIT_CREDIT",
glh.je_header_id "HEADER_ID",
gll.je_line_num "LINE_ID",
glh.je_source "SOURCE",
glh.je_category "CATEGORY",
gll.gl_sl_link_id "GL_SL_LINK_ID",
gll.reference_3 "REFERENCE_3"
FROM apps.gl_je_headers glh,
apps.gl_je_lines gll,
apps.gl_code_combinations gcc
WHERE 1 = 1
AND glh.je_header_id = gll.je_header_id
--AND GLH.JE_HEADER_ID = 2021
AND gcc.code_combination_id = gll.code_combination_id
AND gll.set_of_books_id = p_sob_id
AND gll.period_name = p_period
AND gcc.segment5 = NVL (p_acct, gcc.segment5)
-- AND (GLH.CURRENCY_CODE = DECODE(P_CURRENCY,'LOCAL CURRENCY','EUR')
--OR (GLH.CURRENCY_CODE = DECODE(P_CURRENCY,'FOREIGN CURRENCY',GLH.CURRENCY_COD E) AND GLH.CURRENCY_CODE<>'EUR'))
/*AND (glh.currency_code =
DECODE (p_currency, 'FOREIGN CURRENCY', 'USD')
OR (glh.currency_code =
DECODE (p_currency,
'LOCAL CURRENCY', glh.currency_code)
AND glh.currency_code <> 'USD'))*/
AND (CASE
WHEN p_currency = 'FOREIGN CURRENCY'
THEN glh.currency_code in ( 'USD', 'GBP')
WHEN p_currency = 'LOCAL CURRENCY'
THEN
glh.currency_code != 'USD'
ELSE
NULL
END)
AND gcc.segment5 IN
('10011',
'10012',
'10013',
'10014',
'10015',
'10016',
'10017',
'10018',
'10019',
'10021',
'10023',
'10024',
'10037',
'10036',
'10038',
'10039')
ORDER BY glh.je_source;
WHEN p_currency = 'FOREIGN CURRENCY'
THEN glh.currency_code in ( 'USD', 'GBP')
WHEN p_currency = 'LOCAL CURRENCY'
THEN
glh.currency_code != 'USD'
ELSE
NULL
END)
i was just pasting a small part of the query thats why there is AND
the full query is
SELECT RPAD (' ', 2) "FILLER1",
RPAD (gcc.segment5, 6, ' ') "BANK_CODE",
--RPAD(,6) "BANK_CODE",
RPAD (' ', 4) "FILLER2",
RPAD (SUBSTR (TRIM (glh.name), -2), 2) "FINANCIAL_CODE",
RPAD (' ', 4) "FILLER3",
SUBSTR (
RPAD (
( glh.name
|| '-'
|| gll.je_line_num
|| DECODE (gll.description, NULL, NULL, '-')
|| gll.description),
40,
' '),
1,
40)
"WORDING",
RPAD (' ', 2) "FILLER4",
RPAD (NVL (gll.reference_4, ' '), 7, ' ') "CHECK_NUM",
RPAD (NVL (gll.reference_4, ' '), 12, ' ') "NUM",
NVL (gll.reference_4, ' ') "NUM1",
gll.reference_2 "CASH_RECEIPT_ID",
RPAD (' ', 16) "FILLER5",
--(LPAD(TO_CHAR(DECODE,GLH
DECODE (
glh.currency_code,
'USD', LPAD (
TO_CHAR (
DECODE (gll.entered_dr,
NULL, gll.entered_cr,
gll.entered_dr),
'999999999.00'),
20,
' '),
LPAD (
TO_CHAR (
DECODE (gll.accounted_dr,
NULL, gll.accounted_cr,
gll.accounted_dr),
'999999999.00'),
20,
' '))
"AMOUNT",
RPAD (' ', 2) "FILLER6",
RPAD (TO_CHAR (gll.effective_date, 'DDMMYYYY'), 8, ' ') "DATE",
RPAD (' ', 3) "FILLER7",
RPAD (DECODE (gll.accounted_dr, NULL, 'C', 'D'), 1, ' ')
"DEBIT_CREDIT",
glh.je_header_id "HEADER_ID",
gll.je_line_num "LINE_ID",
glh.je_source "SOURCE",
glh.je_category "CATEGORY",
gll.gl_sl_link_id "GL_SL_LINK_ID",
gll.reference_3 "REFERENCE_3"
FROM apps.gl_je_headers glh,
apps.gl_je_lines gll,
apps.gl_code_combinations gcc
WHERE 1 = 1
AND glh.je_header_id = gll.je_header_id
--AND GLH.JE_HEADER_ID = 2021
AND gcc.code_combination_id = gll.code_combination_id
AND gll.set_of_books_id = p_sob_id
AND gll.period_name = p_period
AND gcc.segment5 = NVL (p_acct, gcc.segment5)
-- AND (GLH.CURRENCY_CODE = DECODE(P_CURRENCY,'LOCAL CURRENCY','EUR')
--OR (GLH.CURRENCY_CODE = DECODE(P_CURRENCY,'FOREIGN
/*AND (glh.currency_code =
DECODE (p_currency, 'FOREIGN CURRENCY', 'USD')
OR (glh.currency_code =
DECODE (p_currency,
'LOCAL CURRENCY', glh.currency_code)
AND glh.currency_code <> 'USD'))*/
AND (CASE
WHEN p_currency = 'FOREIGN CURRENCY'
THEN glh.currency_code in ( 'USD', 'GBP')
WHEN p_currency = 'LOCAL CURRENCY'
THEN
glh.currency_code != 'USD'
ELSE
NULL
END)
AND gcc.segment5 IN
('10011',
'10012',
'10013',
'10014',
'10015',
'10016',
'10017',
'10018',
'10019',
'10021',
'10023',
'10024',
'10037',
'10036',
'10038',
'10039')
ORDER BY glh.je_source;
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
>>WHEN p_currency = 'FOREIGN CURRENCY'
>> THEN glh.currency_code in ( 'USD', 'GBP')
You cannot build a dynamic where clause this way.
If you wish to do dynamic SQL, you need to build a string that contains the SQL, then execute the query.
>> THEN glh.currency_code in ( 'USD', 'GBP')
You cannot build a dynamic where clause this way.
If you wish to do dynamic SQL, you need to build a string that contains the SQL, then execute the query.
it's not really a dynamic condition, it's simply an OR
pardeshirahul,
I appreciate the points, but my answer in http:#a38008585, while correct, was intended to show you how convoluted the CASE option was if you insisted on pursuing that.
The better solution, as stated before, is to NOT to try to use decode or case as you are doing, but instead, to simply use AND and OR as slightwv showed
I appreciate the points, but my answer in http:#a38008585, while correct, was intended to show you how convoluted the CASE option was if you insisted on pursuing that.
The better solution, as stated before, is to NOT to try to use decode or case as you are doing, but instead, to simply use AND and OR as slightwv showed
you can, but there shouldn't be a need that I can see
DECODE (p_currency, 'FOREIGN CURRENCY', 'USD')
would be
case p_currency
when 'FOREIGN CURRENCY' then 'USD'
end
or
case when p_currency = 'FOREIGN CURRENCY' then 'USD' end