By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
462,145 Members | 864 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 462,145 IT Pros & Developers. It's quick & easy.

Problems with decode function in Oracle ... Turning me mad

P: 33
Hi,
I am facing a strange problem with decode function in oracle. My table name is status_hist. Below is the query I am hitting on this table:

select max(decode(voided_flag,'Y',null,decode(status_hist .status,'WD',status_date)))
from status_hist
where sk_seq=6574

The result returned is '29-SEP-05'

However, I checked out the table status_hist to see that for sk_seq=6574 and voided_flag not equals to 'Y' and status='WD', the maximum status_date is '12/22/2005 10:28:29 AM' . However, if I use the CASE-WHEN function, then I get accurate results.

Also, when I hit the query

select max(to_date(decode(voided_flag,'Y',null,decode(sta tus_hist.status,'WD',status_date)),'dd-mon-yy'))
from status_hist
where sk_seq=6574

the result is '12/22/2005'

Can anyone please give an explanation to this?
Mar 6 '07 #1
Share this Question
Share on Google+
16 Replies

100+
P: 153
did you copy these statements exactly from what you tried?
reason i ask is that in the one that is not working there is a space between status_hist and the .status

is status_date a date field or varchar2 ?
Mar 6 '07 #2

P: 33
did you copy these statements exactly from what you tried?
reason i ask is that in the one that is not working there is a space between status_hist and the .status

is status_date a date field or varchar2 ?



Hi dave,
Actually the name of the table is a not the same as I have in my production database. But, the rest of the code is the exact replicate of the one I have hit in my database.
The status_date is of type DATE. I have alsochecked out using to_date function, in that case also, the value returned is right one, but the hour, minute,second part is missing. The output is then '22-DEC-05'. But, the CASE-WHEN gives exact results. I don't know why this is happening
Mar 7 '07 #3

100+
P: 153
Hi dave,
Actually the name of the table is a not the same as I have in my production database. But, the rest of the code is the exact replicate of the one I have hit in my database.
The status_date is of type DATE. I have alsochecked out using to_date function, in that case also, the value returned is right one, but the hour, minute,second part is missing. The output is then '22-DEC-05'. But, the CASE-WHEN gives exact results. I don't know why this is happening

Your going to have to help me re create your scenario... cause it seems to work for me.

