Welcome to Day 19 of my “A Month of PowerShell” series. This series will use the series landing page on this blog at http://blog.waynesheffield.com/wayne/a-month-of-powershell/. Please refer to this page to see all of the posts in this series, and to quickly go to them.

Continuing the mini-series of objects compiled with T-SQL code, today we’re going to deal with user defined functions.

Creating a Scalar Function

Prior to SQL 2008, if you wanted just the date for today, you’d have to do some date manipulation to truncate off the time portion of a datetime value. Let’s make that into a scalar function that can be called as needed.

Altering Scalar Functions

After working with this function for a bit, you realize that you could use the same functionality for any date. You decide to modify this function to accept a parameter, and the code so that it uses the current date if a null is passed in to the parameter.

This example does not consider changes to the Parameters collection if you rename, add or drop parameters for the function.

Dropping a scalar function

To drop the scalar function, just call the function’s drop method.