SQL: Conditional Where Clause

If you have some scenario where you need to add where clause based on some condition.

for example:
GradeID     MinMark   MaxMark    Grade
1           0          0          A
2           0          10         B
3           11         20         C
4           21         NULL       D


As per above table we need Grade based on parameter @Mark to be checked as per the range of MinMark and MaxMark.
You can achive this by below query:

SELECT Grade
FROM Grades
Where
((MaxMark IS NULL AND MinMark<@Mark)
OR (MaxMark IS NOT NULL AND(@Mark BETWEEN MinMark AND MaxMark)))




Comments

Popular posts from this blog

LINQ: Using int.TryParse() within a LINQ where clause

ASP.Net MVC: Configuring asp.net session state in SQL Server.

IIS: Publish Project to remote IIS with Web Deploy.