Expand|Select|Wrap|Line Numbers
  1. [141]dave@ORADB> create table status_hist (
  2.   2  sk_seq       number,
  3.   3  status_date  date,
  4.   4  status       varchar2(10),
  5.   5  voided_flag  varchar2(1)
  6.   6  );
  7.  
  8. Table created.
  9.  
  10. Elapsed: 00:00:00.03
  11. [141]dave@ORADB> 
  12. [141]dave@ORADB> insert into status_hist 
  13.   2    values (6574, to_date('12/22/2005 10:28:29 AM','mm/dd/yyyy hh:mi:ss pm'),'WD','N');
  14.  
  15. 1 row created.
  16.  
  17. Elapsed: 00:00:00.00
  18. [141]dave@ORADB> 
  19. [141]dave@ORADB> 
  20. [141]dave@ORADB> select max(decode(voided_flag,'Y',null,decode(status_hist.status,'WD',status_date))
  21. )
  22.   2  from status_hist 
  23.   3  where sk_seq=6574;
  24.  
  25. MAX(DECOD
  26. ---------
  27. 22-DEC-05
  28.  
  29. Elapsed: 00:00:00.01
  30. [141]dave@ORADB> 
  31. [141]dave@ORADB> select max(to_date(decode(voided_flag,'Y',null,decode(status_hist.status,'WD',statu
  32. s_date) ),'dd-mon-yy') )
  33.   2  from status_hist 
  34.   3  where sk_seq=6574;
  35.  
  36. MAX(TO_DA
  37. ---------
  38. 22-DEC-05
  39.  
  40.  
you shouldnt have to do the to_date in there though... its already a date.

Expand|Select|Wrap|Line Numbers
  1. [141]dave@ORADB> SELECT MAX(DECODE(voided_flag, 'Y', NULL, DECODE(status_hist.status, 'WD', status_d
  2. ate) ) ) my_date
  3.   2  FROM   status_hist
  4.   3  WHERE  sk_seq = 6574;
  5.  
  6. MY_DATE
  7. ---------
  8. 22-DEC-05
  9.  
do i have the right data in the table? does the other date that you are getting exist in the table?
Mar 7 '07 #4

P: 33
Your going to have to help me re create your scenario... cause it seems to work for me.

Expand|Select|Wrap|Line Numbers
  1. [141]dave@ORADB> create table status_hist (
  2.   2  sk_seq       number,
  3.   3  status_date  date,
  4.   4  status       varchar2(10),
  5.   5  voided_flag  varchar2(1)
  6.   6  );
  7.  
  8. Table created.
  9.  
  10. Elapsed: 00:00:00.03
  11. [141]dave@ORADB> 
  12. [141]dave@ORADB> insert into status_hist 
  13.   2    values (6574, to_date('12/22/2005 10:28:29 AM','mm/dd/yyyy hh:mi:ss pm'),'WD','N');
  14.  
  15. 1 row created.
  16.  
  17. Elapsed: 00:00:00.00
  18. [141]dave@ORADB> 
  19. [141]dave@ORADB> 
  20. [141]dave@ORADB> select max(decode(voided_flag,'Y',null,decode(status_hist.status,'WD',status_date))
  21. )
  22.   2  from status_hist 
  23.   3  where sk_seq=6574;
  24.  
  25. MAX(DECOD
  26. ---------
  27. 22-DEC-05
  28.  
  29. Elapsed: 00:00:00.01
  30. [141]dave@ORADB> 
  31. [141]dave@ORADB> select max(to_date(decode(voided_flag,'Y',null,decode(status_hist.status,'WD',statu
  32. s_date) ),'dd-mon-yy') )
  33.   2  from status_hist 
  34.   3  where sk_seq=6574;
  35.  
  36. MAX(TO_DA
  37. ---------
  38. 22-DEC-05
  39.  
  40.  
you shouldnt have to do the to_date in there though... its already a date.

Expand|Select|Wrap|Line Numbers
  1. [141]dave@ORADB> SELECT MAX(DECODE(voided_flag, 'Y', NULL, DECODE(status_hist.status, 'WD', status_d
  2. ate) ) ) my_date
  3.   2  FROM   status_hist
  4.   3  WHERE  sk_seq = 6574;
  5.  
  6. MY_DATE
  7. ---------
  8. 22-DEC-05
  9.  
do i have the right data in the table? does the other date that you are getting exist in the table?

Hi Dave,
First of all thanks a lot for the effort you are pouring in. But, the problem is that the table is a huge table and it is not possible to replicate that table in our schema. The problem is that the error I am getting is for this particular table only. For some other tables, the query is running fine. But what actuall I want to know is that why the decode & case-when are giving different results, whereas ideally they should be the same. Also, I know that the status_date is of type date, but when I give the to_date function, then at least the value returned is correct(though minute, hour & second details are not there). Why does this happen? I need to know the logic behind this apparaently impossible phenomenon
Mar 7 '07 #5

100+
P: 153
ok, it looks like the decode is returning the data type of the first data type possibility, in this case as a string, therefore, without a to_char immediately around the date field being returned in the second decode function, the system is doing the default date to string output which is just the year month and day info (no hours minutes and seconds and hence why you are losing them).

So try putting the to_Char immediately around the the status_date field.

Expand|Select|Wrap|Line Numbers
  1. [153]dave@ORADB> SELECT MAX(DECODE(voided_flag, 'Y', NULL, DECODE(status_hist.status, 'WD', to_Char(
  2. status_date,'mm/dd/yyyy hh:mi:ss pm') ) ) ) my_date
  3.   2  FROM   status_hist
  4.   3  WHERE  sk_seq = 6574;
  5.  
  6. MY_DATE
  7. ----------------------
  8. 12/22/2005 10:28:29 am
  9.  
  10.  
sorry for the delay in getting back to you... been working some rediculous hours of late.
Mar 8 '07 #6

P: 33
ok, it looks like the decode is returning the data type of the first data type possibility, in this case as a string, therefore, without a to_char immediately around the date field being returned in the second decode function, the system is doing the default date to string output which is just the year month and day info (no hours minutes and seconds and hence why you are losing them).

So try putting the to_Char immediately around the the status_date field.

Expand|Select|Wrap|Line Numbers
  1. [153]dave@ORADB> SELECT MAX(DECODE(voided_flag, 'Y', NULL, DECODE(status_hist.status, 'WD', to_Char(
  2. status_date,'mm/dd/yyyy hh:mi:ss pm') ) ) ) my_date
  3.   2  FROM   status_hist
  4.   3  WHERE  sk_seq = 6574;
  5.  
  6. MY_DATE
  7. ----------------------
  8. 12/22/2005 10:28:29 am
  9.  
  10.  
sorry for the delay in getting back to you... been working some rediculous hours of late.



Hi Dave,
First of all, the query you posted should be modified a bit using the to_date function. But, my original query was a bit different. What made me go mad was that the disparity between the "CASE-WHEN" and "DECODE" functions. The outcome of the query clearly reveals that since the voided_flag is of type varchar2, the date value returned by decode becomes of type character .. that's why I used the to_date function. But the strange thing is that "CASE-WHEN" is behaving properly. Is this a permanent distinction between these two functions or this is just an isolate incident. What I want to do is to generalize a rule for the decode. Can you provide me any such documents? However, thanks a lot for your concern. I would like to keep this discussion alive. Would appreciate a lot if you can help me out on my query (difference between processing logic of CASE-WHEN & DECODE)
Mar 8 '07 #7

P: 33
Hi Dave,
First of all, the query you posted should be modified a bit using the to_date function. But, my original query was a bit different. What made me go mad was that the disparity between the "CASE-WHEN" and "DECODE" functions. The outcome of the query clearly reveals that since the voided_flag is of type varchar2, the date value returned by decode becomes of type character .. that's why I used the to_date function. But the strange thing is that "CASE-WHEN" is behaving properly. Is this a permanent distinction between these two functions or this is just an isolate incident. What I want to do is to generalize a rule for the decode. Can you provide me any such documents? However, thanks a lot for your concern. I would like to keep this discussion alive. Would appreciate a lot if you can help me out on my query (difference between processing logic of CASE-WHEN & DECODE)







Hi Dave,
Just checked out something interesting about decode. It can be of help....

This is my test table
ID NAME MARKS ADDRESS FLAG
------ ------------------------- ---------- ------------------------- -
124334 Ayan 75 Chennai Y
143223 Dibendu 84 Hazra N
126466 Avinash 69 Salt Lake N
132443 Raju 66 Rashbihari Y
133246 Bibek 79 Bangalore
133411 Anish 84 Maniktala
133367 Raju 83 Baguihati Y
133332 Sayantan 82 Uttarpara N
133403 Anirban 89 Sovabazar
133388 Rahul Kumar 92 Salt Lake
121212 Sarat 76 Sovabajar Y


I hit this query in the database:
select id, name,
decode(compsc_flag,'Y',marks,address)
from college;
As expected, an error is returned due to datatype mismatch
But, when I hit the query
select id, name,
decode(compsc_flag,'Y',address,marks)
from college;
no error is returned.........
CASE WHEN always returns an error in this regard...
select id,(case when compsc_flag='Y' then marks else address end)
from college;


select id,(case when compsc_flag='Y' then address else marks end)
from college;

both return error... We have to convert the marks to to_char to get the correct result.

This seems to be a difference between the two functions and a strange one too
Mar 8 '07 #8

100+
P: 153
Hi Dave,
First of all, the query you posted should be modified a bit using the to_date function. But, my original query was a bit different. What made me go mad was that the disparity between the "CASE-WHEN" and "DECODE" functions. The outcome of the query clearly reveals that since the voided_flag is of type varchar2, the date value returned by decode becomes of type character .. that's why I used the to_date function. But the strange thing is that "CASE-WHEN" is behaving properly. Is this a permanent distinction between these two functions or this is just an isolate incident. What I want to do is to generalize a rule for the decode. Can you provide me any such documents? However, thanks a lot for your concern. I would like to keep this discussion alive. Would appreciate a lot if you can help me out on my query (difference between processing logic of CASE-WHEN & DECODE)
I wasnt trying to return the exact query back as much as demonstrate my point. decode is a function that was created a long time ago. I double checked, it does return the datatype of the first data type possibility. so because the default date out has no time values that data is lost. so the to_date re created the string back to a date but the time portion didnt exist and therefore couldnt be created.

the case when code was created as an evolution of the decode. they werent created to be equal, case-when is a more powerful tool. but it is bound by having the same datatype in all possibilities.

you never did show me the case when statement. are you sure that logically they were identicle? if i could see it, then i could let you know.
Mar 8 '07 #9

100+
P: 153
Hi Dave,
Just checked out something interesting about decode. It can be of help....

This is my test table
ID NAME MARKS ADDRESS FLAG
------ ------------------------- ---------- ------------------------- -
124334 Ayan 75 Chennai Y
143223 Dibendu 84 Hazra N
126466 Avinash 69 Salt Lake N
132443 Raju 66 Rashbihari Y
133246 Bibek 79 Bangalore
133411 Anish 84 Maniktala
133367 Raju 83 Baguihati Y
133332 Sayantan 82 Uttarpara N
133403 Anirban 89 Sovabazar
133388 Rahul Kumar 92 Salt Lake
121212 Sarat 76 Sovabajar Y


I hit this query in the database:
select id, name,
decode(compsc_flag,'Y',marks,address)
from college;
As expected, an error is returned due to datatype mismatch
But, when I hit the query
select id, name,
decode(compsc_flag,'Y',address,marks)
from college;
no error is returned.........
CASE WHEN always returns an error in this regard...
select id,(case when compsc_flag='Y' then marks else address end)
from college;


select id,(case when compsc_flag='Y' then address else marks end)
from college;

both return error... We have to convert the marks to to_char to get the correct result.

This seems to be a difference between the two functions and a strange one too
right, this makes sense. the decode errors occur because oracle can implicitly convert a number to a string, but you cant convert an alpha-numberic to a number (implicitly or explicitly).

and the case-when requires that all possible outputs are the same datatype. it is interesting though that it doesnt do implicit conversions of dates to chars
Mar 8 '07 #10

100+
P: 153
So, in going back to the original question.

the difference in the results from these two queries:

Expand|Select|Wrap|Line Numbers
  1. select max(decode(voided_flag,'Y',null,decode(status_hist.status,'WD',status_date)))
  2. from status_hist 
  3. where sk_seq=6574
  4.  
  5.  
  6. select max(to_date(decode(voided_flag,'Y',null,decode(status_hist.status,'WD',status_date)),'dd-mon-yy'))
  7. from status_hist 
  8. where sk_seq=6574
  9.  
  10.  
is because max on a string is going to return different results than max on a date. and max on the same to_char(date_field) will be entirely dependant on the format it is output in.

Expand|Select|Wrap|Line Numbers
  1. [143]dave44@ORADB> create table temp (dat   date);
  2.  
  3. Table created.
  4.  
  5. Elapsed: 00:00:02.53
  6. [143]dave44@ORADB> 
  7. [143]dave44@ORADB> insert into temp values (sysdate);
  8.  
  9. 1 row created.
  10.  
  11. Elapsed: 00:00:00.70
  12. [143]dave44@ORADB> 
  13. [143]dave44@ORADB> insert into temp values (sysdate-10);
  14.  
  15. 1 row created.
  16.  
  17. Elapsed: 00:00:00.53
  18. [143]dave44@ORADB> 
  19. [143]dave44@ORADB> commit;
  20.  
  21. Commit complete.
  22.  
  23. Elapsed: 00:00:00.57
  24. [143]dave44@ORADB> 
  25. [143]dave44@ORADB> select max(dat) from temp;
  26.  
  27. MAX(DAT)
  28. ---------
  29. 08-MAR-07
  30.  
  31. Elapsed: 00:00:00.92
  32. [143]dave44@ORADB> 
  33. [143]dave44@ORADB> select max(to_char(dat,'dd/mm/yyyy'))
  34.   2  from temp;
  35.  
  36. MAX(TO_CHA
  37. ----------
  38. 26/02/2007
  39.  
  40. Elapsed: 00:00:00.65
  41. [143]dave44@ORADB> select max(to_char(dat,'mm/dd/yyyy'))
  42.   2  from temp;
  43.  
  44. MAX(TO_CHA
  45. ----------
  46. 03/08/2007
  47.  
Mar 8 '07 #11

P: 33
I wasnt trying to return the exact query back as much as demonstrate my point. decode is a function that was created a long time ago. I double checked, it does return the datatype of the first data type possibility. so because the default date out has no time values that data is lost. so the to_date re created the string back to a date but the time portion didnt exist and therefore couldnt be created.

the case when code was created as an evolution of the decode. they werent created to be equal, case-when is a more powerful tool. but it is bound by having the same datatype in all possibilities.

you never did show me the case when statement. are you sure that logically they were identicle? if i could see it, then i could let you know.


Yeah, they are the same logically. I checked it out.

select max(case when voided_flag='Y' then null
else (case when status_hist.status='WD' then status_date else null end)end) from status_hist
where sk_seq=706379
Mar 9 '07 #12

P: 33
So, in going back to the original question.

the difference in the results from these two queries:

Expand|Select|Wrap|Line Numbers
  1. select max(decode(voided_flag,'Y',null,decode(status_hist.status,'WD',status_date)))
  2. from status_hist 
  3. where sk_seq=6574
  4.  
  5.  
  6. select max(to_date(decode(voided_flag,'Y',null,decode(status_hist.status,'WD',status_date)),'dd-mon-yy'))
  7. from status_hist 
  8. where sk_seq=6574
  9.  
  10.  
is because max on a string is going to return different results than max on a date. and max on the same to_char(date_field) will be entirely dependant on the format it is output in.

Expand|Select|Wrap|Line Numbers
  1. [143]dave44@ORADB> create table temp (dat   date);
  2.  
  3. Table created.
  4.  
  5. Elapsed: 00:00:02.53
  6. [143]dave44@ORADB> 
  7. [143]dave44@ORADB> insert into temp values (sysdate);
  8.  
  9. 1 row created.
  10.  
  11. Elapsed: 00:00:00.70
  12. [143]dave44@ORADB> 
  13. [143]dave44@ORADB> insert into temp values (sysdate-10);
  14.  
  15. 1 row created.
  16.  
  17. Elapsed: 00:00:00.53
  18. [143]dave44@ORADB> 
  19. [143]dave44@ORADB> commit;
  20.  
  21. Commit complete.
  22.  
  23. Elapsed: 00:00:00.57
  24. [143]dave44@ORADB> 
  25. [143]dave44@ORADB> select max(dat) from temp;
  26.  
  27. MAX(DAT)
  28. ---------
  29. 08-MAR-07
  30.  
  31. Elapsed: 00:00:00.92
  32. [143]dave44@ORADB> 
  33. [143]dave44@ORADB> select max(to_char(dat,'dd/mm/yyyy'))
  34.   2  from temp;
  35.  
  36. MAX(TO_CHA
  37. ----------
  38. 26/02/2007
  39.  
  40. Elapsed: 00:00:00.65
  41. [143]dave44@ORADB> select max(to_char(dat,'mm/dd/yyyy'))
  42.   2  from temp;
  43.  
  44. MAX(TO_CHA
  45. ----------
  46. 03/08/2007
  47.  

Yeah, that was evident & that was why I used the to_date function.
Mar 9 '07 #13

100+
P: 153
so do you understand why there is differences in the results?
Mar 10 '07 #14

P: 33
Hi Dave,
Yesterday night found a document & that ended all confusions. There it was clearly stated that in a decode statement, if the first return type is null, then the second return type is converted to datatype varchar2. That's why the error was occuring. In CASE-WHEN, this does not happen. Regarding the CASE-WHEN, I did not find any documents, but I am sure it does no such conversion. I executed the query
select max(decode(voided_flag,'Y',null,decode(mtg_status_ hist.mtg_status,'WD',status_date)))
from bravura.mtg_status_hist
where mtg_sk_seq=706379
minus
select sysdate ferom dual
.... There was an Oracle error.

But, when I used the CASE-WHEN instead of decode, no errors were thrown. Now, everything seems to fit well & fine. And, nevertheless, THANKS A LOT for your valuable comments on the problem.


You can check the link for the document:
http://download-west.oracle.com/docs/cd/B10501_01/server.920/a96540.pdf
Mar 12 '07 #15

P: 1
Hi Friend,
Please send the table structure and fields presented in that table and send the table name and send what is the problem you are facing?

[Thank You,
Srikanth Reddy.P

Hi,
I am facing a strange problem with decode function in oracle. My table name is status_hist. Below is the query I am hitting on this table:

select max(decode(voided_flag,'Y',null,decode(status_hist .status,'WD',status_date)))
from status_hist
where sk_seq=6574

The result returned is '29-SEP-05'

However, I checked out the table status_hist to see that for sk_seq=6574 and voided_flag not equals to 'Y' and status='WD', the maximum status_date is '12/22/2005 10:28:29 AM' . However, if I use the CASE-WHEN function, then I get accurate results.

Also, when I hit the query

select max(to_date(decode(voided_flag,'Y',null,decode(sta tus_hist.status,'WD',status_date)),'dd-mon-yy'))
from status_hist
where sk_seq=6574

the result is '12/22/2005'

Can anyone please give an explanation to this?
Jun 11 '07 #16

P: 33
Hi Friend,
Please send the table structure and fields presented in that table and send the table name and send what is the problem you are facing?

[Thank You,
Srikanth Reddy.P
Hi, I have solved the problem and also mentioned it in the post. You can follow the chain for that purpose. Actually, the problem occured, as for a decode function, if the first return type is null, then the second result type is implicitly converted to varchar2 type. So, I HAD TO USE THE TO_DATE FUNCTION IN PLACE
Jun 11 '07 #17

Post your reply

Sign in to post your reply or Sign up for a free account.