Click here to Skip to main content
15,906,567 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
HI,,,


Can anyone knows how to create user-defined functions in MS SQL.

For Ex:
I need a function called addr(string cname) which can take the input string and return some kind of string.



Thank You....
Posted

CREATE FUNCTION FUN_NAME(@parameter DATATYPE)
RETURN VARCHAR2(200)
AS
BEGIN
DECLARE @variable VARCHAR2(200)
// perform your operation here
// when you assign the value to variable use SET keyword
END
RETURN @variable


Following is the way to call function

Select FUN_NAME('parameterValue');
 
Share this answer
 
Comments
PRAKASH_N 27-Feb-13 1:11am    
Getting error : 'FUN_NAME' is not a recognized function name
I am getting this error while executing
PRAKASH_N 27-Feb-13 1:22am    
I found solution.

SELECT [dbo].[FUN_NAME2]() as C1name



Thank U
Hi ,

Mainly UDF are used in SELECT Statement for doing some calculation or find some values based on Column values like as follows

SQL
CREATE FUNCTION [dbo].[fnNthIndex](@Input VARCHAR(8000), @Delimiter CHAR(1), @Ordinal INT)
/**************************************************************************
CREATED BY	: Prabu
CREATED DATE   : 21-Feb-2013
PURPOSE		: To Find the Nth occurrence of a substring

USAGE		:
			

		SELECT dbo.fnNthIndex('ü123ü124ü27339ü', 'ü', 2)
		

***************************************************************************/
RETURNS INT
AS
BEGIN
    DECLARE  @Pointer INT=1,@Last INT=0, @Count INT=1
    WHILE (2 > 1)
    BEGIN
        SET @Pointer = CHARINDEX(@Delimiter,@Input,@Pointer)
        IF @Pointer = 0
            BREAK
        IF @Count = @Ordinal
	   BEGIN
            SET @Last = @Pointer
            BREAK
        END
        SELECT @Count = @Count + 1, @Pointer = @Pointer + 1
      END
    RETURN @Last
END


User Defined Functions[^]

Understand when to use user-defined functions in SQL Server[^]

User-defined function[^]

Regards,
GVPrabu
 
Share this answer
 

SQL
CREATE FUNCTION [dbo].[udf_ReturnData] ( @myString VARCHAR(8000))
RETURNS @MyTable TABLE
   (
    Parameter1     int,
    Parameter2      varchar(10),
.
.
.
    Parametern
   )
--same way you can return any type of data
AS
BEGIN
 --your query goes here
--you can insert the data to @MyTable variable and return that table you will get your result
END
GO

for more details go through these links,
Click Here[]
http://blog.sqlauthority.com/2007/09/08/sql-server-udf-user-defined-function-get-number-of-days-in-month/[^]

 
Share this answer
 
Hi Look into the below Example(Got it from MS Website)

USE AdventureWorks2012;
GO
IF OBJECT_ID (N'dbo.ISOweek', N'FN') IS NOT NULL
    DROP FUNCTION dbo.ISOweek;
GO
CREATE FUNCTION dbo.ISOweek (@DATE datetime)
RETURNS int
WITH EXECUTE AS CALLER
AS
BEGIN
     DECLARE @ISOweek int;
     SET @ISOweek= DATEPART(wk,@DATE)+1
          -DATEPART(wk,CAST(DATEPART(yy,@DATE) as CHAR(4))+'0104');
--Special cases: Jan 1-3 may belong to the previous year
     IF (@ISOweek=0) 
          SET @ISOweek=dbo.ISOweek(CAST(DATEPART(yy,@DATE)-1 
               AS CHAR(4))+'12'+ CAST(24+DATEPART(DAY,@DATE) AS CHAR(2)))+1;
--Special case: Dec 29-31 may belong to the next year
     IF ((DATEPART(mm,@DATE)=12) AND 
          ((DATEPART(dd,@DATE)-DATEPART(dw,@DATE))>= 28))
          SET @ISOweek=1;
     RETURN(@ISOweek);
END;
GO
SET DATEFIRST 1;
SELECT dbo.ISOweek(CONVERT(DATETIME,'12/26/2004',101)) AS 'ISO Week';
SQL


http://msdn.microsoft.com/en-in/library/ms186755.aspx[^]

SQL
CREATE FUNCTION Example( @TableName TableType READONLY)
RETURNS VARCHAR(50)
AS
BEGIN
    DECLARE @name VARCHAR(50)

    SELECT TOP 1 @name = LocationName FROM @TableName
    RETURN @name
END

SQL



Regards
Willington
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900