简体   繁体   English

获取年、月、日的日期差异 SQL

[英]Get date difference in year, month, and days SQL

Is there a way to calculate how old someone is based on today's date and their birthday then display it in following manners:有没有办法根据今天的日期和生日计算某人的年龄,然后按以下方式显示:

If a user is less than (<) 1 year old THEN show their age in MM & days.
Example:  10 months & 2 days old 

If a user is more than 1 year old AND less than 6 years old THEN show their age in YY & MM & days.
Example:  5 years & 3 months & 10 days old

If a user is more than 6 years old THEN display their age in YY.
Example:  12 years

This is basically what you are looking for:这基本上就是你要找的:

DECLARE @date1 DATETIME
     , @date2 DATETIME;

SELECT @date1 = '1/1/2008'
    , @date2 = GETDATE();
SELECT CASE
         WHEN DATEDIFF(YEAR, @date1, @date2) < 1 THEN CAST(DATEDIFF(mm, @date1, @date2) AS VARCHAR)+' Months & '+CAST(DATEDIFF(dd, DATEADD(mm, DATEDIFF(mm, @date1, @date2), @date1), @date2) AS VARCHAR)+' Days'
         WHEN DATEDIFF(YEAR, @date1, @date2) BETWEEN 1 AND 5 THEN CAST(DATEDIFF(mm, @date1, @date2) / 12 AS VARCHAR)+' Years & '+CAST(DATEDIFF(mm, @date1, @date2) % 12 AS VARCHAR)+' Months'
         WHEN DATEDIFF(YEAR, @date1, @date2) >= 6 THEN CAST(DATEDIFF(YEAR, @date1, @date2) AS VARCHAR)+' Years'
      END;

Result for when a user is less than (<) 1 year old THEN show their age in MM & days:结果当用户小于 (<) 1 岁 THEN 以 MM 和天数显示他们的年龄:

在此处输入图片说明

Result for when a user is more than 1 year old AND less than 6 years old THEN show their age in YY & MM & days:当用户大于 1 岁且小于 6 岁 THEN 以 YY & MM & days 显示他们的年龄时的结果:

在此处输入图片说明

Result for when a user is more than 6 years old THEN display their age in YY:结果当用户超过 6 岁 THEN 在 YY 中显示他们的年龄:

在此处输入图片说明

from this previous question How to calculate age in T-SQL with years, months, and days来自上一个问题如何使用年、月和日计算 T-SQL 中的年龄

you can do procedure like this你可以做这样的程序

       CREATE procedure [dbo].[proc_datediff]
       (
        @date datetime
        )
       as
      begin 
  DECLARE @diff varchar(70)
  DECLARE  @tmpdate datetime, @years int, @months int, @days int

 SELECT @tmpdate = @date

    SELECT @years = DATEDIFF(yy, @tmpdate, GETDATE()) - CASE WHEN  
    (MONTH(@date) > MONTH(GETDATE())) OR (MONTH(@date) = 
   MONTH(GETDATE()) AND DAY(@date) > DAY(GETDATE())) THEN 1 ELSE 0 END
   SELECT @tmpdate = DATEADD(yy, @years, @tmpdate)
  SELECT @months = DATEDIFF(m, @tmpdate, GETDATE()) - CASE WHEN 
 DAY(@date) > DAY(GETDATE()) THEN 1 ELSE 0 END
   SELECT @tmpdate = DATEADD(m, @months, @tmpdate)
 SELECT @days = DATEDIFF(d, @tmpdate, GETDATE())
  select @diff=
  case
     when @years < 1 then
   concat( @months,'  Months ',@days,'  days ' )
   when @years >=1 and @years < 6
    then 
    concat(@years,'  year ', @months,'  Months ',@days,'  days ' )
 when @years >= 6  then

  concat( @years,'  years ' )
   end;
 select @diff

 end
 execute proc_datediff '1/1/2016'
  go

Probably not the most efficient way to go about it, but here's how I did it:可能不是最有效的方法,但我是这样做的:

I had to first get the date difference between today's date and person's birthdate.我必须首先得到今天的日期和人的生日之间的日期差异。 I used it to get years, months, days, etc by combining it with ABS(), and Remainder (%) function.我将它与 ABS() 和 Remainder (%) 函数结合使用来获得年、月、日等。

