Home » SQL & PL/SQL » SQL & PL/SQL » problem with case statement (Oracle 10g)
problem with case statement [message #626100] Mon, 20 October 2014 13:49 Go to next message
bobghw
Messages: 34
Registered: July 2012
Member
Hi
query needs to extract data for the previous day except on Mondays when it needs to pull Friday, Saturday and Sunday data.
Using a case statement for criteria and it pulls the previous days data but not the weekend data?

SELECT Ip.Adm_Date + Ip.Adm_Time / 86400 AS Admdatetime  
     , Ip.Pt_Code  
     , Cpi.Surname  
     , Cpi.First_Given_Name  
     , Ip.W_Code
     , ''as "Reviewed/No MR completed   "
     , '' AS "MR < 24"  
     , '' AS "MR 24-36"  
     , '' AS "MR 36-48"  
     , '' AS "MR 48-60"  
     , '' AS "MR 60-72"  
     , '' AS "MR>72"  
     , '' AS "MR Finalised"
     , '' AS "MR on Dx"  
     , '' AS "Pharm/Tech"  
     , '' AS "Met MR Criteria" 
     , '' as "Comments" 
  FROM K_Ipreg Ip, K_Cpireg Cpi 
 WHERE Ip.Pt_Code = Cpi.Pt_Code  
   AND Ip.Disch_Date IS NULL  
   AND Ip.Adm_Date BETWEEN ( ( CASE  
                                 WHEN TO_CHAR ( SYSDATE, 'd' ) = 1  
                                 THEN SYSDATE - 4  
                                 ELSE SYSDATE - 2  
                              END ) )  
                       AND SYSDATE  
   AND W_Code NOT IN ('ICW', 'NWB', 'ANTE', 'BABY', 'CHILD', 'DELI', 'ED', 'MATY', 'SCBU')
Re: problem with case statement [message #626102 is a reply to message #626100] Mon, 20 October 2014 14:43 Go to previous messageGo to next message
Michel Cadot
Messages: 68647
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

'D' format mask depends on your terrory, so you can't rely on it.

SQL> alter session set NLS_TERRITORY=AMERICA;

Session altered.

SQL> select to_char(sysdate,'D') d from dual;
D
-
2

1 row selected.

SQL> alter session set NLS_TERRITORY=FRANCE;

Session altered.

SQL> select to_char(sysdate,'D') d from dual;
D
-
1

1 row selected.

Re: problem with case statement [message #626107 is a reply to message #626102] Mon, 20 October 2014 21:26 Go to previous messageGo to next message
bobghw
Messages: 34
Registered: July 2012
Member
Hi Thanks for the reply TO_CHAR ( SYSDATE, 'd' ) returns 1 for Monday, 2 for Tuesday etc..
sysdate - 4 returns the date for 4 days ago so when the case statement executes on a Monday the TO_CHAR ( SYSDATE, 'd' ) returns 1
then it should return data from the last 3 days plus the day it executes?

Have modified the case statement so will see what happens.
  CASE  WHEN TO_CHAR ( SYSDATE, 'd' ) = 1 THEN SYSDATE - 4  ELSE SYSDATE - 2  END and SYSDATE 

Re: problem with case statement [message #626111 is a reply to message #626107] Tue, 21 October 2014 00:11 Go to previous messageGo to next message
Michel Cadot
Messages: 68647
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Quote:
TO_CHAR ( SYSDATE, 'd' ) returns 1 for Monday, 2 for Tuesday etc..


No, this is what I showed, it depends on your territory.

Re: problem with case statement [message #626133 is a reply to message #626100] Tue, 21 October 2014 03:50 Go to previous messageGo to next message
sandeep_orafaq
Messages: 88
Registered: September 2014
Member
As Michael suggested, it can be NLS_TERRITORY Setting for your session. Are you executing the query in the same session where you are executing
<Quote>
TO_CHAR ( SYSDATE, 'd' ) returns 1 for Monday, 2 for Tuesday etc..
</Quote>

Also check for other conditions you have kept in query. Is the data according to your conditions?
Re: problem with case statement [message #626148 is a reply to message #626107] Tue, 21 October 2014 08:27 Go to previous messageGo to next message
joy_division
Messages: 4963
Registered: February 2005
Location: East Coast USA
Senior Member
bobghw wrote on Mon, 20 October 2014 22:26
Hi Thanks for the reply TO_CHAR ( SYSDATE, 'd' ) returns 1 for Monday, 2 for Tuesday etc..


Actually, it returns a '1' or a '2,' not a 1 or a 2. TO_CHAR return a string, not a number.
Re: problem with case statement [message #626153 is a reply to message #626148] Tue, 21 October 2014 12:21 Go to previous messageGo to next message
Bill B
Messages: 1971
Registered: December 2004
Senior Member
also if adm_date is trunc'ed (time removed) then you can use the following

 AND Ip.Adm_Date BETWEEN 
     decode(TO_CHAR ( SYSDATE, 'DY' ),'MON',TRUNC(SYSDATE - 4),TRUNC(SYSDATE - 2)) 
     AND TRUNC(SYSDATE)
Re: problem with case statement [message #626157 is a reply to message #626111] Tue, 21 October 2014 14:05 Go to previous messageGo to next message
bobghw
Messages: 34
Registered: July 2012
Member
Hi Michel

NLS_Territory is New Zealand and
SELECT TO_CHAR ( SYSDATE, 'd' ) from dual

returns 3 for today(Wednesday)
Re: problem with case statement [message #626159 is a reply to message #626157] Tue, 21 October 2014 14:20 Go to previous messageGo to next message
Michel Cadot
Messages: 68647
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

But are you sure that all your clients forever will be in New Zealand?
No, then switch to something like Bill's query, you can then force the language you want to use in the query:
SQL> select to_char(sysdate,'Dy','NLS_DATE_LANGUAGE=ENGLISH') en,
  2         to_char(sysdate,'Dy','NLS_DATE_LANGUAGE=FRENCH') fr,
  3         to_char(sysdate,'Dy','NLS_DATE_LANGUAGE=GERMAN') ge
  4  from dual
  5  /
EN  FR   GE
--- ---- --
Tue Mar. Di

Then you are not dependent on the client settings.

Re: problem with case statement [message #626160 is a reply to message #626153] Tue, 21 October 2014 14:22 Go to previous messageGo to next message
bobghw
Messages: 34
Registered: July 2012
Member
Hi Bill

will give your suggestion a try.
Re: problem with case statement [message #626162 is a reply to message #626159] Tue, 21 October 2014 14:23 Go to previous messageGo to next message
bobghw
Messages: 34
Registered: July 2012
Member
Thanks Michel, but yes I can guarantee all my clients will be in New Zealand. Smile
Re: problem with case statement [message #626163 is a reply to message #626162] Tue, 21 October 2014 14:24 Go to previous messageGo to next message
Michel Cadot
Messages: 68647
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

For ever? I doubt you can say that. Smile

Re: problem with case statement [message #626164 is a reply to message #626163] Tue, 21 October 2014 14:26 Go to previous messageGo to next message
bobghw
Messages: 34
Registered: July 2012
Member
Provincial hospital in NZ Smile
Re: problem with case statement [message #626166 is a reply to message #626164] Tue, 21 October 2014 14:35 Go to previous messageGo to next message
Michel Cadot
Messages: 68647
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Yes, but maybe some day some one will interested by your product and will want to use it in his country.
Or maybe your hospital will be associated with an (or several) other one(s) in an other country in the same computer department or to use the same products in order to decrease the expense for the computer department
There are so may reasons a product won't stay where it was developed, it is a pity (even a shame) it has not been thought to be independent of the country, language or date format.

[Updated on: Tue, 21 October 2014 14:36]

Report message to a moderator

Re: problem with case statement [message #626167 is a reply to message #626166] Tue, 21 October 2014 14:37 Go to previous messageGo to next message
bobghw
Messages: 34
Registered: July 2012
Member
lol, ok you win but I won't be here by the time any of that happens Smile
Re: problem with case statement [message #626168 is a reply to message #626167] Tue, 21 October 2014 14:42 Go to previous message
Michel Cadot
Messages: 68647
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Laughing

Previous Topic: CREATE TABLE AS vs CREATE TABLE then INSERT
Next Topic: need sql query
Goto Forum:
  


Current Time: Fri Apr 26 20:00:45 CDT 2024