获取年,月和日SQL的日期差异

时间:2016-03-22 22:16:35

标签: sql sql-server date date-difference

有没有办法根据今天的日期和他们的生日来计算某人的年龄,然后以下列方式显示:

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

5 个答案:

答案 0 :(得分:1)

这基本上就是你要找的东西:

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;

当用户小于(&lt;)1岁时的结果然后显示他们的年龄在MM&amp;天:

enter image description here

当用户超过1岁且小于6岁时的结果然后显示他们的年龄在YY&amp; MM&amp;天:

enter image description here

当用户超过6岁时的结果然后在YY中显示他们的年龄:

enter image description here

答案 1 :(得分:1)

来自上一个问题 How to calculate age in T-SQL with years, months, and days

你可以做这样的程序

       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

答案 2 :(得分:0)

可能不是最有效的解决方法,但这是我如何做到的:

我必须首先获得今天的日期和人的出生日期之间的日期差异。通过将它与ABS()和剩余(%)函数相结合,我用它来获得年,月,日等。

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,

答案 3 :(得分:0)

$normalizeChars = array( 'Š'=>'S', 'š'=>'s', 'Ð'=>'Dj','Ž'=>'Z', 'ž'=>'z', 'À'=>'A', 'Á'=>'A', 'Â'=>'A', 'Ã'=>'A', 'Ä'=>'A', 'Å'=>'A', 'Æ'=>'A', 'Ç'=>'C', 'È'=>'E', 'É'=>'E', 'Ê'=>'E', 'Ë'=>'E', 'Ì'=>'I', 'Í'=>'I', 'Î'=>'I', 'Ï'=>'I', 'Ñ'=>'N', 'Ń'=>'N', 'Ò'=>'O', 'Ó'=>'O', 'Ô'=>'O', 'Õ'=>'O', 'Ö'=>'O', 'Ø'=>'O', 'Ù'=>'U', 'Ú'=>'U', 'Û'=>'U', 'Ü'=>'U', 'Ý'=>'Y', 'Þ'=>'B', 'ß'=>'Ss','à'=>'a', 'á'=>'a', 'â'=>'a', 'ã'=>'a', 'ä'=>'a', 'å'=>'a', 'æ'=>'a', 'ç'=>'c', 'è'=>'e', 'é'=>'e', 'ê'=>'e', 'ë'=>'e', 'ì'=>'i', 'í'=>'i', 'î'=>'i', 'ï'=>'i', 'ð'=>'o', 'ñ'=>'n', 'ń'=>'n', 'ò'=>'o', 'ó'=>'o', 'ô'=>'o', 'õ'=>'o', 'ö'=>'o', 'ø'=>'o', 'ù'=>'u', 'ú'=>'u', 'û'=>'u', 'ü'=>'u', 'ý'=>'y', 'ý'=>'y', 'þ'=>'b', 'ÿ'=>'y', 'ƒ'=>'f', 'ă'=>'a', 'î'=>'i', 'â'=>'a', 'ș'=>'s', 'ț'=>'t', 'Ă'=>'A', 'Î'=>'I', 'Â'=>'A', 'Ș'=>'S', 'Ț'=>'T', ); $legal_name = utf8_encode($res['LEGAL_NAME']); $res['LEGAL_NAME'] = strtr($legal_name, $normalizeChars);

  

DAYmarker是您的日期字段。

答案 4 :(得分:0)

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
)

示例:

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)

enter image description here