Sql 如何以年.月格式计算从出生日期算起的年龄

Sql 如何以年.月格式计算从出生日期算起的年龄,sql,sql-server,Sql,Sql Server,我写了一个查询,但它只返回几年,但我也需要几个月。以下是我写的问题: select cast(datediff(DAY, '2000-06-01 10:00:01', getDate() -1) / (365.23076923074) as varchar) as 'Age' 可能是这样的 Declare @dateofbirth datetime Declare @currentdatetime datetime Declare @years varchar(40) Declare @mon

我写了一个查询,但它只返回几年,但我也需要几个月。以下是我写的问题:

select cast(datediff(DAY, '2000-06-01 10:00:01', getDate() -1) / (365.23076923074) as varchar) as 'Age'

可能是这样的

Declare @dateofbirth datetime
Declare @currentdatetime datetime
Declare @years varchar(40)
Declare @months varchar(30)
Declare @days varchar(30)
set @dateofbirth='1986-03-15' 
set @currentdatetime = getdate()--current datetime
select @years=datediff(year,@dateofbirth,@currentdatetime)-- To find Years
select @months=datediff(month,@dateofbirth,@currentdatetime)-(datediff(year,@dateofbirth,@currentdatetime)*12)
-- To Find Months
select @days=datepart(d,@currentdatetime)-datepart(d,@dateofbirth)-- To Find Days
select @years  +' years,   ' +@months +' months,   '+@days   +' days' asYearMonthDay 
产出将是

29 years, 2 months, -10 days

更新

单行查询将是

SELECT CAST(DATEDIFF(YEAR,'2000-06-01 10:00:01',GETDATE()) AS VARCHAR(10)) + '.' + CAST(DATEDIFF(MONTH,'2000-06-01 10:00:01',GETDATE())-(DATEDIFF(YEAR,'2000-06-01 10:00:01',GETDATE())*12) AS VARCHAR(10)) AS Age
试着这样做:

DECLARE @setdate datetime
            ,@current datetime
SET @setdate = '20000601'
SET @current = GETDATE()
SELECT Years=DATEDIFF(year,@setdate,@current ) 
                      - CASE WHEN MONTH(@current )*100+DAY(@current )<MONTH(@setdate)*100+DAY(@setdate) THEN 1 ELSE 0 END
          , Months=( DATEDIFF(month,@setdate,@current )
                          - CASE WHEN DAY(@current )<DAY(@setdate) THEN 1 ELSE 0 END )  % 12
DECLARE@setdate-datetime
,@当前日期时间
SET@setdate='20000601'
SET@current=GETDATE()
选择Years=DATEDIFF(year,@setdate,@current)
-当月份(@current)*100+天(@current)我的解决方案是:

 select convert(varchar(05),cast(datediff(DAY, '2000-06-01 10:00:01', getDate() -1) / (365.23076923074) as int))+'.'+convert(varchar(05),cast(datediff(MONTH, '2000-06-01 10:00:01', getDate() -1) % (12) as int)) as Age
试试这个:

declare @DateOfBirth date= '1999-06-1'

Select AgeYears=DATEDIFF(year,@DateOfBirth ,getdate())- CASE WHEN MONTH(getdate())*100+DAY(getdate())<MONTH(@DateOfBirth)*100+DAY(@DateOfBirth) then 1 else 0 end
    ,AgeMoanths=(DATEDIFF(month,@DateOfBirth,getdate())  - CASE WHEN DAY(getdate())<DAY(@DateOfBirth) THEN 1 ELSE 0 END ) % 12
    ,Agedays=DATEDIFF (day,dateadd(month,(DATEDIFF(month,@DateOfBirth,getdate())  - CASE WHEN DAY(getdate())<DAY(@DateOfBirth) THEN 1 ELSE 0 END )  ,@DateOfBirth) ,getdate())
declare@DateOfBirth date='1999-06-1'

选择AgeYears=DATEDIFF(year,@DateOfBirth,getdate())-CASE WHEN MONTH(getdate())*100+DAY(getdate())你能不能提供一个例子,正好是你所期望的输出。看这里:我想输出14.11,其中14是年,11是月。我需要单行sql,因为我需要它作为子查询添加。我刚刚给出了示例。您可以将其设置为单行查询将天数添加到查询中会是什么样子?在我看来,这样做可能需要很多额外的逻辑,在不同的月份有不同的天数(加上二月,在同一个月份和年份中有不同的天数!)——尤其是在单个查询中。
Declare @DOB varchar(20)
Set @DOB ='1989-12-15 10:00:01' SELECT CAST(DATEDIFF(YEAR,@DOB,GETDATE()) AS VARCHAR(10)) + 'Years' 
+ CAST(DATEDIFF(MONTH,@DOB,GETDATE())-(DATEDIFF(YEAR,@DOB,GETDATE())*12) AS VARCHAR(10)) + 'Months' 
+ CAST(DATEDIFF(dd,@DOB,GETDATE())-(DATEDIFF(year,@DOB,GETDATE())*1461/4) AS VARCHAR(10)) +'Days'
declare @DateOfBirth date= '1999-06-1'

Select AgeYears=DATEDIFF(year,@DateOfBirth ,getdate())- CASE WHEN MONTH(getdate())*100+DAY(getdate())<MONTH(@DateOfBirth)*100+DAY(@DateOfBirth) then 1 else 0 end
    ,AgeMoanths=(DATEDIFF(month,@DateOfBirth,getdate())  - CASE WHEN DAY(getdate())<DAY(@DateOfBirth) THEN 1 ELSE 0 END ) % 12
    ,Agedays=DATEDIFF (day,dateadd(month,(DATEDIFF(month,@DateOfBirth,getdate())  - CASE WHEN DAY(getdate())<DAY(@DateOfBirth) THEN 1 ELSE 0 END )  ,@DateOfBirth) ,getdate())