471,084 Members | 1,046 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,084 software developers and data experts.

using case, Valid statement?

Is this a valid sql statement:

SELECT
case field1
when 'first' then 1
when 'second' then 1
else null
end
FROM mytable

Mike
Jul 19 '05 #1
3 4348
I can use decode like:

decode(
field1,'first',1,
decode(field1,'second',2,0
)
)

except that this has to be all nested and is very hard to work with. If
I have nine items it becomes very ugly.

Michael Hill wrote:

Is this a valid sql statement:

SELECT
case field1
when 'first' then 1
when 'second' then 1
else null
end
FROM mytable

Mike

Jul 19 '05 #2
I can use decode like:

decode(
field1,'first',1,
decode(field1,'second',2,0
)
)

except that this has to be all nested and is very hard to work with. If
I have nine items it becomes very ugly.

Michael Hill wrote:

Is this a valid sql statement:

SELECT
case field1
when 'first' then 1
when 'second' then 1
else null
end
FROM mytable

Mike

Jul 19 '05 #3
Michael Hill <hi****@ram.lmtas.lmco.com> wrote in message news:<40***************@ram.lmtas.lmco.com>...
I can use decode like:

decode(
field1,'first',1,
decode(field1,'second',2,0
)
)


You don't have to nest :

decode(field1,
'first',1,
'second',2,
'third',3,
null)

It can go up to 255 arguments, I think?

Is this a valid sql statement:

SELECT
case field1
when 'first' then 1
when 'second' then 1
else null
end
FROM mytable


In 9i it is valid syntax.
In 8i you can use :

case
when field1='first' then 1
when field1='second' then 2
else null
end

The good thing about this syntax is, that each when clause can have
widely different boolean expressions.

In short:

In 8i you use decode syntax if it's just a case of different values
for a column, and use case syntax for more complicated expressions.

In 9i you use the same constructs, but they added the simplified case
syntax (the one you ask if it's valid) as an alternative to decode.
/KiBeHa
Jul 19 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

21 posts views Thread by Andy | last post: by
5 posts views Thread by Colleyville Alan | last post: by
25 posts views Thread by CJM | last post: by
5 posts views Thread by Frederick Dean | last post: by
1 post views Thread by Thiero | last post: by
9 posts views Thread by Robbie Hatley | last post: by
3 posts views Thread by Michael Hill | last post: by

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.