如何从出生日期开始计算年龄。月份格式

时间:2023-01-30 01:27:08

I have written a query but it returns only years, but I need months too. Below is the query I have written:

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

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

5 个解决方案

#1


May be something like this

可能是这样的

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 

output would be

输出将是

29 years, 2 months, -10 days

SQL FIDDLE

Update

Single line query would be

单行查询将是

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

#2


Try this:

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())

#3


Try like this:

试试这样:

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

SQL FIDDLE DEMO

SQL FIDDLE DEMO

And to get the output in format like 14.11 try this:

要以14.11的格式获得输出,请尝试以下方法:

DECLARE @setdate datetime
            ,@current datetime
SET @setdate = '20000601'
SET @current = GETDATE()
SELECT CAST((DATEDIFF(year,@setdate,@current ) 
                      - CASE WHEN MONTH(@current )*100+DAY(@current )<MONTH(@setdate)*100+DAY(@setdate) THEN 1 ELSE 0 END) AS varchar(10))
          +'.' + CAST((( DATEDIFF(month,@setdate,@current )
                          - CASE WHEN DAY(@current )<DAY(@setdate) THEN 1 ELSE 0 END )  % 12) AS varchar(10))

SQL FIDDLE DEMO

SQL FIDDLE DEMO

#4


My solution was :

我的解决方案是:

 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

#5


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'

#1


May be something like this

可能是这样的

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 

output would be

输出将是

29 years, 2 months, -10 days

SQL FIDDLE

Update

Single line query would be

单行查询将是

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

#2


Try this:

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())

#3


Try like this:

试试这样:

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

SQL FIDDLE DEMO

SQL FIDDLE DEMO

And to get the output in format like 14.11 try this:

要以14.11的格式获得输出,请尝试以下方法:

DECLARE @setdate datetime
            ,@current datetime
SET @setdate = '20000601'
SET @current = GETDATE()
SELECT CAST((DATEDIFF(year,@setdate,@current ) 
                      - CASE WHEN MONTH(@current )*100+DAY(@current )<MONTH(@setdate)*100+DAY(@setdate) THEN 1 ELSE 0 END) AS varchar(10))
          +'.' + CAST((( DATEDIFF(month,@setdate,@current )
                          - CASE WHEN DAY(@current )<DAY(@setdate) THEN 1 ELSE 0 END )  % 12) AS varchar(10))

SQL FIDDLE DEMO

SQL FIDDLE DEMO

#4


My solution was :

我的解决方案是:

 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

#5


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'