Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

CASE clause in the WHERE section 1

Status
Not open for further replies.

dancarr22

Programmer
Oct 15, 2002
8
0
0
CA
Anyone know how to put a CASE or IF statement in a where clause...this didn't work
SELECT NAME FROM CUSTOMERS WHERE
CASE WHEN SYSDATE=SYSDATE THEN
NAME LIKE 'a%'
ELSE
NAME LIKE 'b%'
END
 
What are you trying to do?

Why SYSDATE=SYSDATE ?

A case expression returns a value, it can not be used as a macro.
 
That was just a test to see if the logic works.
I want to change the WHERE clause depending on
a given value. For example if today is a Monday
I want to pull up all the 'Monday' clients
so WHERE Today='Monday' if today is Tuesday
I want WHERE Today='Tuesday'. It may sound
a bit screwy but it's what we need because
we can't save functions, etc. to the database
and need to do everything in in-line SQL
 
To continue your example, it could look like

SELECT NAME
FROM CUSTOMERS
WHERE name like
CASE WHEN SYSDATE=SYSDATE THEN
'a%'
ELSE
'b%'
END

Do you really need a case expression in your case, e.g. something like

select * from customer where lower(today) = lower(to_char(to_date(sysdate,'DAY'))

With reservation for total misunderstanding and forgotten Oracle skills.
 
You can use a DECODE() statement thus:

Code:
SELECT NAME 
FROM CUSTOMERS 
WHERE name LIKE DECODE(SYSDATE,SYSDATE,'a%','b%')

Whether it makes sense to do so is another matter...
-- Chris Hunt
Extra Connections Ltd
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top