SQL OR

Hi Experts,

I have the below query, in which I would like to exclude ALL bookings that meet ANY of the criteria in my WHERE clause.  So, exclude any record that has a null booking date, or null booking time, or null trip type.  All fields do not need to be null to exclude that row.  I need to exclude rows if ANY is null, that's why I'm using OR instead of AND... However, by my jpeg, you'll see that I'm still getting nulls.  Why is this?

SELECT fldReservID, fldUserInit, fldUser, fldTripType, fldPickupStat, fldAssocResID,
	   fldTripSubType, fldArchived, 
	
	/*DATE Format*/
	   CONVERT(VARCHAR(10), CAST([fldBookDate] AS DATE),120) AS [Booking_Date], 
	/*TIME Format*/
	   CONVERT(VARCHAR(10), CAST([fldBookTime] AS TIME),114) AS [Booking_Time], 
	
	   fldAgent, fldBookLoc, fldPickupLoc,
	
	/*DATE Format*/
	   CONVERT(VARCHAR(10), CAST([fldPickupDate] AS DATE),120) AS [Pickup_Date], 
	/*TIME Format*/
	   CONVERT(VARCHAR(10), CAST([fldPickupTime] AS TIME),114) AS [Pickup_Time],
	   
	   fldDriver, fldVehicle, fldLName, fldRoomNo, fldReservComment,
	   
	/*QUANTITIES*/
	  	 fldFamQty,		  fldAdultQty,   fldSenQty, fldStudQty, fldChildQty, fldInfQty,
		((fldFamQty * 4) + fldAdultQty + fldSenQty + fldStudQty + fldChildQty) AS [total_pax],

	/*RATES*/
	  	 fldFamRate, fldAdultRate, fldSenRate, fldStudRate, fldChildRate, fldInfRate,
	  	 
	/*COSTS*/
		(fldFamQty		* fldFamRate)	AS [fam_cost],
		(fldAdultQty	* fldAdultRate) AS [ad_cost],
		(fldSenQty		* fldSenRate)	AS [sn_cost],
		(fldStudQty		* fldStudRate)	AS [st_cost],
		(fldChildQty	* fldChildRate) AS [ch_cost],
		(fldInfQty		* fldInfRate)	AS [inf_cost],
		
		(
			(fldFamQty		* fldFamRate)	+
			(fldAdultQty	* fldAdultRate) +
			(fldSenQty		* fldSenRate)	+
			(fldStudQty		* fldStudRate)	+
			(fldChildQty	* fldChildRate) +
			(fldInfQty		* fldInfRate)	
		
		) AS [total_cost]
		
	
FROM tblReservations 
WHERE (fldTripType IS NOT NULL) OR (fldBookTime IS NOT NULL) OR (fldBookTime IS NOT NULL) 
	  OR (fldAgent IS NOT NULL)
ORDER BY fldReservID

Open in new window


ThanksResults
APD TorontoSoftware DeveloperAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Kent OlsenDBACommented:
You want to use AND in this filter.  And check to see that you're referencing the correct columns.  One appears twice.

WHERE (fldTripType IS NOT NULL) 
  AND (fldBookTime IS NOT NULL) 
  AND (fldBookTime IS NOT NULL) 
  AND (fldAgent IS NOT NULL)

Open in new window


Good Luck,
Kent
0
APD TorontoSoftware DeveloperAuthor Commented:
Wouldn't AND need to meet ALL? So, wouldn't AND exclude rows where ALL are null?
0
Kent OlsenDBACommented:
You want to exclude rows where any of the those columns are NULL, so you want to select the rows where all of the columns are not NULL.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Protecting & Securing Your Critical Data

Considering 93 percent of companies file for bankruptcy within 12 months of a disaster that blocked access to their data for 10 days or more, planning for the worst is just smart business. Learn how Acronis Backup integrates security at every stage

Mark ElySenior Coldfusion DeveloperCommented:
You have fldBookTime in the where clause twice....  Also no need for the () in the where clause as it is written.  

SELECT fldReservID, fldUserInit, fldUser, fldTripType, fldPickupStat, fldAssocResID,
	   fldTripSubType, fldArchived, 
	
	/*DATE Format*/
	   CONVERT(VARCHAR(10), CAST([fldBookDate] AS DATE),120) AS [Booking_Date], 
	/*TIME Format*/
	   CONVERT(VARCHAR(10), CAST([fldBookTime] AS TIME),114) AS [Booking_Time], 
	
	   fldAgent, fldBookLoc, fldPickupLoc,
	
	/*DATE Format*/
	   CONVERT(VARCHAR(10), CAST([fldPickupDate] AS DATE),120) AS [Pickup_Date], 
	/*TIME Format*/
	   CONVERT(VARCHAR(10), CAST([fldPickupTime] AS TIME),114) AS [Pickup_Time],
	   
	   fldDriver, fldVehicle, fldLName, fldRoomNo, fldReservComment,
	   
	/*QUANTITIES*/
	  	 fldFamQty,		  fldAdultQty,   fldSenQty, fldStudQty, fldChildQty, fldInfQty,
		((fldFamQty * 4) + fldAdultQty + fldSenQty + fldStudQty + fldChildQty) AS [total_pax],

	/*RATES*/
	  	 fldFamRate, fldAdultRate, fldSenRate, fldStudRate, fldChildRate, fldInfRate,
	  	 
	/*COSTS*/
		(fldFamQty		* fldFamRate)	AS [fam_cost],
		(fldAdultQty	* fldAdultRate) AS [ad_cost],
		(fldSenQty		* fldSenRate)	AS [sn_cost],
		(fldStudQty		* fldStudRate)	AS [st_cost],
		(fldChildQty	* fldChildRate) AS [ch_cost],
		(fldInfQty		* fldInfRate)	AS [inf_cost],
		
		(
			(fldFamQty		* fldFamRate)	+
			(fldAdultQty	* fldAdultRate) +
			(fldSenQty		* fldSenRate)	+
			(fldStudQty		* fldStudRate)	+
			(fldChildQty	* fldChildRate) +
			(fldInfQty		* fldInfRate)	
		
		) AS [total_cost]
		
	
FROM tblReservations 
WHERE fldTripType IS NOT NULL AND
	  fldBookTime IS NOT NULL AND
	  fldAgent IS NOT NULL
ORDER BY fldReservID

Open in new window

0
APD TorontoSoftware DeveloperAuthor Commented:
It has been a long day :)
0
Kent OlsenDBACommented:
Yeah.  It's usually not called "Friday" though.   :)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Query Syntax

From novice to tech pro — start learning today.