1. <legend id='jx77O'><style id='jx77O'><dir id='jx77O'><q id='jx77O'></q></dir></style></legend>
    2. <tfoot id='jx77O'></tfoot>

      <i id='jx77O'><tr id='jx77O'><dt id='jx77O'><q id='jx77O'><span id='jx77O'><b id='jx77O'><form id='jx77O'><ins id='jx77O'></ins><ul id='jx77O'></ul><sub id='jx77O'></sub></form><legend id='jx77O'></legend><bdo id='jx77O'><pre id='jx77O'><center id='jx77O'></center></pre></bdo></b><th id='jx77O'></th></span></q></dt></tr></i><div id='jx77O'><tfoot id='jx77O'></tfoot><dl id='jx77O'><fieldset id='jx77O'></fieldset></dl></div>
      • <bdo id='jx77O'></bdo><ul id='jx77O'></ul>

      <small id='jx77O'></small><noframes id='jx77O'>

      如何根据出生日期和 getDate() 计算年龄(以年为单位)

      时间:2023-07-17
      <i id='qNGHO'><tr id='qNGHO'><dt id='qNGHO'><q id='qNGHO'><span id='qNGHO'><b id='qNGHO'><form id='qNGHO'><ins id='qNGHO'></ins><ul id='qNGHO'></ul><sub id='qNGHO'></sub></form><legend id='qNGHO'></legend><bdo id='qNGHO'><pre id='qNGHO'><center id='qNGHO'></center></pre></bdo></b><th id='qNGHO'></th></span></q></dt></tr></i><div id='qNGHO'><tfoot id='qNGHO'></tfoot><dl id='qNGHO'><fieldset id='qNGHO'></fieldset></dl></div>

          1. <legend id='qNGHO'><style id='qNGHO'><dir id='qNGHO'><q id='qNGHO'></q></dir></style></legend>
              <tbody id='qNGHO'></tbody>

              <tfoot id='qNGHO'></tfoot>
                <bdo id='qNGHO'></bdo><ul id='qNGHO'></ul>

                <small id='qNGHO'></small><noframes id='qNGHO'>

              • 本文介绍了如何根据出生日期和 getDate() 计算年龄(以年为单位)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我有一张表格,列出了人们的出生日期(目前是 nvarchar(25))

                I have a table listing people along with their date of birth (currently a nvarchar(25))

                如何将其转换为日期,然后计算他们的年龄(以年为单位)?

                How can I convert that to a date, and then calculate their age in years?

                我的数据如下

                ID    Name   DOB
                1     John   1992-01-09 00:00:00
                2     Sally  1959-05-20 00:00:00
                

                我想看:

                ID    Name   AGE  DOB
                1     John   17   1992-01-09 00:00:00
                2     Sally  50   1959-05-20 00:00:00
                

                推荐答案

                闰年/天有问题,方法如下,看下面更新:

                There are issues with leap year/days and the following method, see the update below:

                试试这个:

                DECLARE @dob  datetime
                SET @dob='1992-01-09 00:00:00'
                
                SELECT DATEDIFF(hour,@dob,GETDATE())/8766.0 AS AgeYearsDecimal
                    ,CONVERT(int,ROUND(DATEDIFF(hour,@dob,GETDATE())/8766.0,0)) AS AgeYearsIntRound
                    ,DATEDIFF(hour,@dob,GETDATE())/8766 AS AgeYearsIntTrunc
                

                输出:

                AgeYearsDecimal                         AgeYearsIntRound AgeYearsIntTrunc
                --------------------------------------- ---------------- ----------------
                17.767054                               18               17
                
                (1 row(s) affected)
                

                更新这里有一些更准确的方法:

                UPDATE here are some more accurate methods:

                多年来最好的方法

                DECLARE @Now  datetime, @Dob datetime
                SELECT   @Now='1990-05-05', @Dob='1980-05-05'  --results in 10
                --SELECT @Now='1990-05-04', @Dob='1980-05-05'  --results in  9
                --SELECT @Now='1989-05-06', @Dob='1980-05-05'  --results in  9
                --SELECT @Now='1990-05-06', @Dob='1980-05-05'  --results in 10
                --SELECT @Now='1990-12-06', @Dob='1980-05-05'  --results in 10
                --SELECT @Now='1991-05-04', @Dob='1980-05-05'  --results in 10
                
                SELECT
                    (CONVERT(int,CONVERT(char(8),@Now,112))-CONVERT(char(8),@Dob,112))/10000 AS AgeIntYears
                

                你可以把上面的10000改成10000.0并得到小数,但不会像下面的方法那么准确.

                you can change the above 10000 to 10000.0 and get decimals, but it will not be as accurate as the method below.

                多年来最好的十进制方法

                DECLARE @Now  datetime, @Dob datetime
                SELECT   @Now='1990-05-05', @Dob='1980-05-05' --results in 10.000000000000
                --SELECT @Now='1990-05-04', @Dob='1980-05-05' --results in  9.997260273973
                --SELECT @Now='1989-05-06', @Dob='1980-05-05' --results in  9.002739726027
                --SELECT @Now='1990-05-06', @Dob='1980-05-05' --results in 10.002739726027
                --SELECT @Now='1990-12-06', @Dob='1980-05-05' --results in 10.589041095890
                --SELECT @Now='1991-05-04', @Dob='1980-05-05' --results in 10.997260273973
                
                SELECT 1.0* DateDiff(yy,@Dob,@Now) 
                    +CASE 
                         WHEN @Now >= DATEFROMPARTS(DATEPART(yyyy,@Now),DATEPART(m,@Dob),DATEPART(d,@Dob)) THEN  --birthday has happened for the @now year, so add some portion onto the year difference
                           (  1.0   --force automatic conversions from int to decimal
                              * DATEDIFF(day,DATEFROMPARTS(DATEPART(yyyy,@Now),DATEPART(m,@Dob),DATEPART(d,@Dob)),@Now) --number of days difference between the @Now year birthday and the @Now day
                              / DATEDIFF(day,DATEFROMPARTS(DATEPART(yyyy,@Now),1,1),DATEFROMPARTS(DATEPART(yyyy,@Now)+1,1,1)) --number of days in the @Now year
                           )
                         ELSE  --birthday has not been reached for the last year, so remove some portion of the year difference
                           -1 --remove this fractional difference onto the age
                           * (  -1.0   --force automatic conversions from int to decimal
                                * DATEDIFF(day,DATEFROMPARTS(DATEPART(yyyy,@Now),DATEPART(m,@Dob),DATEPART(d,@Dob)),@Now) --number of days difference between the @Now year birthday and the @Now day
                                / DATEDIFF(day,DATEFROMPARTS(DATEPART(yyyy,@Now),1,1),DATEFROMPARTS(DATEPART(yyyy,@Now)+1,1,1)) --number of days in the @Now year
                             )
                     END AS AgeYearsDecimal
                

                这篇关于如何根据出生日期和 getDate() 计算年龄(以年为单位)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:使用 merge..output 获取 source.id 和 target.id 之间的映射 下一篇:如何使用 T-SQL 临时禁用外键约束?

                相关文章

                • <bdo id='TI7v7'></bdo><ul id='TI7v7'></ul>

              • <legend id='TI7v7'><style id='TI7v7'><dir id='TI7v7'><q id='TI7v7'></q></dir></style></legend>
                <tfoot id='TI7v7'></tfoot>

              • <small id='TI7v7'></small><noframes id='TI7v7'>

                <i id='TI7v7'><tr id='TI7v7'><dt id='TI7v7'><q id='TI7v7'><span id='TI7v7'><b id='TI7v7'><form id='TI7v7'><ins id='TI7v7'></ins><ul id='TI7v7'></ul><sub id='TI7v7'></sub></form><legend id='TI7v7'></legend><bdo id='TI7v7'><pre id='TI7v7'><center id='TI7v7'></center></pre></bdo></b><th id='TI7v7'></th></span></q></dt></tr></i><div id='TI7v7'><tfoot id='TI7v7'></tfoot><dl id='TI7v7'><fieldset id='TI7v7'></fieldset></dl></div>