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

Using a Parameter to change filter from = to in 1

Status
Not open for further replies.

IanWaterman

Programmer
Jun 26, 2002
3,511
GB
This is my failed attempt at a Where clause

where (case when @SelectGL = 'Client Account' then TXN_GLCODE_FK = 211 else TXN_GLCODE_FK in (353, 355) end)
and TXN_AMOUNT <> 0
and cast(TXN_CREATED_DATE as date) >= @StartDate
and cast(TXN_CREATED_DATE as date) <= @Enddate

I know the use of case is wrong. I am trying to to build logic that when

@SelectGL = 'Client Account' then TXN_GLCODE_FK = 211

or

@SelectGL = 'Interest' then TXN_GLCODE_FK in (353, 355)

Any suggestions

Thank you

Ian
 
Code:
Where 
  (
    (@SelectGL = 'Client Account' And TXN_GLCODE_FK = 211)
    or
    (@SelectGL = 'Interest' And TXN_GLCODE_FK in (353, 355)
  )
and TXN_AMOUNT <> 0
and cast(TXN_CREATED_DATE as date) >= @StartDate
and cast(TXN_CREATED_DATE as date) <= @Enddate

-George
Microsoft SQL Server MVP
My Blogs
SQLCop
twitter
"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom
 
I think I missed a parenthesis:

Code:
Where 
  (
    (@SelectGL = 'Client Account' And TXN_GLCODE_FK = 211)
    or
    (@SelectGL = 'Interest' And TXN_GLCODE_FK in (353, 355))
  )
and TXN_AMOUNT <> 0
and cast(TXN_CREATED_DATE as date) >= @StartDate
and cast(TXN_CREATED_DATE as date) <= @Enddate

-George
Microsoft SQL Server MVP
My Blogs
SQLCop
twitter
"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom
 
Thanks George! Really obvious soluition if I thought about it a bit more.

Ian
 
The best solutions are the simple ones. Glad you got it working.

-George
Microsoft SQL Server MVP
My Blogs
SQLCop
twitter
"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom
 
Since @SelectGL is a constant is it possible formatting like below could be more efficient?

Code:
DECLARE @ValidInt1 = 211, @ValidInt2 = 211

IF (@SelectGL = 'Client Account') BEGIN
	SET @ValidInt1 = 353
	SET @ValidInt2 = 355
END

...
WHERE TXN_GLCODE_FK IN (@ValidInt1, @ValidInt2)
...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top