Question

I have created an user defined functions in SQL server. Is it possible to call these function in select statement, just like any other built-in function?

Create function [dbo].[TIME_TO_SEC](@inputTime time)
returns INT
As
Begin 
    DECLARE @secDiff INT = datediff(second, '00:00:00', @inputTime)
    return @secDiff;
end 

Currently calling this as below:

DECLARE @ret int;
EXEC TIME_TO_SEC @inputTime='01:00:00'
select @ret;

I am attempting something as simple as:

select TIME_TO_SEC('01:00:00') from TimeTable

Is it feasible in SQL-server?

Was it helpful?

Solution

It's possible, but you need to reference the function by the user's schema

select dbo.TIME_TO_SEC('01:00:00') from TimeTable

OTHER TIPS

if you get an issue with your function, try to add "WITH EXECUTE AS CALLER"

Create function [dbo].[TIME_TO_SEC](@inputTime time)
Returns int
WITH EXECUTE AS CALLER
As
Begin 
    DECLARE @secDiff INT = datediff(second, '00:00:00', @inputTime)
    return @secDiff;
end 

then call the function with select

select  dbo.[TIME_TO_SEC](getdate())

Yes, it's certainly feasible, and it looks like you've got everything you need already. Have you tried it?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top