RE: SQL and case structure
Thanks to everyone who answered this question. The paper noted below is excellent! -Original Message- From: Robson, Peter [mailto:[EMAIL PROTECTED]] Sent: Tuesday, October 08, 2002 10:39 AM To: Multiple recipients of list ORACLE-L Subject:RE: SQL and case structure Doug Burns wrote a very informative paper on Decode, presented at an earlier UK-OUG meeting. Its available from his web site: http://doug.burns.tripod.com/decode.html And the answer to the question posed below is 'yes'. peter edinburgh -Original Message- From: Droogendyk, Harry [mailto:[EMAIL PROTECTED]] Sent: 07 October 2002 23:59 To: Multiple recipients of list ORACLE-L Subject: RE: SQL and case structure Jesse: Can I also use the value of another column rather than a literal like my example coded? e.g. select acct_no, decode(substr(acct_no,16,1),'1',field1 '2',field2 field3 ) as descr from star.kills ; TIA -Original Message- Sent: Monday, October 07, 2002 5:28 PM To: Multiple recipients of list ORACLE-L CASE in PL/SQL serves a slightly different function, but DECODE should do the trick. Try: select acct_no, DECODE(substr(acct_no,16,1), '1','one', '2','two', 'other') as DESCR from star.kills; I also changed desc to descr, since desc is a reserved word. HTH! GL! :) Rich Jesse System/Database Administrator [EMAIL PROTECTED] Quad/Tech International, Sussex, WI USA -Original Message- From: Droogendyk, Harry [mailto:[EMAIL PROTECTED]] Sent: Monday, October 07, 2002 3:39 PM To: Multiple recipients of list ORACLE-L Subject: SQL and case structure Listers: I've used SAS's version of SQL and it allows the coding of conditional logic in the SELECT statement: proc sql; select acct_no, case substr(acct_no,16,1) when '1' then 'one' when '2' then 'two' else 'other' end as desc from star.kills; quit; The same syntax does not work in SQL*Plus for Oracle 8. Can someone point me to the correct syntax? Secondly, any URLs for this kind of information would be most appreciated. Regards, Harry -- Please see the official ORACLE-L FAQ: http://www.orafaq.com -- Author: Jesse, Rich INET: [EMAIL PROTECTED] Fat City Network Services-- 858-538-5051 http://www.fatcity.com San Diego, California-- Mailing list and web hosting services - To REMOVE yourself from this mailing list, send an E-Mail message to: [EMAIL PROTECTED] (note EXACT spelling of 'ListGuru') and in the message BODY, include a line containing: UNSUB ORACLE-L (or the name of mailing list you want to be removed from). You may also send the HELP command for other information (like subscribing). -- Please see the official ORACLE-L FAQ: http://www.orafaq.com -- Author: Droogendyk, Harry INET: [EMAIL PROTECTED
RE: SQL and case structure
Doug Burns wrote a very informative paper on Decode, presented at an earlier UK-OUG meeting. Its available from his web site: http://doug.burns.tripod.com/decode.html And the answer to the question posed below is 'yes'. peter edinburgh -Original Message- From: Droogendyk, Harry [mailto:[EMAIL PROTECTED]] Sent: 07 October 2002 23:59 To: Multiple recipients of list ORACLE-L Subject: RE: SQL and case structure Jesse: Can I also use the value of another column rather than a literal like my example coded? e.g. select acct_no, decode(substr(acct_no,16,1),'1',field1 '2',field2 field3 ) as descr from star.kills ; TIA -Original Message- Sent: Monday, October 07, 2002 5:28 PM To: Multiple recipients of list ORACLE-L CASE in PL/SQL serves a slightly different function, but DECODE should do the trick. Try: select acct_no, DECODE(substr(acct_no,16,1), '1','one', '2','two', 'other') as DESCR from star.kills; I also changed desc to descr, since desc is a reserved word. HTH! GL! :) Rich Jesse System/Database Administrator [EMAIL PROTECTED] Quad/Tech International, Sussex, WI USA -Original Message- From: Droogendyk, Harry [mailto:[EMAIL PROTECTED]] Sent: Monday, October 07, 2002 3:39 PM To: Multiple recipients of list ORACLE-L Subject: SQL and case structure Listers: I've used SAS's version of SQL and it allows the coding of conditional logic in the SELECT statement: proc sql; select acct_no, case substr(acct_no,16,1) when '1' then 'one' when '2' then 'two' else 'other' end as desc from star.kills; quit; The same syntax does not work in SQL*Plus for Oracle 8. Can someone point me to the correct syntax? Secondly, any URLs for this kind of information would be most appreciated. Regards, Harry -- Please see the official ORACLE-L FAQ: http://www.orafaq.com -- Author: Jesse, Rich INET: [EMAIL PROTECTED] Fat City Network Services-- 858-538-5051 http://www.fatcity.com San Diego, California-- Mailing list and web hosting services - To REMOVE yourself from this mailing list, send an E-Mail message to: [EMAIL PROTECTED] (note EXACT spelling of 'ListGuru') and in the message BODY, include a line containing: UNSUB ORACLE-L (or the name of mailing list you want to be removed from). You may also send the HELP command for other information (like subscribing). -- Please see the official ORACLE-L FAQ: http://www.orafaq.com -- Author: Droogendyk, Harry INET: [EMAIL PROTECTED] Fat City Network Services-- 858-538-5051 http://www.fatcity.com San Diego, California-- Mailing list and web hosting services - To REMOVE yourself from this mailing list, send an E-Mail message to: [EMAIL PROTECTED] (note EXACT spelling of 'ListGuru') and in the message BODY, include a line containing: UNSUB ORACLE-L (or the name of mailing list you want to be removed from). You may also send the HELP command for other information (like subscribing). * This e-mail message, and any files transmitted with it, are confidential and intended solely for the use of the addressee. If this message was not addressed to you, you have received it in error and any copying, distribution or other use of any part of it is strictly prohibited. Any views or opinions presented are solely those of the sender and do not necessarily represent those of the British Geological Survey. The security of e-mail communication cannot be guaranteed and the BGS accepts no liability for claims arising as a result of the use of this medium to transmit messages from or to the BGS. The BGS cannot accept any responsibility for viruses, so please scan all attachments.http://www.bgs.ac.uk * -- Please see the official ORACLE-L FAQ: http://www.orafaq.com -- Author: Robson, Peter INET: [EMAIL PROTECTED] Fat City Network Services-- 858-538-5051 http://www.fatcity.com San Diego, California-- Mailing list and web hosting services - To REMOVE yourself from this mailing list, send an E-Mail message to: [EMAIL PROTECTED] (note EXACT spelling of 'ListGuru') and in the message BODY, include a line containing: UNSUB ORACLE-L (or the name of mailing list you want to be removed from). You may also send the HELP command for other information (like subscribing).
RE: SQL and case structure
CASE in PL/SQL serves a slightly different function, but DECODE should do the trick. Try: select acct_no, DECODE(substr(acct_no,16,1), '1','one', '2','two', 'other') as DESCR from star.kills; I also changed desc to descr, since desc is a reserved word. HTH! GL! :) Rich Jesse System/Database Administrator [EMAIL PROTECTED] Quad/Tech International, Sussex, WI USA -Original Message- From: Droogendyk, Harry [mailto:[EMAIL PROTECTED]] Sent: Monday, October 07, 2002 3:39 PM To: Multiple recipients of list ORACLE-L Subject: SQL and case structure Listers: I've used SAS's version of SQL and it allows the coding of conditional logic in the SELECT statement: proc sql; select acct_no, case substr(acct_no,16,1) when '1' then 'one' when '2' then 'two' else 'other' end as desc from star.kills; quit; The same syntax does not work in SQL*Plus for Oracle 8. Can someone point me to the correct syntax? Secondly, any URLs for this kind of information would be most appreciated. Regards, Harry -- Please see the official ORACLE-L FAQ: http://www.orafaq.com -- Author: Jesse, Rich INET: [EMAIL PROTECTED] Fat City Network Services-- 858-538-5051 http://www.fatcity.com San Diego, California-- Mailing list and web hosting services - To REMOVE yourself from this mailing list, send an E-Mail message to: [EMAIL PROTECTED] (note EXACT spelling of 'ListGuru') and in the message BODY, include a line containing: UNSUB ORACLE-L (or the name of mailing list you want to be removed from). You may also send the HELP command for other information (like subscribing).
RE: SQL and case structure
Look up the DECODE function select acct_no, decode(substr(acct_no,16,1),'1','one','2','two','other') from star.kills; -Original Message- Sent: Monday, October 07, 2002 3:39 PM To: Multiple recipients of list ORACLE-L Listers: I've used SAS's version of SQL and it allows the coding of conditional logic in the SELECT statement: proc sql; select acct_no, case substr(acct_no,16,1) when '1' then 'one' when '2' then 'two' else 'other' end as desc from star.kills; quit; The same syntax does not work in SQL*Plus for Oracle 8. Can someone point me to the correct syntax? Secondly, any URLs for this kind of information would be most appreciated. Regards, Harry -- Please see the official ORACLE-L FAQ: http://www.orafaq.com -- Author: Droogendyk, Harry INET: [EMAIL PROTECTED] Fat City Network Services-- 858-538-5051 http://www.fatcity.com San Diego, California-- Mailing list and web hosting services - To REMOVE yourself from this mailing list, send an E-Mail message to: [EMAIL PROTECTED] (note EXACT spelling of 'ListGuru') and in the message BODY, include a line containing: UNSUB ORACLE-L (or the name of mailing list you want to be removed from). You may also send the HELP command for other information (like subscribing). -- Please see the official ORACLE-L FAQ: http://www.orafaq.com -- Author: Kevin Lange INET: [EMAIL PROTECTED] Fat City Network Services-- 858-538-5051 http://www.fatcity.com San Diego, California-- Mailing list and web hosting services - To REMOVE yourself from this mailing list, send an E-Mail message to: [EMAIL PROTECTED] (note EXACT spelling of 'ListGuru') and in the message BODY, include a line containing: UNSUB ORACLE-L (or the name of mailing list you want to be removed from). You may also send the HELP command for other information (like subscribing).
RE: SQL and case structure
Title: RE: SQL and case structure -Original Message- From: Droogendyk, Harry [mailto:[EMAIL PROTECTED]] I've used SAS's version of SQL and it allows the coding of conditional logic in the SELECT statement: proc sql; select acct_no, case substr(acct_no,16,1) when '1' then 'one' when '2' then 'two' else 'other' end as desc from star.kills; quit; The same syntax does not work in SQL*Plus for Oracle 8. Can someone point me to the correct syntax? Secondly, any URLs for this kind of information would be most appreciated. The case syntax was introduced in Oracle 8.1. Here's a link to Oracle 8.0 documentation http://download-west.oracle.com/docs/cd/A64702_01/doc/index.htm (read the SQL Reference manual) CASE expressions are described in the Oracle 8.1 Data Warehousing guide http://download-west.oracle.com/docs/cd/A87860_01/doc/index.htm Click on Oracle8i Server and SQL*Plus and then on Data Warehousing guide: you will eventually find this: http://download-west.oracle.com/docs/cd/A87860_01/doc/server.817/a76994/analysis.htm#18058 To do something similar to a CASE in Oracle 8.0 SQL, use the decode function. Example: if column = 1 return 10 if column = 2 return 20 if column = 3 return 40 otherwise return the value of the column select decode (col, -- expression 1, 10, -- change 1 to 10 2, 20, -- change 2 to 20 3, 40, -- change 3 to 40 col -- default is the original value ) from table ; or select decode (col, 1, 10, 2, 20, 3, 40, col) from table ;
RE: SQL and case structure
Jesse: Can I also use the value of another column rather than a literal like my example coded? e.g. select acct_no, decode(substr(acct_no,16,1),'1',field1 '2',field2 field3 ) as descr from star.kills ; TIA -Original Message- Sent: Monday, October 07, 2002 5:28 PM To: Multiple recipients of list ORACLE-L CASE in PL/SQL serves a slightly different function, but DECODE should do the trick. Try: select acct_no, DECODE(substr(acct_no,16,1), '1','one', '2','two', 'other') as DESCR from star.kills; I also changed desc to descr, since desc is a reserved word. HTH! GL! :) Rich Jesse System/Database Administrator [EMAIL PROTECTED] Quad/Tech International, Sussex, WI USA -Original Message- From: Droogendyk, Harry [mailto:[EMAIL PROTECTED]] Sent: Monday, October 07, 2002 3:39 PM To: Multiple recipients of list ORACLE-L Subject: SQL and case structure Listers: I've used SAS's version of SQL and it allows the coding of conditional logic in the SELECT statement: proc sql; select acct_no, case substr(acct_no,16,1) when '1' then 'one' when '2' then 'two' else 'other' end as desc from star.kills; quit; The same syntax does not work in SQL*Plus for Oracle 8. Can someone point me to the correct syntax? Secondly, any URLs for this kind of information would be most appreciated. Regards, Harry -- Please see the official ORACLE-L FAQ: http://www.orafaq.com -- Author: Jesse, Rich INET: [EMAIL PROTECTED] Fat City Network Services-- 858-538-5051 http://www.fatcity.com San Diego, California-- Mailing list and web hosting services - To REMOVE yourself from this mailing list, send an E-Mail message to: [EMAIL PROTECTED] (note EXACT spelling of 'ListGuru') and in the message BODY, include a line containing: UNSUB ORACLE-L (or the name of mailing list you want to be removed from). You may also send the HELP command for other information (like subscribing). -- Please see the official ORACLE-L FAQ: http://www.orafaq.com -- Author: Droogendyk, Harry INET: [EMAIL PROTECTED] Fat City Network Services-- 858-538-5051 http://www.fatcity.com San Diego, California-- Mailing list and web hosting services - To REMOVE yourself from this mailing list, send an E-Mail message to: [EMAIL PROTECTED] (note EXACT spelling of 'ListGuru') and in the message BODY, include a line containing: UNSUB ORACLE-L (or the name of mailing list you want to be removed from). You may also send the HELP command for other information (like subscribing).
RE: SQL and case structure
Look up the DECODE command. SELECT decode(substr(acct_no,16,1),'1',one,'2','two','other') FROM should look like that Alec Macdonell -Original Message- Harry Sent: Monday, October 07, 2002 1:39 PM To: Multiple recipients of list ORACLE-L Listers: I've used SAS's version of SQL and it allows the coding of conditional logic in the SELECT statement: proc sql; select acct_no, case substr(acct_no,16,1) when '1' then 'one' when '2' then 'two' else 'other' end as desc from star.kills; quit; The same syntax does not work in SQL*Plus for Oracle 8. Can someone point me to the correct syntax? Secondly, any URLs for this kind of information would be most appreciated. Regards, Harry -- Please see the official ORACLE-L FAQ: http://www.orafaq.com -- Author: Droogendyk, Harry INET: [EMAIL PROTECTED] Fat City Network Services-- 858-538-5051 http://www.fatcity.com San Diego, California-- Mailing list and web hosting services - To REMOVE yourself from this mailing list, send an E-Mail message to: [EMAIL PROTECTED] (note EXACT spelling of 'ListGuru') and in the message BODY, include a line containing: UNSUB ORACLE-L (or the name of mailing list you want to be removed from). You may also send the HELP command for other information (like subscribing). -- Please see the official ORACLE-L FAQ: http://www.orafaq.com -- Author: Alec Macdonell INET: [EMAIL PROTECTED] Fat City Network Services-- 858-538-5051 http://www.fatcity.com San Diego, California-- Mailing list and web hosting services - To REMOVE yourself from this mailing list, send an E-Mail message to: [EMAIL PROTECTED] (note EXACT spelling of 'ListGuru') and in the message BODY, include a line containing: UNSUB ORACLE-L (or the name of mailing list you want to be removed from). You may also send the HELP command for other information (like subscribing).