declare @year int = 365
declare @month int = 30
declare @sixYears int = 2190

select 
--CAST(DATEDIFF(mm, a.BirthDateTime,  getdate()) AS VARCHAR) as GetMonth,
--CAST(DATEDIFF(dd, DATEADD(mm, DATEDIFF(mm, a.BirthDateTime, getdate()), a.BirthDateTime), getdate()) AS VARCHAR) as GetDays,

CASE 
    WHEN 
        DATEDIFF(dd,a.BirthDateTime,getdate())  < @year 
    THEN 
        cast((DATEDIFF(dd,a.BirthDateTime,getdate()) / (@month)) as varchar) +' Months & ' +
        CAST(ABS(DATEDIFF(dd, DATEADD(mm, DATEDIFF(mm, a.BirthDateTime, getdate()), a.BirthDateTime), getdate())) AS VARCHAR)
        + ' Days'
    WHEN
        DATEDIFF(dd,a.BirthDateTime,getdate()) between @year and @sixYears
    THEN
        cast((DATEDIFF(dd,a.BirthDateTime,getdate()) / (@year)) as varchar) +' Years & ' +
        CAST((DATEDIFF(mm, a.BirthDateTime,  getdate()) % (12)) AS VARCHAR) + ' Months'
    WHEN DATEDIFF(dd,a.BirthDateTime,getdate()) > @sixYears
    THEN cast(a.Age as varchar) + ' Years'

    end as FinalAGE,

dc.DayMarker = cast(getdate() as date)

DAYmarker 是您的日期字段。

CREATE FUNCTION [dbo].[FindDateDiff](@Date1 date,@Date2 date, @IncludeTheEnDate bit)
RETURNS TABLE 
AS
RETURN 
(
    SELECT
        CALC.Years,CALC.Months,D.Days,
        Duration = RTRIM(Case When CALC.Years > 0 Then CONCAT(CALC.Years, ' year(s) ') Else '' End
                       + Case When CALC.Months > 0 Then CONCAT(CALC.Months, ' month(s) ') Else '' End
                       + Case When D.Days > 0 OR (CALC.Years=0 AND CALC.Months=0) Then CONCAT(D.Days, ' day(s)') Else '' End)
    FROM (VALUES(IIF(@Date1<@Date2,@Date1,@Date2),DATEADD(DAY, IIF(@IncludeTheEnDate=0,0,1), IIF(@Date1<@Date2,@Date2,@Date1)))) T(StartDate, EndDate)
    CROSS APPLY(Select
        TempEndYear = Case When ISDATE(CONCAT(YEAR(T.EndDate), FORMAT(T.StartDate,'-MM-dd')))=1 Then CONCAT(YEAR(T.EndDate), FORMAT(T.StartDate,'-MM-dd'))
                        Else CONCAT(YEAR(T.EndDate),'-02-28') End
    ) TEY
    CROSS APPLY(Select EndYear = Case When TEY.TempEndYear > T.EndDate Then DATEADD(YEAR, -1, TEY.TempEndYear) Else TEY.TempEndYear End) EY
    CROSS APPLY(Select
        Years = DATEDIFF(YEAR,T.StartDate,EY.EndYear),
        Months = DATEDIFF(MONTH,EY.EndYear,T.EndDate)-IIF(DAY(EY.EndYear)>DAY(T.EndDate),1,0)
    ) CALC
    CROSS APPLY(Select Days =  DATEDIFF(DAY,DATEADD(MONTH,CALC.Months,DATEADD(YEAR,CALC.Years,T.StartDate)),T.EndDate)) D
)

Sample:样本:

Select [From] = '2021-01-01',[To] = '2021-12-31',IncludeEndDate='Yes',* From dbo.FindDateDiff('2021-01-01','2021-12-31',1)
Select [From] = '2021-01-01',[To] = '2021-12-31',IncludeEndDate='No',* From dbo.FindDateDiff('2021-01-01','2021-12-31',0)
Select [From] = '2015-12-15',[To] = '2018-12-14',IncludeEndDate='Yes',* From dbo.FindDateDiff('2015-12-15','2018-12-14',1)
Select [From] = '2015-12-15',[To] = '2018-12-14',IncludeEndDate='No',* From dbo.FindDateDiff('2015-12-15','2018-12-14',0)

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM