473,807 Members | 2,877 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

33 New Member
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(void ed_flag,'Y',nul l,decode(status _hist.status,'W D',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(dec ode(voided_flag ,'Y',null,decod e(status_hist.s tatus,'WD',stat us_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
16 7290
Dave44
153 New Member
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_fi eld) 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
Medhatithi
33 New Member
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.sta tus='WD' then status_date else null end)end) from status_hist
where sk_seq=706379
Mar 9 '07 #12
Medhatithi
33 New Member
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_fi eld) 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
Dave44
153 New Member
so do you understand why there is differences in the results?
Mar 10 '07 #14
Medhatithi
33 New Member
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(void ed_flag,'Y',nul l,decode(mtg_st atus_hist.mtg_s tatus,'WD',stat us_date)))
from bravura.mtg_sta tus_hist
where mtg_sk_seq=7063 79
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
sreeforu
1 New Member
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(void ed_flag,'Y',nul l,decode(status _hist.status,'W D',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(dec ode(voided_flag ,'Y',null,decod e(status_hist.s tatus,'WD',stat us_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
Medhatithi
33 New Member
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

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

Similar topics

1
23284
by: Walt | last post by:
Hi, I want to write a query that returns Y or N depending on whether the current time is within a specified window i.e. EVENT_TABLE ---------- event_id number PK event_start date event_end date
2
14614
by: Mark Hoffman | last post by:
I'm a newbie at Oracle..Be gentle! I have a table that stores information (WMI data) about computers on our network. The table looks like: ComputerID ItemID Class Property Value
1
4264
by: etravels | last post by:
Hiyas I am currently working on Oracle Report which ties in with my a report in the ebusiness suite of Oracle telesales module. Basically, the address on statement reports have blank fields (null) where the customer does not have and address2, address 3, At present its set up to read customers ADDRESS1
10
111284
by: N | last post by:
What is the function in SQL that works like DECODE in Oracle?" Thanks, N
2
14957
by: Amin Schoeib | last post by:
Hi, Like I see there is no equivalent to the Oracle decode Function In Postgres.Is there maybe somebody who wrote decode as a Function? Schoeib 4Tek Gesellschaft für angewandte Informationstechnologien mbH Schoeib Amin Tel. +49 (0) 69 697688-132
1
2806
by: Deven Oza | last post by:
Hello everyone Please help me for the following query I want to solve the query Using the (DECODE) function, I want to write a program segment that will successfully decode all the CIS courses pertaining to Databases into a description. For example, (1)CIS 143 to be decoded to Introduction to SQL., (2)CIS 090 to be decoded to Introduction to VISUAL BASIC., (3)CIS 145 to be decoded to Introduction to ORACLE., (4)CIS 147 to be decoded...
2
10366
by: Mikeland | last post by:
I come from the oracle world and would use the Oracle 'DECODE' function extensively. Is there any equivalent in db2??? using db2 version 8.1 on win SELECT COUNT(*) INTO VAR_COUNT_STREET FROM TB_TOWN_STREET WHERE upper(trim(STREETNAME)) = upper(trim(STREET)) AND upper(trim(TOWN_NAME)) = upper(trim(TOWN))
11
13122
by: dan-x | last post by:
I am a novice to SQL Server, so this is probably a really easy problem to fix. I'm translating an Oracle query and need to change the 'decode' to something compatible. Everything I've read points me to using 'case' but no matter how I write it I can't get it to work and get a syntax error. Suggestions? select SYST CTR , isnull(substring(CD_A, 1, 3), ' ') RESCODE , DES DESCRIPTION , decode (substring(CD_A, 1, 3), CODE,PRICE,0)...
1
5490
ollyb303
by: ollyb303 | last post by:
Hello, I have been using the following expression in Access as part of a statement to query an Oracle database: (Sum(CASE WHEN STATS_DAILY_SA.LOGIN_TIME > (STATS_DAILY_SA.SCHEDULED_TIME - DECODE(STATS_DAILY_SA.EXCEPTION_TIME, Null, 0, STATS_DAILY_SA.EXCEPTION_TIME)) THEN 0 ELSE ((STATS_DAILY_SA.SCHEDULED_TIME - DECODE(STATS_DAILY_SA.EXCEPTION_TIME, Null, 0, STATS_DAILY_SA.EXCEPTION_TIME)) - STATS_DAILY_SA.LOGIN_TIME)...
0
9720
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9599
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10626
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10372
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10374
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10112
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7650
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5685
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3854
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.