Link to home
Start Free TrialLog in
Avatar of Larry Brister
Larry BristerFlag for United States of America

asked on

Complex SQL Server WHERE CLause

In my where statement below...
I need to include this logic...
Where @FIlter = 'BOOKING'
... add    "AND criminal.Ignore=0"

WHERE	ISNULL(i.isDeleted, 0) = ISNULL(@isDeleted, 0)
		AND s.SalesSiteID = ISNULL(@SalesSiteID, s.SalesSiteID)
		AND i.IndividualTypeID = 2
		AND (
				(i.LastName LIKE '%' + ISNULL(@SearchOther, i.LastName) + '%')
				OR (i.FirstName LIKE '%' + ISNULL(@SearchOther, i.FirstName) + '%')
				OR (i.EmployeeID LIKE '%' + ISNULL(@SearchOther, i.EmployeeID) + '%'
				OR ee.EmailAddress = @SearchOther)
			)
			AND criminal.jsondata LIKE '%' + ISNULL(@filter,criminal.jsondata) + '%'

Open in new window

Avatar of Jim Horn
Jim Horn
Flag of United States of America image

Ok.  You'll probably want to state your requirements in greater detail, but for the moment give this a whirl..
WHERE	(@FIlter = 'BOOKING' AND criminal.Ignore=0)    -- Looky here
		AND ISNULL(i.isDeleted, 0) = ISNULL(@isDeleted, 0)
		AND s.SalesSiteID = ISNULL(@SalesSiteID, s.SalesSiteID)
		AND i.IndividualTypeID = 2
		AND (
				(i.LastName LIKE '%' + ISNULL(@SearchOther, i.LastName) + '%')
				OR (i.FirstName LIKE '%' + ISNULL(@SearchOther, i.FirstName) + '%')
				OR (i.EmployeeID LIKE '%' + ISNULL(@SearchOther, i.EmployeeID) + '%'
				OR ee.EmailAddress = @SearchOther)
			)
			AND criminal.jsondata LIKE '%' + ISNULL(@filter,criminal.jsondata) + '%'

Open in new window

Is there any trick on your question?
If not then it should be simple as:
WHERE ISNULL(i.isDeleted, 0) = ISNULL(@isDeleted, 0)
	AND s.SalesSiteID = ISNULL(@SalesSiteID, s.SalesSiteID)
	AND i.IndividualTypeID = 2
	AND (
		(i.LastName LIKE '%' + ISNULL(@SearchOther, i.LastName) + '%')
		OR (i.FirstName LIKE '%' + ISNULL(@SearchOther, i.FirstName) + '%')
		OR (i.EmployeeID LIKE '%' + ISNULL(@SearchOther, i.EmployeeID) + '%'
			OR ee.EmailAddress = @SearchOther)
		)
	AND criminal.jsondata LIKE '%' + ISNULL(@filter,criminal.jsondata) + '%'
	AND @FIlter = 'BOOKING' 
	AND criminal.Ignore=0

Open in new window

Avatar of Larry Brister

ASKER

Neither of those work.
I will be passing in either NULL or 'BOOKING' in @filter

SO the [AND] statement has to be included only  If criminal.jsondata contains 'BOOKING' AND criminal.Ignore = 0
ELSe
No FIlter
SOLUTION
Avatar of Jim Horn
Jim Horn
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Jim Horn
Fair enough
You've helped me for years and have earned the right of a "correction" in methodology.

That said...
Yours as well as Vitors both worked in context of the way I framed the question.
Vitors actually gave me the final solution with "best expert guess."
I will be splitting the points if you guys don't mind
I'm ok with the split.
Cheers
Thanks guys
Have a blessed day
Thanks for the split, and thanks for understanding.  -Jim