473,395 Members | 1,987 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Need help with SQL Statement checking for nulls

88
Hi,

I have a table with over 20 columns. Seven of these of these columns correspond to a filter on a search form. Based on what is selected on the search form, and what is in these seven fields in the table, it indicates whether or not I pull the record. Thing is....is that not all the records use the same combination of these seven fields. One might only use the first two and the other five will be null, or one might use the middle three and the other four will be null. However, if any of these fields match up to its corresponding filter on the form, even if the others are null (because if a field is NULL it applies to all), I need to pull it.

I started out trying to do this in VB code and executing a regular select statment for each combination, but I quickly realized that this is not good as it will mean a ton of code to check for each field combination that is possible.

So, I'm trying to put together a single SQL Statment that I will execute on the SQL Server that will check if each of these fields is null before trying to do a comparrison on it. I don't know if this can be done and I am very new to the syntax for SQL Server queries. Here is what I have so far just trying to do the check on one of the columns;

Expand|Select|Wrap|Line Numbers
  1. SELECT [RecordNumber]
  2.       ,[Lender]
  3.       ,[LoanName]
  4.       ,[Adjustment]
  5. FROM [Website].[dbo].[Adjustments]
  6. WHERE [Website].[dbo].[Adjustments].[Lender] = 'Test'
  7. AND [Website].[dbo].[Adjustments].[LoanName] = 'Testing'
  8. IF [Website].[dbo].[Adjustments].[Units] IS NOT NULL THEN
  9.     AND [Website].[dbo].[Adjustments].[Units] = 2
  10.  
If I try to run this I get a message saying;

incorrect synax near 'THEN'

So I remove that and get

incorrect syntax near '2'

Is this possible or am I way out in left field?
Jan 22 '08 #1
2 1418
ck9663
2,878 Expert 2GB
Hi,

I have a table with over 20 columns. Seven of these of these columns correspond to a filter on a search form. Based on what is selected on the search form, and what is in these seven fields in the table, it indicates whether or not I pull the record. Thing is....is that not all the records use the same combination of these seven fields. One might only use the first two and the other five will be null, or one might use the middle three and the other four will be null. However, if any of these fields match up to its corresponding filter on the form, even if the others are null (because if a field is NULL it applies to all), I need to pull it.

I started out trying to do this in VB code and executing a regular select statment for each combination, but I quickly realized that this is not good as it will mean a ton of code to check for each field combination that is possible.

So, I'm trying to put together a single SQL Statment that I will execute on the SQL Server that will check if each of these fields is null before trying to do a comparrison on it. I don't know if this can be done and I am very new to the syntax for SQL Server queries. Here is what I have so far just trying to do the check on one of the columns;

Expand|Select|Wrap|Line Numbers
  1. SELECT [RecordNumber]
  2.       ,[Lender]
  3.       ,[LoanName]
  4.       ,[Adjustment]
  5. FROM [Website].[dbo].[Adjustments]
  6. WHERE [Website].[dbo].[Adjustments].[Lender] = 'Test'
  7. AND [Website].[dbo].[Adjustments].[LoanName] = 'Testing'
  8. IF [Website].[dbo].[Adjustments].[Units] IS NOT NULL THEN
  9.     AND [Website].[dbo].[Adjustments].[Units] = 2
  10.  
If I try to run this I get a message saying;

incorrect synax near 'THEN'

So I remove that and get

incorrect syntax near '2'

Is this possible or am I way out in left field?

first your error. sql server IF condition does not have the THEN (read here.

about your search condition, can the operator OR handle it?

-CK
Jan 22 '08 #2
fperri
88
first your error. sql server IF condition does not have the THEN (read here.

about your search condition, can the operator OR handle it?

-CK
Never thought about OR, I'll give it a try.
Jan 22 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: rdshultz | last post by:
using access 2000. Have a form based on a couple of controls, it finds the last used Inquiry number in a table. So it finds the highest Inquiry number for the "Office" , "Company" and "YearStamp"...
8
by: Brian Basquille | last post by:
Hello all, Bit of a change of pace now. As opposed to the typical questions regarding my Air Hockey game, am also working on a Photo Album which uses an Access Database to store information...
10
by: roy.anderson | last post by:
Error is thus: "Cast from type 'DBNull' to type 'String' is not valid." Simple, right? Well, no (or at least not for me). Here's a function, followed by the calling code below: Function...
4
by: Tony WONG | last post by:
i use the below formula to add up records which is extracted from SQL by ASP. sumQS = cint(objRS1("Q1S")) + cint(objRS1("Q2S")) + .... but if cint(objRS1("Q1S")) is null, it gets error. i...
2
by: bbhushan44 | last post by:
In a DB2 server setup on Sun, the clients are using Developer 2000 as frontend on the ODBC connection. A particular report is taking too long to execute. The snapshot shows the following. The...
2
by: Bill | last post by:
I have a 200 record database that includes a date/time field, AnnivDate, for a wedding anniversary. AnnivDate has nulls and some incorrect year data. I have been creating the Access database...
8
by: markjerz | last post by:
Hi, I basically have two tables with the same structure. One is an archive of the other (backup). I want to essentially insert the data in to the other. I use: INSERT INTO table ( column,...
1
by: Dennis Hartmann | last post by:
ACC 97 - Report with multiple grouping levels. Report footers have text controls with controlsource: "=Avg()". The report's Recordsource was changed so field1 contains null (instead of zero) for no...
7
by: tshad | last post by:
I have a program in 2005 that is reading a text file removing text and then writing it back out again. It removes lines that start with PRINT. This program has worked fine for months. Now all...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.