1

I'm trying to learn function in SQL Server, and I don't know why I cannot get a proper result from a T-SQL function.

The query what creates function is:

CREATE FUNCTION yuri_func1 
     (@valDate VARCHAR(10))
RETURNS VARCHAR
AS
BEGIN
    DECLARE @valWeekday VARCHAR(10);

    SET @valWeekday = DATENAME(WEEKDAY, @valDate);

    RETURN @valWeekday;
END

And the other query is

select dbo.yuri_func1('2017-12-29') as [요일]

but the only result I got is just

Blank. (="")

But when I executed function like this,

select DATENAME(WEEKDAY, '2017-12-29')

the result was

MONDAY

I still don't get that why they return different results.

Does anybody know why?

2
  • 1
    you have not defined the size of return type correct and define the size as RETURNS VARCHAR(10) Commented Dec 29, 2017 at 5:17
  • Thanks Yogesh, I really appreciate your comment :) Commented Dec 29, 2017 at 5:44

1 Answer 1

3

This is because you should be accepting DateTime as a parameter in your function and not varchar

create Function yuri_func1 (@valDate DateTime) --Wrong parameter type
RETURN VARCHAR(10) -- No proper sizing of return type
AS
BEGIN
    declare @valWeekday varCHAR(10);

    Set @valWeekday = DATENAME(WEEKDAY,@valDate);


    return @valWeekday;
END
GO
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Alander. As you commented, I changed function query. and it worked! I appreciate that ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.