From the course: Complete Guide to Advanced SQL Server

Unlock the full course today

Join today to access over 24,400 courses taught by industry experts.

IF ELSE and CASE statements

IF ELSE and CASE statements

When developing scalar-valued user-defined functions, there are two constructs that you'll probably find most helpful in arriving at a desired output. The first one that I want to take a look at is called an IF-ELSE statement. IF-ELSE can be used to evaluate a condition and return one value if the condition is true, and a different value if the condition is false. I'm going to create a function that will evaluate whether a number is even or odd. The function will be in the application schema and it will be called EvenOdd. It's going to take a single input parameter, and I'm going to use the variable name @InputNumber here. And it'll store an integer data type. This function will return a char(10) data type. And then we have the AS keyword and BEGIN. And finally, we end the CREATE FUNCTION statement down here on Line Number 16. Inside of this function, we're going to create a new variable called Output. And its data type is just going to match what the function will return. So a…

Contents