Go Back  Xtreme Visual Basic Talk > Legacy Visual Basic (VB 4/5/6) > Database and Reporting > Case Statement


Reply
 
Thread Tools Display Modes
  #1  
Old 07-21-2008, 03:07 AM
clintv clintv is offline
Regular
 
Join Date: Jul 2007
Posts: 68
Default Case Statement


Hi,

Is it possible to use IN operator in CASE statement? If not, what are the other options i could use in this code:

WHERE CASE @ExceptionStat WHEN 'A' THEN ExceptionStatus IN ('F','O','P','R') WHEN 'P' ExceptionStatus IN ('F','O','P') ELSE ExceptionStatus IN ('O','R') END
AND(A.DatePaid BETWEEN @DatePaidFrom AND @DatePaidTo)
AND A.Currency = @Currency

Thank you so much!
Reply With Quote
  #2  
Old 07-22-2008, 08:04 AM
Timbo's Avatar
Timbo Timbo is offline
Green-Eyed

Super Moderator
* Guru *
 
Join Date: May 2001
Location: Bangkok, Thailand
Posts: 10,261
Default

Yes, but your CASE construct is wrong:
Quote:
CASE WHEN @ExceptionStat = 'A' THEN
__________________
"He's not the Messiah. He's a very naughty boy!" - Brian's mum

Can't find the answer? >> Try something new!
Become a Professional
Reply With Quote
  #3  
Old 07-22-2008, 09:30 PM
clintv clintv is offline
Regular
 
Join Date: Jul 2007
Posts: 68
Default

Thanks Timbo! Ill check it out il give you feedback later thanks!
Reply With Quote
  #4  
Old 07-22-2008, 09:41 PM
clintv clintv is offline
Regular
 
Join Date: Jul 2007
Posts: 68
Default

Hi Timbo,

Ive tried what you told me here's my code:

WHERE CASE WHEN @ExceptionStat = 'A' THEN ExceptionStatus IN ('F','O','P','R') WHEN @ExceptionStat = 'P' THEN ExceptionStatus IN ('F','O','P') ELSE ExceptionStatus IN ('O','R') END
AND(A.DatePaid BETWEEN @DatePaidFrom AND @DatePaidTo)
AND A.Currency = @Currency


It doesnt work still
Reply With Quote
  #5  
Old 07-22-2008, 09:59 PM
NEOLLE's Avatar
NEOLLE NEOLLE is offline
fully realized avatar

Super Moderator
* Expert *
 
Join Date: Jun 2004
Location: Davao Philippines
Posts: 2,295
Default

Hello clintv,

I'm not sure if this is possible but I don't recommend such method to be used in a where clause since you can not optimize the query's performance. This is difficult (if not impossible) to tune.

Reply With Quote
  #6  
Old 07-22-2008, 11:06 PM
clintv clintv is offline
Regular
 
Join Date: Jul 2007
Posts: 68
Default

ahh i see thanks. any suggestion anyone?
Reply With Quote
  #7  
Old 07-23-2008, 12:43 AM
NEOLLE's Avatar
NEOLLE NEOLLE is offline
fully realized avatar

Super Moderator
* Expert *
 
Join Date: Jun 2004
Location: Davao Philippines
Posts: 2,295
Default

One way is to use the UNION operator.
Reply With Quote
  #8  
Old 07-23-2008, 04:35 AM
clintv clintv is offline
Regular
 
Join Date: Jul 2007
Posts: 68
Default

Im afraid i cannot use UNION operator with this one since im accessing one table am i right? Ill get back to you if i found the solution with this one thanks anyway.
Reply With Quote
  #9  
Old 07-23-2008, 04:41 AM
NEOLLE's Avatar
NEOLLE NEOLLE is offline
fully realized avatar

Super Moderator
* Expert *
 
Join Date: Jun 2004
Location: Davao Philippines
Posts: 2,295
Default

Yes you can use the UNION operator even if you are accessing only one table.
Reply With Quote
  #10  
Old 07-24-2008, 01:27 AM
clintv clintv is offline
Regular
 
Join Date: Jul 2007
Posts: 68
Default

so i need to have CASE statement whit UNION operator? Can you give me the syntax please i cannot find any in the internet thanks
Reply With Quote
  #11  
Old 07-24-2008, 01:57 AM
clintv clintv is offline
Regular
 
Join Date: Jul 2007
Posts: 68
Default

I finally got the solution for this heres my code

