problem with scalar function in MSSQL

Hello,
I have an error message when trying to create a function that returns the following day in X years.
(Column, parameter or variable #0 : datatype SMALLDATETIME not found

what's wrong with this syntax ?

CREATE FUNCTION [AUJOURDHUI + X AN](@NBRANNEE INT) RETURNS SMALLDATIME
AS
BEGIN
RETURN (SELECT cast(convert(varchar(12),dateadd(yy,@NBRANNEE,GETDATE())+1,103) as smalldatetime) AS [AUJOURDHUI+X ANNEE])
END

thanks you for your help !

regards .
bruno_boccaraAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Barry CunneyCommented:
Hi Bruno,
I think that one problem may be with the format of the datestring that the CAST .. SMALLDATETIME is getting

The following throws a conversion error:
SELECT CAST('23/06/2016' AS smalldatetime)

whereas the following is OK
SELECT CAST('2016-06-23' AS smalldatetime)

so I think that maybe if you do a CONVERT(...,120) you should get the '2016-06-23' format and this may get you the result you need
Vitor MontalvãoMSSQL Senior EngineerCommented:
Why are you converting twice? Dateadd() already returns a datetime value.
This should be enough:
RETURN dateadd(yy,@NBRANNEE,GETDATE()+1)
bruno_boccaraAuthor Commented:
I know that's it's not the best way, but I converted twice to obtain the end of the following day.
if today we are the 06/22/2015,  it gives me 06/23/yyyy.

look the following , it works when it is a simple select, but not in a scalar function ...

DECLARE @NBRANNEE INT
SET @NBRANNEE = -1

(SELECT cast(convert(varchar(12),dateadd(yy,@NBRANNEE,GETDATE())+1,103) as smalldatetime) AS [AUJOURDHUI+X ANNEE])
IT Pros Agree: AI and Machine Learning Key

We’d all like to think our company’s data is well protected, but when you ask IT professionals they admit the data probably is not as safe as it could be.

Barry CunneyCommented:
Hi Bruno,
When I try that exact last statement you posted I get the following error:
"The conversion of a varchar data type to a smalldatetime data type resulted in an out-of-range value."
Vitor MontalvãoMSSQL Senior EngineerCommented:
Try to put the result in a variable before returning the value:
CREATE FUNCTION [AUJOURDHUI + X AN](@NBRANNEE INT) RETURNS SMALLDATETIME
 AS
 BEGIN
    DECLARE @MyNewDate smalldatetime

    SET @MyNewDate = cast(convert(varchar(12),dateadd(yy,@NBRANNEE,GETDATE())+1,103) as smalldatetime)
    RETURN @MyNewDate 
 END

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
bruno_boccaraAuthor Commented:
it works fine.
thanks.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.