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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Query problem (probably easy)

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
This is probably obvious to everyone except me...

My telephone directory is a single table. The search query is as follows:

select LAST_NAME, TEL_NUM, DEPT, SECTION
from ALL_R
where ((upper(LAST_NAME) LIKE upper:)Last_Name)||'%')
AND ((upper(DEPT) like '%'||upper:)keyword)||'%')
OR (upper(SECTION) like '%'||upper:)keyword)||'%')
AND(TEL_NUM LIKE '%'||:)Tel_Num)||'%'))
order by LAST_NAME ASC

This works fine for seaches on name, keyword or number in that it returns all the people required. What my query doesn't do is return rows which have the name missing e.g. dept fax numbers. However I change it I have problems - HELP PLEASE.

Badger, UK
 
Whereas you have lots of parens, they aren't also used to organize your query. Point is this,

SELECT a AND b OR c

can mean that you want [a AND b] OR c
or it can mean you want a AND [b or c]

Try using parenthesis to make it more explicit, as:

SELECT (a AND b) OR c
FROM table
WHERE a LIKE '%keyword%' John Hoarty
jhoarty@quickestore.com
 
I think you will have to add a test for nulls on each column that may have nulls. Try something like

select LAST_NAME, TEL_NUM, DEPT, SECTION
from ALL_R
where ((upper(LAST_NAME) LIKE upper:)Last_Name)||'%'
or last_name is null)
AND ((upper(DEPT) like '%'||upper:)keyword)||'%'
or dept is null)
OR (upper(SECTION) like '%'||upper:)keyword)||'%'
or section is null)
AND(TEL_NUM LIKE '%'||:)Tel_Num)||'%')
or tel_num is null)
order by LAST_NAME ASC
 
Along the same lines as Karluk:

select LAST_NAME, TEL_NUM, DEPT, SECTION
from ALL_R
where ((upper(NVL(LAST_NAME,:Last_Name)) LIKE upper:)Last_Name)||'%')
AND ((upper(NVL(DEPT,:keyword)) like '%'||upper:)keyword)||'%')
OR (upper(NVL(SECTION,:keyword)) like '%'||upper:)keyword)||'%')
AND(NVL(TEL_NUM,:Tel_Num) LIKE '%'||:)Tel_Num)||'%'))
order by LAST_NAME ASC
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top