Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 empty fields in a record

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2001-07-10 : 08:05:32
Amanda writes "Hi,

I have a phonebook database that has a page to allow users to search on 4 fields, if one of the fields are empty in the database the search will not be succesful. The phonebook is for a hospital so we have a lot of phone numbers that arent attached to people etc.

the script

Surname= Request.Form("Surname")
Forename= Request.Form("Forename")
JobTitle= Request.Form("JobTitle")
Dept= Request.Form("Department")

Set DBConnection = Server.CreateObject("ADODB.Connection")
DBConnection.Open "PHONE"

SQLQ = ""
SQLQ = SQLQ & "SELECT * FROM PHONE, REF_DEPTS WHERE "
If Surname <> "" then
SQLQ = SQLQ & "(SURNAME like '" & Surname & "') AND "
else
SQLQ = SQLQ & "(SURNAME like '%') AND "
end if

If Forename <> "" then
SQLQ = SQLQ & "(FORENAME like '" & Forename & "') AND "
else
SQLQ = SQLQ & "(FORENAME like '%') AND "
end if

SQLQ = SQLQ & "NOT(FORENAME like '' AND TITLE like '') "

If JobTitle <> "" then
SQLQ = SQLQ & "AND (JOB_DESCRIPTION like '" & JobTitle & "') "
else
SQLQ = SQLQ & "AND (JOB_DESCRIPTION like '%') "
end if


SQLQ = SQLQ & "AND (REF_DEPTS.REF_DEPT_CODE = PHONE.DEPARTMENT) "

If Dept <> "" then
SQLQ = SQLQ & "AND (DEPARTMENT like '" & Dept & "') "
else
SQLQ = SQLQ & "AND (DEPARTMENT like '%') "
end if

SQLQ = SQLQ & "ORDER BY SURNAME"
Set DBData = DBConnection.Execute(SQLQ)

SQLGetDepts = "SELECT * FROM REF_DEPTS ORDER BY REF_DEPT_DESC"
Set DeptsData = DBConnection.Execute(SQLGetDepts)



"
   

- Advertisement -