question

rkvvrkvarma avatar image
rkvvrkvarma asked

How do I call a scalar user defined function in a stored procedure?

Hello , This is my scalar function!! SET ANSI_NULLS OFF GO SET QUOTED_IDENTIFIER ON GO ALTER FUNCTION [BUSINESS_DURATION_MINTS_FN] (@string varchar(100) ) RETURNS INT AS BEGIN DECLARE @total_mints INT DECLARE @hours INT DECLARE @Mints INT IF @string IS NOT NULL BEGIN SET @string = REPLACE(UPPER(@string),'HOURS','HOUR') SET @string = REPLACE(UPPER(@string),'MINUTES','MINUTE') IF ((CHARINDEX(UPPER('HOUR'),UPPER(@string))!=0) AND (CHARINDEX(UPPER('MINUTE'),UPPER(@string))!= 0)) BEGIN SET @hours = CONVERT(INT,SUBSTRING(@string,1, (CHARINDEX(UPPER('HOUR'), UPPER(@string))-2))) SET @Mints = CONVERT(INT,SUBSTRING(@string,(CHARINDEX(UPPER('HOUR'),UPPER(@string))+4),(CHARINDEX(UPPER('MINUTE'),UPPER(@string))-(CHARINDEX(UPPER('HOUR'), UPPER(@string))+5)))) END IF ((CHARINDEX(UPPER('HOUR'),UPPER(@string))=0) AND (CHARINDEX(UPPER('MINUTE'),UPPER(@string))!= 0)) BEGIN SET @hours = 0 SET @Mints = CONVERT(INT,SUBSTRING(@string,1, (CHARINDEX(UPPER('MINUTE'), UPPER(@string))-2))) END IF ((CHARINDEX(UPPER('HOUR'),UPPER(@string))!=0) AND (CHARINDEX(UPPER('MINUTE'),UPPER(@string))=0)) BEGIN SET @hours = CONVERT(INT,SUBSTRING(@string,1, (CHARINDEX(UPPER('HOUR'), UPPER(@string))-2))) SET @Mints = 0 END IF ((CHARINDEX(UPPER('HOUR'),UPPER(@string))= 0) AND (CHARINDEX(UPPER('MINUTE'),UPPER(@string))=0)) BEGIN SET @hours = 0 SET @Mints = 0 END SET @total_mints = @hours*60 + @Mints END IF @string IS NULL BEGIN SET @total_mints = NULL END RETURN @total_mints END GO How do I call this function in a stored procedure?? Can you help me with any code??? Thanks!!
stored-procedurestsql
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

·
JohnM avatar image
JohnM answered
Within the procedure, if you were doing a basic select statement: SELECT dbo.function(@parameter) as 'ColumnA', * FROM dbo.Table If you need to set a variable: SET @x = dbo.function(@parameter) Hope that helps!
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.