The CASE statement in SQL allows you to implement conditional logic within queries, making it a powerful tool for handling complex data transformations and classifications.
CASE
The CASE statement works like an IF-ELSE structure, evaluating conditions and returning corresponding values:
IF-ELSE
SELECT column_name, CASE WHEN condition1 THEN result1 WHEN condition2 THEN result2 ELSE default_result END AS alias_name FROM table_name;
Assume we have an Employees table with an Age column, and we want to categorize employees based on their age groups:
Employees
Age
SELECT Name, Age, CASE WHEN Age < 25 THEN 'Young' WHEN Age BETWEEN 25 AND 40 THEN 'Mid-Age' ELSE 'Senior' END AS AgeCategory FROM Employees;
CASE is often used in aggregate functions to perform conditional counting or summing:
SELECT SUM(CASE WHEN Age < 25 THEN 1 ELSE 0 END) AS YoungCount, SUM(CASE WHEN Age BETWEEN 25 AND 40 THEN 1 ELSE 0 END) AS MidAgeCount, SUM(CASE WHEN Age > 40 THEN 1 ELSE 0 END) AS SeniorCount FROM Employees;
You can use CASE to customize sorting order dynamically:
SELECT Name, Age FROM Employees ORDER BY CASE WHEN Age < 25 THEN 1 WHEN Age BETWEEN 25 AND 40 THEN 2 ELSE 3 END;
The CASE statement is a versatile tool in SQL for implementing conditional logic in SELECT, WHERE, ORDER BY, and aggregate functions. It enhances query flexibility, making data classification and transformation more efficient.
SELECT
WHERE
ORDER BY
When working with URLs in C#, encoding is essential to ensure that special characters (like spaces, ?, &, and =) don’t break the URL structure. The recommended way to encode a string for a URL is by using Uri.EscapeDataString(), which converts unsafe characters into their percent-encoded equivalents.
string rawText = "hello world!"; string encodedText = Uri.EscapeDataString(rawText); Console.WriteLine(encodedText); // Output: hello%20world%21
This method encodes spaces as %20, making it ideal for query parameters.
For ASP.NET applications, you can also use HttpUtility.UrlEncode() (from System.Web), which encodes spaces as +:
using System.Web; string encodedText = HttpUtility.UrlEncode("hello world!"); Console.WriteLine(encodedText); // Output: hello+world%21
For .NET Core and later, Uri.EscapeDataString() is the preferred choice.
Raw string literals in C# provide a flexible way to work with multiline strings, with some interesting rules around how quotes work.
The key insight is that you can use any number of double quotes (three or more) to delimit your string, as long as the opening and closing sequences have the same number of quotes.
"""
// Three quotes - most common usage string basic = """ This is a basic multiline string """; // Four quotes - when your content has three quotes string withThreeQuotes = """" Here's some text with """quoted""" content """"; // Five quotes - when your content has four quotes string withFourQuotes = """"" Here's text with """"nested"""" quotes """""; // Six quotes - for even more complex scenarios string withFiveQuotes = """""" Look at these """""nested""""" quotes! """""";
The general rule is that if your string content contains N consecutive double quotes, you need to wrap the entire string with at least N+1 quotes. This ensures the compiler can properly distinguish between your content and the string's delimiters.
// Example demonstrating the N+1 rule string example1 = """ No quotes inside """; // 3 quotes is fine string example2 = """" Contains """three quotes""" """"; // Needs 4 quotes (3+1) string example3 = """"" Has """"four quotes"""" """""; // Needs 5 quotes (4+1)
// Indentation example string properlyIndented = """ { "property": "value", "nested": { "deeper": "content" } } """; // This line's position determines the indentation
This flexibility with quote counts makes raw string literals extremely versatile, especially when dealing with content that itself contains quotes, like JSON, XML, or other structured text formats.
String interpolation, introduced in C# 6.0, provides a more readable and concise way to format strings compared to traditional concatenation (+) or string.Format(). Instead of manually inserting variables or placeholders, you can use the $ symbol before a string to directly embed expressions inside brackets.
string name = "Walt"; string job = 'Software Engineer'; string message = $"Hello, my name is {name} and I am a {job}"; Console.WriteLine(message);
This would produce the final output of:
Hello, my name is Walt and I am a Software Engineer
String interpolation can also be chained together into a multiline string (@) for even cleaner more concise results:
string name = "Walt"; string html = $@" <div> <h1>Welcome, {name}!</h1> </div>";
Register for my free weekly newsletter.