需要考虑18岁以下的2个月的18岁的人的年龄 [英] Need to consider age of person as 18 who is 2months less than 18
问题描述
选择datediff(yy,'1986-12-06','2003-10-12 14:55:49.480')作为年份
这给了我17岁的年龄我现在需要考虑是否只剩下两个月完成18年我将如何做到这一点。请建议
select datediff(yy,'1986-12-06','2003-10-12 14:55:49.480')as years
this gives me age as 17 years now i need to consider if it is only two months remaining in completing 18 years how would i do this.Please suggest
推荐答案
好吧,想想几个月的年龄:谁会去在两个月内18岁,是18 * 12-2 = 214个月。
Well, think on the age in months: who is going to be 18 in two months, is 18*12-2=214 months old.
...datediff(mm,...
年龄与日期不完全相同,真正的问题是您正在访问年数 - 这是一个整数值,并且只考虑计算中的年份数。所以
Ages are not quite the same as dates, and the real problem is that you are accessing the number of years - which is an integer value, and only considers the year number in the calculation. So
SELECT datediff(yy,'1986-01-01','2003-12-31') AS years
和
SELECT datediff(yy,'1986-01- 01' , 2003-01-01')AS年
And
SELECT datediff(yy,'1986-01-01','2003-01-01') AS years
也将返回17.
解决方案是使用月数:
Will also both return 17.
A solution is to use the number of months:
SELECT datediff(mm,'1986-01-01','2003-12-31') AS months
哪个好一点 - 你可以比较一下对18 * 12的月数,但如果您需要18年的确切截止日期,它仍然没有帮助:
which is a little better - you can compare the number of months against 18 * 12, but it's still not that helpfull if you need to have an exact cut off date at 18 years:
SELECT datediff(mm,'2003-11-30','2003-12-01')AS months
例如返回1.
更好的解决方案是将年份添加到开始日期:
returns 1 for example.
A better solution is to add the years to the start date:
SELECT DATEDIFF(dd, DATEADD(yy, 18, '1986-12-06'), '2013-10-12') as DAYS
如果结果是负数,则它们不到18,如果它大于零则超过18,如果它是零则是18岁生日!
If the result is negative they are under 18, if it's greater than zero they are over 18, and if it is zero it's their 18th birthday!
这篇关于需要考虑18岁以下的2个月的18岁的人的年龄的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!