数据库中通过日期计算年龄的SQL语句是什么
Admin 2022-08-05 群英技术资讯 698 次浏览
定义函数:
CREATE FUNCTION [dbo].[GetAge] ( @BirthDay nvarchar(20) --生日 ) RETURNS varchar(20) AS BEGIN if(@BirthDay is NUlL or @BirthDay='') return ''; -- Declare the return variable here DECLARE @age varchar(20) DECLARE @years int DECLARE @months int DECLARE @days int -- Add the T-SQL statements to compute the return value here set @age = '' set @years = year(GETDATE()) - year(@birthday) set @months = month(GETDATE()) - month(@birthday) if day(@birthday)<=day(GETDATE()) set @days = day(GETDATE()) - day(@birthday) else begin set @months = @months - 1 if MONTH(@birthday) in (1,3,5,7,8,10,12) set @days = 31-day(@birthday)+day(GETDATE()) else if MONTH(@birthday) in (4,6,9,11) set @days = 30-day(@birthday)+day(GETDATE()) else if MONTH(@birthday) = 2 if (year(@birthday)%4 = 0 and year(@birthday)%100 <> 0) or year(@birthday)%400 = 0 set @days = 29-day(@birthday)+day(GETDATE()) else set @days = 28-day(@birthday)+day(GETDATE()) end if @months < 0 begin set @years = @years - 1 set @months = @months + 12 end if @years = 0 and @months = 0 begin return convert(varchar,@days+1) + '天' end if @years > 0 set @age = cast(@years as varchar(5)) + '岁' if @years < 3 and @months > 0 and @years>-1 begin set @age = @age + cast(@months as varchar(5)) + '月' end if @years<0 set @age='' RETURN @age END
使用函数:
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
本文详细讲解了SQL Server的内存机制,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
参考php手册,对php连接sql server系列做出个小结。主要有五种方式:
如果你从网页中获取用户输入,并将其插入到 SQL 数据库中的话,那么你很可能已经暴露于一种被称作 SQL 注入的安全风险之下了。本节将会教你如何防止 SQL 注入,以及如何保护 Perl 这样的服务器端脚本中的程序和 SQL 语句。
序列是根据需要产生的一组有序整数:1, 2, 3 ... 序列在数据库中经常用到,因为许多应用要求数据表中的的每一行都有一个唯一的值,序列为此提供了一种简单的方法。
SQL HAVING 子句,在 SQL 中增加 HAVING 子句原因是,WHERE 关键字无法与 Aggregate 函数一起使用。HAVING子句已添加到SQL中,因为WHERE关键字不能用于聚合函数。
成为群英会员,开启智能安全云计算之旅
立即注册Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008