View Full Version : DECODE
Sherman H.
04-01-2004, 02:21 PM
I have to run a query to give a column a value based on a time range. Can I
use DECODE?
select decode(trans_date, trans_date>='01-Jul-2002' and
trans_date<='30-Jun-2003','Fiscal2002', .....) as fiscal,
from. . .
where. . .
Malcolm Dew-Jones
04-01-2004, 10:24 PM
Sherman H. (shung@earthlink.net) wrote:
: I have to run a query to give a column a value based on a time range. Can I
: use DECODE?
: select decode(trans_date, trans_date>='01-Jul-2002' and
: trans_date<='30-Jun-2003','Fiscal2002', .....) as fiscal,
: from. . .
: where. . .
I believe that the newer versions of oracle support more complex logic
than decode, i.e. case/choice expressions but I don't recall the names
involved. That would be my first choice.
You could also write a function and use that to return the value.
Or use decode. The usual trick is to subtract one number from another to
get either a positive, negative, or zero difference, and then use sign()
to force that to be -1, 0, or 1, which decode can handle.
With a date, I am thinking you could convert it into seconds, or perhaps
use numeric formats of the date that would work as numbers, i.e.
01-Jul-2002 becomes 2002 07 01 which becomes 20020701, which can be used
as a number and compared to other dates like
e.g. (not your logic at all, just example of what I mean)
decode( sign( 20020701 - 20030630 ) -- if 20020701 < 20030630
, -1 , 'Fiscal2002' -- then this
, 0 , 'something else -- elsif = then this (error?)
, 'not fiscal -- else this case
)
(The numbers would come from date conversion functions)
$0.02
Mark C. Stock
04-02-2004, 03:24 AM
"Malcolm Dew-Jones" <yf110@vtn1.victoria.tc.ca> wrote in message
news:406d072b@news.victoria.tc.ca...
| Sherman H. (shung@earthlink.net) wrote:
| : I have to run a query to give a column a value based on a time range.
Can I
| : use DECODE?
|
| : select decode(trans_date, trans_date>='01-Jul-2002' and
| : trans_date<='30-Jun-2003','Fiscal2002', .....) as fiscal,
| : from. . .
| : where. . .
|
|
| I believe that the newer versions of oracle support more complex logic
| than decode, i.e. case/choice expressions but I don't recall the names
| involved. That would be my first choice.
|
| You could also write a function and use that to return the value.
|
| Or use decode. The usual trick is to subtract one number from another to
| get either a positive, negative, or zero difference, and then use sign()
| to force that to be -1, 0, or 1, which decode can handle.
|
| With a date, I am thinking you could convert it into seconds, or perhaps
| use numeric formats of the date that would work as numbers, i.e.
| 01-Jul-2002 becomes 2002 07 01 which becomes 20020701, which can be used
| as a number and compared to other dates like
|
| e.g. (not your logic at all, just example of what I mean)
|
| decode( sign( 20020701 - 20030630 ) -- if 20020701 < 20030630
| , -1 , 'Fiscal2002' -- then this
| , 0 , 'something else -- elsif = then this (error?)
| , 'not fiscal -- else this case
|
| )
|
| (The numbers would come from date conversion functions)
|
| $0.02
the perils of multi-posting -- see the derivative thread over in
c.d.o.server for a solution using TRUNC or ROUND
;-{ mcs
MyLounge.com Site Map
Forum:
Cars,
Cell Phone,
Database,
Games,
Home Improvement,
IT,
Music,
School,
Sports,
Web Design,
Web Server,
Weight Loss
The MyLounge.com forum is intended for informational use only and should not
be relied upon and is not a substitute for any advice. The information contained
on MyLounge.com are opinions and suggestions of members and is not a representation
of the opinions of MyLounge.com. MyLounge.com does not warrant or vouch for
the accuracy, completeness or usefulness of any postings or the qualifications
of any person responding. Please consult a expert or seek the services of an
attorney in your area for more accuracy on your specific situation. Please note
that our forums also serve as mirrors to Usenet newsgroups. Many posts you see
on our forums are made by newsgroup users who may not be members of MyLounge.com
Term of Service
vBulletin v3.0.7, Copyright ©2000-2009, Jelsoft Enterprises Ltd.