如何在 T-SQL 中用年、月、日计算年龄
在 T-SQL (SQL Server 2000) 中计算某人年龄(以年、月和日为单位)的最佳方法是什么?
datediff
函数不能很好地处理年份边界,而且将月份和日期分开也很麻烦。 我知道我可以相对轻松地在客户端完成此操作,但我希望在我的 存储中完成此操作程序。
What would be the best way to calculate someone's age in years, months, and days in T-SQL (SQL Server 2000)?
The datediff
function doesn't handle year boundaries well, plus getting the months and days separate will be a bear. I know I can do it on the client side relatively easily, but I'd like to have it done in my stored procedure.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(27)
获取文本年龄的简单方法如下:
结果格式为:
Simple way to get age as text is as below:
Results Format will be:
下面是一些 T-SQL,它可以提供自 @date 中指定的日期以来的年数、月数和天数。 它考虑到 DATEDIFF() 计算差异时不考虑具体月份或日期这一事实(因此 8/31 和 9/1 之间的月份差异为 1 个月),并使用减少结果的 case 语句来处理该差异,其中合适的。
Here is some T-SQL that gives you the number of years, months, and days since the day specified in @date. It takes into account the fact that DATEDIFF() computes the difference without considering what month or day it is (so the month diff between 8/31 and 9/1 is 1 month) and handles that with a case statement that decrements the result where appropriate.
您是否想计算一个年龄的总天数/月数/年数? 你有开始日期吗? 或者你想剖析它(例如:24年1个月29天)?
如果您有一个正在使用的开始日期,datediff 将使用以下命令输出总天/月/年:
相应的输出为 (8827/290/24)。
现在,如果您想进行解剖方法,则必须减去以天为单位的年数(天 - 365*年),然后对其进行进一步的数学运算以获得月份等。
Are you trying to calculate the total days/months/years of an age? do you have a starting date? Or are you trying to dissect it (ex: 24 years, 1 month, 29 days)?
If you have a start date that you're working with, datediff will output the total days/months/years with the following commands:
with the respective outputs being (8827/290/24).
Now, if you wanted to do the dissection method, you'd have to subtract the number of years in days (days - 365*years), and then do further math on that to get the months, etc.
很老的问题,但我想分享我为计算年龄所做的事情
Quite Old question, but I want to share what I have done to calculate age
下面的 SQL 代码可以提供自 sysdate 以来的年数、月数和天数。
输入此格式的 input_birth_date 值 (dd_mon_yy)。 注意:年、月、年输入相同的值(出生日期)。 例如 85 年 3 月 1 日
Here is SQL code that gives you the number of years, months, and days since the sysdate.
Enter value for input_birth_date this format(dd_mon_yy). note: input same value(birth date) for years, months & days such as 01-mar-85
试试这个...
基本上“DateDiff(年份...”,给你这个人今年的年龄,所以我刚刚添加了一个案例声明,如果他们今年还没有过生日,那么减去 1 年,否则返回该值。
Try this...
Basically the "DateDiff( year...", gives you the age the person will turn this year, so i have just add a case statement to say, if they have not had a birthday yet this year, then subtract 1 year, else return the value.
我创建了一个函数
calculateAge
,它从外部获取参数dateOfBirth
,然后计算以年、月和日为单位的年龄,最后以字符串格式返回。I have created a function
calculateAge
that takes parameterdateOfBirth
from outside and then it calculates the age in years, months and days and finally it returns in string format.还有另一种计算年龄的方法是
见下表
要查找年龄,您可以通过月份计算
结果将是
There is another method for calculate age is
See below table
For finding age,you can calculate through month
Result will be
与函数是同一类东西。
The same sort of thing as a function.
这是一个(稍微)简单的版本:
Here is a (slightly) simpler version:
T-SQL 中的
DateTime
值存储为浮点数。 您只需将日期相减,现在就得到一个新日期,即它们之间的时间跨度。DateTime
values in T-SQL are stored as floats. You can just subtract the dates from each other and you now have a new date that is the timespan between them.输出
Output
我使用我修改过的这个函数(天数部分)来自@Dane的回答: https://stackoverflow.com/a/57720/2097023< /a>
它工作得很好。
I use this Function I modified (the Days part) From @Dane answer: https://stackoverflow.com/a/57720/2097023
It works pretty well.
我已经多次看到这个问题,结果输出年、月、日,但从未输出数字/小数结果。 (至少没有一个不正确舍入的)。
我欢迎对此功能的反馈。 可能还不需要一点调整。
-- 函数的输入是两个日期。
-- 输出是两个日期之间的年数,采用 Decimal(7,4) 格式。
-- 输出始终为正数。
-- 注意:如果差异大于 999.9999,则输出不会处理
-- 逻辑基于三个步骤。
-- 1) 差异是否小于 1 年(0.5000、0.3333、0.6667 等)
-- 2) 差异是否正好是整数年(1、2、3 等)
-- 3) (其他)...差异是年数和一些天数。 (1.5000、2.3333、7.6667 等)
`
I've seen the question several times with results outputting Years, Month, Days but never a numeric / decimal result. (At least not one that doesn't round incorrectly).
I welcome feedback on this function. Might not still need a little adjusting.
-- Input to the function is two dates.
-- Output is the numeric number of years between the two dates in Decimal(7,4) format.
-- Output is always always a possitive number.
-- NOTE:Output does not handle if difference is greater than 999.9999
-- Logic is based on three steps.
-- 1) Is the difference less than 1 year (0.5000, 0.3333, 0.6667, ect.)
-- 2) Is the difference exactly a whole number of years (1,2,3, ect.)
-- 3) (Else)...The difference is years and some number of days. (1.5000, 2.3333, 7.6667, ect.)
`
以下是如何计算给定出生日期和当前日期的年龄。
查询:
输出:
年龄
27天5个月23年
Here is how to calculate the age given a birth date and the current date.
Query:
Output:
Age
27 Days 5 Months 23 Years
通过 ISO 格式日期的算术实现。
测试用例
天数的方法与接受的答案不同,差异显示在下面的评论中:
按案例陈述的天数的简短版本:
如果您只想要年和月的年龄,它可以更简单
注意: SQL Server 日期格式
Implemented by arithmetic with ISO formatted date.
Test Cases
The approach of days is different from the accepted answer, the differences shown in the comments below:
An short version of Days by case statement:
If you want the age of years and months only, it could be simpler
NOTE: A very useful link of SQL Server Date Formats
已经给出了很多解决方案,但我相信这个解决方案既易于理解又可靠,因为它也可以处理闰年:
这个想法是简单地计算两年(出生和现在)之间的年份差异,如果当年尚未达到周年纪念日,则减去 1。
Plenty of solutions have been given already, but I beleive this one to be both easy to understand and reliable, as it will handle leap years as well :
The idea is to simply compute the difference in years between the two years (birth and now), and substract 1 if the anniversary has not been reached for the current year.
以下是我根据出生日期和当前日期计算年龄的方法。
Here is how I calculate the age given a birth date and the current date.
有一种简单的方法,基于两天之间的小时数,但结束日期被截断。
事实证明,这一方法极其准确和可靠。 如果没有 GETDATE() 上的内部 CAST,它可能会在午夜前几个小时翻转生日,但是,有了 CAST,它就会在恰好午夜时改变年龄。
There is an easy way, based on the hours between the two days BUT with the end date truncated.
This one has proven to be extremely accurate and reliable. If it weren't for the inner CAST on the GETDATE() it might flip the birthday a few hours before midnight but, with the CAST, it is dead on with the age changing over at exactly midnight.
对于那些想要在表中创建计算列来存储年龄的人:
For the ones that want to create a calculated column in a table to store the age: