If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > COALESCE Function conversion

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-02-12, 10:33
madsongtel madsongtel is offline
Registered User
 
Join Date: May 2011
Posts: 9
COALESCE Function conversion

Hi All,

i am new to DB2 and i have to convert below query into CASE function, i should not use COALESCE function in my query, inplace of COALESCE function i should use CASE function. can some one please help me

COALESCE(((Case
When SUBSTR(L.LOCATION,1,3) = '123' Then
(Select SUBSTR(LOC_CD,1,3) from POST Where POST_CD = L.POST_TX)
When SUBSTR(L.Location,1,3) = '234' Then
(Select REG_CD from RGN Where L.POST_TX BETWEEN CD1 AND CD2)
Else ' '
End ) ),( ' ' )) as ADDRESS_CODE from LOCATION_TB L
Reply With Quote
  #2 (permalink)  
Old 01-02-12, 12:10
tonkuma tonkuma is online now
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,193
Quote:
... i have to convert below query into CASE function, i should not use COALESCE function in my query, ...
I couldn't find any reason not to use COALESCE function, except for exercise.
Could you explain the reason?

Anyway, your query might be rewritten to...
Code:
SELECT
       COALESCE(
          SUBSTR(LOC_CD,1,3)
        , REG_CD
        , ' '
       ) as ADDRESS_CODE
 from  LOCATION_TB L 
 LEFT  OUTER JOIN
       POST
   ON  SUBSTR(L.LOCATION,1,3) = '123'
   AND L.POST_TX = POST_CD
 LEFT  OUTER JOIN
       RGN
   ON  SUBSTR(L.LOCATION,1,3) = '234'
   AND L.POST_TX BETWEEN CD1 AND CD2
;
Reply With Quote
  #3 (permalink)  
Old 01-02-12, 12:24
tonkuma tonkuma is online now
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,193
Or, calculate SUBSTR(LOCATION,1,3) once.

Code:
SELECT
       COALESCE(
          SUBSTR(LOC_CD,1,3)
        , REG_CD
        , ' '
       ) as ADDRESS_CODE
 from  (SELECT L.*
             , SUBSTR(LOCATION,1,3) AS cd3
         FROM  LOCATION_TB L
       ) L 
 LEFT  OUTER JOIN
       POST
   ON  L.cd3     = '123'
   AND L.POST_TX = POST_CD
 LEFT  OUTER JOIN
       RGN
   ON  L.cd3     = '234'
   AND L.POST_TX BETWEEN CD1 AND CD2
;
Reply With Quote
  #4 (permalink)  
Old 01-02-12, 12:26
madsongtel madsongtel is offline
Registered User
 
Join Date: May 2011
Posts: 9
Thank you ...solved my problem
Reply With Quote
  #5 (permalink)  
Old 01-02-12, 13:45
tonkuma tonkuma is online now
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,193
Sorry!
I forgot a simple modification for your original query, like...

Code:
SELECT
       COALESCE(
          (SELECT SUBSTR(LOC_CD,1,3)
            FROM  POST
            WHERE SUBSTR(L.LOCATION,1,3) = '123'
              AND L.POST_TX = POST_CD
          )
        , (SELECT REG_CD
            FROM  RGN
            WHERE SUBSTR(L.LOCATION,1,3) = '234'
              AND L.POST_TX BETWEEN CD1 AND CD2
          )
        , ' '
       ) as ADDRESS_CODE
 from  LOCATION_TB L 
;
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On