- Introduction
- UDF examples
- Database creation date
- Date the object was created
- Get date part of datetime value
- Get time part of datetime value
- Get the number of working days between two dates
Introduction
SQL Server 2000 supports User-Defined Functions (UDFs) - one or more Transact-SQL statements that can be used to encapsulate code for reuse. User-defined functions cannot make a permanent changes to the data or modify database tables. UDF can change only local objects for this UDF, such as local cursors or variables.There are three types of UDF in SQL Server 2000:
Scalar functions return a single data value (not a table) with RETURNS clause. Scalar functions can use all scalar data types, with exception of timestamp and user-defined data types.
Inline table-valued functions return the result set of a single SELECT statement.
Multistatement table-valued functions return a table, which was built with many TRANSACT-SQL statements.
User-defined functions can be invoked from a query like built-in functions such as OBJECT_ID, LEN, DATEDIFF, or can be executed through an EXECUTE statement like stored procedures.
UDF examples
Here you can find some useful user-defined functions.Database creation date
This UDF will return the creation date for a given database (you should specify database name as parameter for this UDF):CREATE FUNCTION dbo.DBCreationDate |
SELECT dbo.DBCreationDate('pubs')
Date the object was created
This UDF will return the creation date for a given object in the current database:CREATE FUNCTION dbo.ObjCreationDate |
SELECT dbo.ObjCreationDate('authors')
Get date part of datetime value
This UDF will return the date part of datetime value:CREATE FUNCTION dbo.DatePart |
SELECT dbo.DatePart('11/11/2000 11:15AM')
Get time part of datetime value
This UDF will return the time part of datetime value:CREATE FUNCTION dbo.TimePart |
SELECT dbo.TimePart('11/11/2000 11:15AM')
Get the number of working days between two dates
This UDF will return the number of working days between two dates (not including these dates):CREATE FUNCTION dbo.GetWorkingDays |
SELECT dbo.GetWorkingDays ('11/13/2000', '12/27/2000')
No comments:
Post a Comment