昨天去一保险公司面试DA,被3个SQL DEVELOPERDBA 围剿,问一大堆技术问题,还要当场写CODE.其中一题是: Write a SQL Function, input data is date of birth then Function should return a integer current Age.
【 在 river08 (sh) 的大作中提到: 】 : 昨天去一保险公司面试DA,被3个SQL DEVELOPERDBA 围剿,问一大堆技术问题,还要当 : 场写CODE.其中一题是: : Write a SQL Function, input data is date of birth then Function should : return a integer current Age. : 这题看似简单,但算的方法和公式有多种,算出结果也不一样,比如,如果你3/01/ : 2020 是你的20岁生日,那么今天1/22/2020 你是19 岁还是20 岁? 如果12/1/2019 是 : 你20 岁生日,那今天1/22/2020你是20 岁还是21 岁? : 这是保险公司精算师通常要写的Issue Age 公式,看看那位大神能写出最正确保险公司 : 要SQL Function?
MySQL: /* set @birthday:='2001-01-25'; -- '2001-01-24'
select @age:=floor(period_diff(date_format(current_date,'%Y%m'),date_format(@birthday,'%Y%m'))/12); select @age:[email protected] from dual where month(current_date)=month(@birthday) and day(current_date) select @age; */
Create Function Get_Age (@Birth Date) RETURNS INT As BEGIN Declare @Age INT Set @Age = DateDiff(yyyy,@Birth, GetDate() - IIF(Month(GetDate()) IIF(Month(GetDate())=Month(@Birth) And Day(GetDate()) RETURN @Age END
【 在 river08 (sh) 的大作中提到: 】 : 昨天去一保险公司面试DA,被3个SQL DEVELOPERDBA 围剿,问一大堆技术问题,还要当 : 场写CODE.其中一题是: : Write a SQL Function, input data is date of birth then Function should : return a integer current Age. : 这题看似简单,但算的方法和公式有多种,算出结果也不一样,比如,如果你3/01/ : 2020 是你的20岁生日,那么今天1/22/2020 你是19 岁还是20 岁? 如果12/1/2019 是 : 你20 岁生日,那今天1/22/2020你是20 岁还是21 岁? : 这是保险公司精算师通常要写的Issue Age 公式,看看那位大神能写出最正确保险公司 : 要SQL Function?
【 在 river08 (sh) 的大作中提到: 】 : 昨天去一保险公司面试DA,被3个SQL DEVELOPERDBA 围剿,问一大堆技术问题,还要当 : 场写CODE.其中一题是: : Write a SQL Function, input data is date of birth then Function should : return a integer current Age. : 这题看似简单,但算的方法和公式有多种,算出结果也不一样,比如,如果你3/01/ : 2020 是你的20岁生日,那么今天1/22/2020 你是19 岁还是20 岁? 如果12/1/2019 是 : 你20 岁生日,那今天1/22/2020你是20 岁还是21 岁? : 这是保险公司精算师通常要写的Issue Age 公式,看看那位大神能写出最正确保险公司 : 要SQL Function?
第二种方法,聪明一点先比较年份,然后cast DOB 月日 + 今年年份 -》 比较今天的 日期。 这个方法要注意2/29号这个特殊日子, 分开对待。
两种方法都可以精确到具体日期。
【 在 river08 (sh) 的大作中提到: 】 : 昨天去一保险公司面试DA,被3个SQL DEVELOPERDBA 围剿,问一大堆技术问题,还要当 : 场写CODE.其中一题是: : Write a SQL Function, input data is date of birth then Function should : return a integer current Age. : 这题看似简单,但算的方法和公式有多种,算出结果也不一样,比如,如果你3/01/ : 2020 是你的20岁生日,那么今天1/22/2020 你是19 岁还是20 岁? 如果12/1/2019 是 : 你20 岁生日,那今天1/22/2020你是20 岁还是21 岁? : 这是保险公司精算师通常要写的Issue Age 公式,看看那位大神能写出最正确保险公司 : 要SQL Function?
昨天去一保险公司面试DA,被3个SQL DEVELOPERDBA 围剿,问一大堆技术问题,还要当场写CODE.其中一题是:
Write a SQL Function, input data is date of birth then Function should
return a integer current Age.
这题看似简单,但算的方法和公式有多种,算出结果也不一样,比如,如果你3/01/
2020 是你的20岁生日,那么今天1/22/2020 你是19 岁还是20 岁? 如果12/1/2019 是你20 岁生日,那今天1/22/2020你是20 岁还是21 岁?
这是保险公司精算师通常要写的Issue Age 公式,看看那位大神能写出最正确保险公司要SQL Function?
sql server有datediff函数,先把这个写出来就差不多了。
【 在 river08 (sh) 的大作中提到: 】
: 昨天去一保险公司面试DA,被3个SQL DEVELOPERDBA 围剿,问一大堆技术问题,还要当
: 场写CODE.其中一题是:
: Write a SQL Function, input data is date of birth then Function should : return a integer current Age.
: 这题看似简单,但算的方法和公式有多种,算出结果也不一样,比如,如果你3/01/
: 2020 是你的20岁生日,那么今天1/22/2020 你是19 岁还是20 岁? 如果12/1/2019 是
: 你20 岁生日,那今天1/22/2020你是20 岁还是21 岁?
: 这是保险公司精算师通常要写的Issue Age 公式,看看那位大神能写出最正确保险公司
: 要SQL Function?
DateDiff only for start 后面还差很多,可以很简单,也可以很复杂
【 在 TheMatrix (TheMatrix) 的大作中提到: 】
: sql server有datediff函数,先把这个写出来就差不多了。
说清楚要求就能做。
【 在 river08 (sh) 的大作中提到: 】
: DateDiff only for start 后面还差很多,可以很简单,也可以很复杂
Current year - birth year
Expected error is half year. That is it.
【 在 TheMatrix(TheMatrix) 的大作中提到: 】
: 说清楚要求就能做。
简单回复下MySQL解决方案。period_diff返回当前日期和生日之间的月数之差,除以12变成年,求floor留整即可。当前月是否是出生月,如果是,比较当前日期是否小于出
生之日,如果是就是说未到当前整数年龄,减一即可。以下是SQL实现,function的话
放进function定义,return @age即可。
MySQL:
/*
set @birthday:='2001-01-25'; -- '2001-01-24'
select @age:=floor(period_diff(date_format(current_date,'%Y%m'),date_format(@birthday,'%Y%m'))/12);
select @age:[email protected] from dual where month(current_date)=month(@birthday) and day(current_date)
select @age;
*/
我想你的思路是对的,有一个减1 还是减0 的问题我都没想清楚。
【 在 xiejing119 () 的大作中提到: 】
: 简单回复下MySQL解决方案。period_diff返回当前日期和生日之间的月数之差,除以12
: 变成年,求floor留整即可。当前月是否是出生月,如果是,比较当前日期是否小于出
: 生之日,如果是就是说未到当前整数年龄,减一即可。以下是SQL实现,function的话
: 放进function定义,return @age即可。
: MySQL:
: /*
: set @birthday:='2001-01-25'; -- '2001-01-24'
: select @age:=floor(period_diff(date_format(current_date,'%Y%m'),date_
format(
: @birthday,'%Y%m'))/12);
: select @age:[email protected] from dual where month(current_date)=month(@birthday)
and
: ...................
TSql:
Create Function Get_Age (@Birth Date)
RETURNS INT
As
BEGIN
Declare @Age INT
Set @Age = DateDiff(yyyy,@Birth, GetDate() -
IIF(Month(GetDate()) IIF(Month(GetDate())=Month(@Birth) And
Day(GetDate()) RETURN @Age
END
【 在 river08 (sh) 的大作中提到: 】
: 昨天去一保险公司面试DA,被3个SQL DEVELOPERDBA 围剿,问一大堆技术问题,还要当
: 场写CODE.其中一题是:
: Write a SQL Function, input data is date of birth then Function should : return a integer current Age.
: 这题看似简单,但算的方法和公式有多种,算出结果也不一样,比如,如果你3/01/
: 2020 是你的20岁生日,那么今天1/22/2020 你是19 岁还是20 岁? 如果12/1/2019 是
: 你20 岁生日,那今天1/22/2020你是20 岁还是21 岁?
: 这是保险公司精算师通常要写的Issue Age 公式,看看那位大神能写出最正确保险公司
: 要SQL Function?
请问你应聘的这个职位pay range是多少?
【 在 river08 (sh) 的大作中提到: 】
: 昨天去一保险公司面试DA,被3个SQL DEVELOPERDBA 围剿,问一大堆技术问题,还要当
: 场写CODE.其中一题是:
: Write a SQL Function, input data is date of birth then Function should : return a integer current Age.
: 这题看似简单,但算的方法和公式有多种,算出结果也不一样,比如,如果你3/01/
: 2020 是你的20岁生日,那么今天1/22/2020 你是19 岁还是20 岁? 如果12/1/2019 是
: 你20 岁生日,那今天1/22/2020你是20 岁还是21 岁?
: 这是保险公司精算师通常要写的Issue Age 公式,看看那位大神能写出最正确保险公司
: 要SQL Function?
这个可能比较清楚:
CREATE FUNCTION [dbo].[udfGetAge](@DateOfBirth DATETIME)
RETURNS SMALLINT
AS
BEGIN
DECLARE @Age SMALLINT
SET @Age =(DATEDIFF(YY, @DateOfBirth, GETDATE())-
(CASE
WHEN GETDATE() >= DATEADD(YY, DATEDIFF(YY, @DateOfBirth, GETDATE()), @
DateOfBirth) THEN 0
ELSE 1
END))
RETURN @Age
END;
那还不如(today()-dob)/365
【 在 Rolling (A Rolling Stone) 的大作中提到: 】
: Current year - birth year
: Expected error is half year. That is it.
:
: 说清楚要求就能做。
:
这我以前写过,
第一种最直接也是最笨的方法,拆开DOB为年月日,分开比较。
第二种方法,聪明一点先比较年份,然后cast DOB 月日 + 今年年份 -》 比较今天的
日期。 这个方法要注意2/29号这个特殊日子, 分开对待。
两种方法都可以精确到具体日期。
【 在 river08 (sh) 的大作中提到: 】
: 昨天去一保险公司面试DA,被3个SQL DEVELOPERDBA 围剿,问一大堆技术问题,还要当
: 场写CODE.其中一题是:
: Write a SQL Function, input data is date of birth then Function should : return a integer current Age.
: 这题看似简单,但算的方法和公式有多种,算出结果也不一样,比如,如果你3/01/
: 2020 是你的20岁生日,那么今天1/22/2020 你是19 岁还是20 岁? 如果12/1/2019 是
: 你20 岁生日,那今天1/22/2020你是20 岁还是21 岁?
: 这是保险公司精算师通常要写的Issue Age 公式,看看那位大神能写出最正确保险公司
: 要SQL Function?
如果一个人出生于2月29日,第二年2月28日他满周岁了吗?