CASE WHEN within the Where clause. - gei

07-Nov-07 04:59:00
Hi all.
Any syntax examples on using a "CASE WHEN' within the where clause. Is it
legal? Need to use different searchcriteria based on a table value. Something
like...


WHERE OrderHead = @OrdrerHead AND ProduktNr LIKE @ProduktNr
AND (CASE WHEN OrderHead.FieldX = 1 THEN OrderLine.FieldN = 4 ELSE
OrderLine.FieldN = 2)......


Somehow I have ended in a situation that needs something like this :-)

--
Thanks all
Regards Geir
reply
 
 

CASE WHEN within the Where clause. - Tibor Karaszi

07-Nov-07 05:14:42
The CASE expression returns a value. So your WHERE clause should look something like:

WHERE OrderHead = @OrdrerHead
AND ProduktNr LIKE @ProduktNr
AND OrderLine.FieldN =
(CASE WHEN OrderHead.FieldX = 1 THEN 4 ELSE 2 END)......

ButI strongly recommend reading below great article:
http://www.sommarskog.se/dyn-search.html

--
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://sqlblog.com/blogs/tibor_karaszi
reply
 
ADD a INT value to a DATETIME value in a NewDateTime column
promotion
Silverlight    WPF    WCF    WWF    LINQ   
JavaScript    AJAX    ASP.NET    XAML   
C#    VB.NET    VB 6.0    GDI+    IIS    XML   
.NET Generics    Anonymous Methods    Delegate   
Visual Studio .NET    Expression Blend    Virus   
Windows Vista    Windows XP    Windows Update   
Windows 2003 Server    Windows 2008 Server   
SQL Server    Microsoft Excel    Microsoft Word   
SharePoint    BizTalk    Virtual Earth   
.NET Compact Framework    Web Service   

"Everything" RSS / ATOM Feed Parser
How to send and receive messages through message queuing in .Net
How to Read text file as database
SQL Server 2005 Paging Performance Tip
Display code of web page.
Fully Scalable Excel File Importer class for .net using Microsoft Jet driver
Generic Chart Color Manager class that can be used for any charts
Helper class to style the infragistics wingrid
Using Reflection to detemine as Assembly Info in and out.
Helper class to play with Window (Owners and position)
Resolving displayname from the culture using the XmlLanguage and LanguageSpecificStringDictionary class