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
In C#, you can format an integer with commas (thousands separator) using ToString with a format specifier.
int number = 1234567; string formattedNumber = number.ToString("N0"); // "1,234,567" Console.WriteLine(formattedNumber);
Explanation:
"N0": The "N" format specifier stands for Number, and "0" means no decimal places. The output depends on the culture settings, so in regions where , is the decimal separator, you might get 1.234.567.
Alternative:
You can also specify culture explicitly if you need a specific format:
using System.Globalization; int number = 1234567; string formattedNumber = number.ToString("N0", CultureInfo.InvariantCulture); Console.WriteLine(formattedNumber); // "1,234,567"
When working with financial data in C#, proper currency formatting is essential for clear and professional presentation. The .NET framework provides several convenient methods to format numeric values as currency, with the most common being the ToString() method with the "C" format specifier.
For example, decimal amount = 1234.56m; string formatted = amount.ToString("C"); will display "$1,234.56" in US culture.
decimal amount = 1234.56m; string formatted = amount.ToString("C");
For more control over the formatting, you can specify a culture explicitly using CultureInfo - amount.ToString("C", new CultureInfo("fr-FR")) would display "1 234,56 €".
amount.ToString("C", new CultureInfo("fr-FR"))
This allows your application to handle different currency symbols, decimal separators, and grouping conventions appropriately.
If you need to handle multiple currencies or require more specialized formatting, you can also use the String.Format() method or string interpolation with custom format strings.
For instance, String.Format("{0:C}", amount) or $"{amount:C}" achieves the same result as ToString("C"). Additionally, you can control the number of decimal places using format strings like "C2" for two decimal places.
String.Format("{0:C}", amount)
$"{amount:C}"
Remember that when dealing with financial calculations, it's best practice to use the decimal type rather than float or double to avoid rounding errors that could impact currency calculations.
Example
decimal price = 1234.56m; // Basic currency formatting Console.WriteLine(price.ToString("C")); // Output: $1,234.56 // Currency formatting with specific culture Console.WriteLine(price.ToString("C", new CultureInfo("de-DE"))); // Output: 1.234,56 € // Currency formatting with string interpolation Console.WriteLine($"{price:C}"); // Output: $1,234.56 // Controlling decimal places Console.WriteLine(price.ToString("C3")); // Output: $1,234.560
In C#, readonly, const, and static are keywords used to define variables with different behaviors in terms of mutability, memory allocation, and scope.
readonly
const
static
Understanding their differences is crucial for writing efficient and maintainable code. In this article we'll take a look at each and see how they are used.
A const variable is a compile-time constant, meaning its value must be assigned at declaration and cannot be changed later.
string
enum
public class MathConstants { public const double Pi = 3.14159; } // Usage: Console.WriteLine(MathConstants.Pi); // Output: 3.14159
⚠ Limitations:
A readonly field allows initialization either at declaration or in the constructor but cannot be modified afterward.
public class Circle { public readonly double Radius; public readonly double Pi = 3.14159; public Circle(double radius) { Radius = radius; // Allowed because it's inside the constructor. } } // Usage: Circle c = new Circle(5); Console.WriteLine(c.Radius); // Output: 5
✔ Best for: Values that should remain constant per instance but need to be assigned dynamically at runtime.
A static variable belongs to the type itself rather than to any instance of the class.
public class GlobalConfig { public static string ApplicationName = "MyApp"; public static readonly DateTime StartTime = DateTime.Now; } // Usage: Console.WriteLine(GlobalConfig.ApplicationName); // Output: MyApp
✔ Best for: Global state, caching, configuration values, and utility methods.
Choosing the Right One:
Understanding these differences helps you write cleaner, more efficient C# code. Happy coding! 🚀
Register for my free weekly newsletter.