需要一个仅返回年份的标量值函数 [英] Need a scalar valued function which return just the year
问题描述
我创建了一个标量值函数,我需要函数来返回年份..但它不起作用。我的代码如下所示:
I have created a scalar-valued function, i need the function to return the year.. but it doesn't work. My code looks like this:
ALTER FUNCTION [Anketa_MPI].[GetYear]
(
@Date datetime
)
RETURNS datetime
AS
BEGIN
RETURN YEAR(@Date)
END
当我执行函数时选择[Anketa_MPI]。[GetYear](getdate())作为YearToday
我得到这个输出1905-07-08 00:00:00.000
但是当我只是查询选择YEAR(getdate())时它正常工作..我的代码是什么wronkg?
When i execute the function select [Anketa_MPI].[GetYear](getdate()) as YearToday
I get this output 1905-07-08 00:00:00.000
But when i just query select YEAR(getdate()) it works correctly .. what is it wronkg with my code?
推荐答案
如果你只是升ock返回年份。
将您的退货类型更改为整数。
...希望它有所帮助。
If you are only looking for the year to be returned.
Change your return type to integer.
... hope it helps.
ALTER FUNCTION [Anketa_MPI].[GetYear]
(
@Date datetime
)
RETURNS INT
AS
BEGIN
RETURN YEAR(@Date)
END
解决方案1很好,你有返回Int
Solution 1 is good ,you have return as Int
当我只是查询选择YEAR(getdate())时它工作正常..用我的代码是什么wronkg?
when i just query select YEAR(getdate()) it works correctly .. what is it wronkg with my code?
因为Year是sql中的内置函数,用于选择年
部分日期时间
http://msdn.microsoft.com/en-IN/library/ms186313.aspx [ ^ ]
这篇关于需要一个仅返回年份的标量值函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!