560 attributed reviews in the last 3 years
Refreshingly small course sizes
Outstandingly good courseware
Whizzy online classrooms
Wise Owls only (no freelancers)
Almost no cancellations
We have genuine integrity
We invoice after training
Review 30+ years of Wise Owl
View our top 100 clients
Scalar User Defined Functions in SQL Server - UDFs
Part four of a five-part series of blogs
If you have a calculation that you frequently use in SQL queries you're probably bored of writing out the same code time after time. Why not try creating a user-defined function to save you the hassle? This blog teaches you how to define your own custom functions, including how to use input parameters, how to alter a function after you've created it, and even how to use fancy programming techniques such as variables and IF statements to help structure complex sequences of calculations.
This part of the series explains what to do if you've created a function and decided that you need to change the way that it works.
The easiest way to alter a function that you've created is to right-click on its name in the Object Explorer and choose to modify it.
Choose this option to start altering a function.
When you do this you'll be presented with a page of system-generated code that will look very much like the original code you wrote to create your function. The most important difference is that rather than the word CREATE, the word ALTER is used.
The line starting with ALTER FUNCTION is the important one. You can happily delete the four system-generated lines above it.
You can now modify the code to change the way your function works. For our date format example we could add a section which puts a suffix at the end of the number of the day, like so:
ALTER FUNCTION [dbo].[fnLongDate]
@MyDate AS DATETIME
RETURN DATENAME(DW,@MyDate) + ' ' +
WHEN DAY(@MyDate) IN (1, 21, 31) THEN 'st'
WHEN DAY(@MyDate) IN (2, 22) THEN 'nd'
WHEN DAY(@MyDate) IN (3, 23) THEN 'rd'
END + ' ' +
DATENAME(M,@MyDate) + ' ' +
Finally, in order to make sure the function is actually modified, you need to execute the code that you have written.
This is one of those easy things to forget - on several occasions I've written the code to modify a function, then gone away to test it and wondered why my modifications didn't work. I eventually realised that I hadn't executed the code to perform the modification!
You can execute this code in the same way as any other code: either click the Execute tool at the top of the screen; or press F5 on the keyboard.
To test if your modifications have worked you can simply execute a query which uses it.
Now our function adds the appropriate suffix to the day of the month.
The most drastic modification you can make to a custom function is to delete it entirely! You can do this using the Object Explorer, as shown in the diagram below:
Right-click on the name of your function and choose Delete.
You can also write code to delete your function. This is much easier than the code you need to write in order to create it!
DROP FUNCTION fnLongDate
When you execute this code your function will be removed from the database, although you may have to refresh the Scalar-valued Functions folder in order to prove that this has happened.
The next part of this blog series will show you how to create longer, more complex functions using programming techniques such as using variables and writing conditional statements.
|Parts of this blog|
25 Aytoun Street