我有功能[dbo].[MyAge]
CREATE FUNCTION [dbo].[MyAge]
(
@DOB AS DATE,
@EndDate as DATE = '2999-01-01'
)
RETURNS TINYINT
AS
BEGIN
DECLARE @Result as TINYINT
IF @EndDate = '2999-01-0'
SET @EndDate = GETDATE()
IF @DOB >= @EndDate
SET @Result = 0
ELSE
BEGIN
IF (MONTH(@EndDate)*100)+DAY(@EndDate) >= (MONTH(@DOB)*100)+DAY(@DOB)
SET @Result = DATEDIFF(Year,@DOB,@EndDate)
ELSE
SET @Result = DATEDIFF(Year,@DOB,@EndDate)-1
END
RETURN @Result
END
但是当我选择主题名称时会出现错误。从字符串转换日期和/或时间时转换失败
DECLARE @dob datetime
SET @dob='19560109'
SELECT [dbo].[MyAge]( @dob,default )
我尝试SET @dob='1956-01-09'
但出现相同的结果或者我尝试SET DATEFORMAT ymd
;
我想重建这个函数来计算以月和天为单位的年龄,但由于错误我无法尝试这样做。