From the course: Program Databases with Transact-SQL

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

IF ELSE and CASE statements

IF ELSE and CASE statements - SQL Server Tutorial

From the course: Program Databases with Transact-SQL

Start my 1-month free trial

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 could 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'll 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 the 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