SQL 练习40
- 按照出生日期来计算学生的年龄信息
IF OBJECT_ID('GetStudentAge','FN') IS NOT NULL
DROP FUNCTION GetStudentAge
GO
CREATE FUNCTION GetStudentAge(@birth DATE) RETURNS INT
AS
BEGIN
DECLARE @age INT
SET @age = year(GETDATE()) - YEAR(@birth)
IF MONTH(GETDATE()) < MONTH(@birth)
set @age = @age -1
ELSE IF MONTH(GETDATE()) = MONTH(@birth) AND DAY(GETDATE()) < DAY(@birth)
set @age = @age-1
RETURN @age
END
SELECT *,dbo.GetStudentAge(sage) 年龄 from Student
本文来自博客园,作者:弩哥++,转载请注明原文链接:https://www.cnblogs.com/bangbangzoutianya/p/15169109.html