WHERE ExceptionStatus = CASE WHEN @ExceptionStat = 'A' THEN 'F' END
oR ExceptionStatus = CASE WHEN @ExceptionStat = 'A' THEN 'O' END
OR ExceptionStatus = CASE WHEN @ExceptionStat = 'A' THEN 'P' END
OR ExceptionStatus = CASE WHEN @ExceptionStat = 'A' THEN 'R' END
OR ExceptionStatus = CASE WHEN @ExceptionStat = 'P' THEN 'F' END
OR ExceptionStatus = CASE WHEN @ExceptionStat = 'P' THEN 'O' END
OR ExceptionStatus = CASE WHEN @ExceptionStat = 'P' THEN 'P' END
OR ExceptionStatus = CASE WHEN @ExceptionStat = 'C' THEN 'O' END
OR ExceptionStatus = CASE WHEN @ExceptionStat = 'C' THEN 'R' END
AND(A.DatePaid BETWEEN @DatePaidFrom AND @DatePaidTo)
AND A.Currency = @Currency

Is this process friendly or what? But apparently i still have problem how do i
compare NULL values in CASE expression? This code has syntax error

WHERE ExceptionStatus = CASE WHEN @ExceptionStat = 'A' THEN 'F' END
oR ExceptionStatus = CASE WHEN @ExceptionStat = 'A' THEN 'O' END
OR ExceptionStatus = CASE WHEN @ExceptionStat = 'A' THEN 'P' END
OR ExceptionStatus = CASE WHEN @ExceptionStat = 'A' THEN 'R' END
OR ExceptionStatus = CASE WHEN @ExceptionStat = 'P' THEN 'F' END
OR ExceptionStatus = CASE WHEN @ExceptionStat = 'P' THEN 'O' END
OR ExceptionStatus = CASE WHEN @ExceptionStat = 'P' THEN 'P' END
OR ExceptionStatus = CASE WHEN @ExceptionStat = 'C' THEN 'O' END
OR ExceptionStatus = CASE WHEN @ExceptionStat = 'C' THEN 'R' END
OR ExceptionStatus = CASE WHEN @ExceptionStat = 'C' THEN NULL END
AND(A.DatePaid BETWEEN @DatePaidFrom AND @DatePaidTo)
AND A.Currency = @Currency


How do i do this please help Thanks!!!!
Reply With Quote
  #12  
Old 07-24-2008, 02:07 AM
NEOLLE's Avatar
NEOLLE NEOLLE is offline
fully realized avatar

Super Moderator
* Expert *
 
Join Date: Jun 2004
Location: Davao Philippines
Posts: 2,295
Default

Hello clintv,

I had second thoughts on the UNION operator. I thought I understood what you need.

Have you tried Dynamic SQL? I think this can address your situation - this technique allows you to dynamically construct DML statements based on your criteria.

Im assuming that you are using MS SQL.
Reply With Quote
  #13  
Old 07-24-2008, 03:54 AM
clintv clintv is offline
Regular
 
Join Date: Jul 2007
Posts: 68
Default

ok ill study this tomorrow, im 2 days late of my deadline just because of this I have to work on weekends Anyway, have faith
Reply With Quote
  #14  
Old 07-24-2008, 04:04 AM
clintv clintv is offline
Regular
 
Join Date: Jul 2007
Posts: 68
Default

Unfortunately the info you sent cant help me Im handling large table i cannot use Example 3--Null columns script

Suggestions anyone?
Reply With Quote
  #15  
Old 07-30-2008, 09:43 PM
clintv clintv is offline
Regular
 
Join Date: Jul 2007
Posts: 68
Default

Hi ive finally come to answer with this query here ... il post it for the benfit of everyone

BEFORE:

WHERE CASE @ExceptionStat WHEN 'A' THEN ExceptionStatus IN ('F','O','P','R') WHEN 'P' ExceptionStatus IN ('F','O','P') ELSE ExceptionStatus IN ('O','R') END
AND(A.DatePaid BETWEEN @DatePaidFrom AND @DatePaidTo)
AND A.Currency = @Currency

AFTER:


WHERE ((@ExceptionStat = 'A' AND ExceptionStatus IN ('F','O','P','R')) OR (@ExceptionStat = 'P' AND ExceptionStatus IN ('F','O','P')) OR (@ExceptionStat = 'O' AND ExceptionStatus IN ('O','R')))
AND(A.DatePaid BETWEEN @DatePaidFrom AND @DatePaidTo)
AND A.Currency = @Currency


total transformation
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->