2

私は自分の質問で立ち往生しています。というテーブルがありますPatient。この表の列には患者がいDOBます。実際、患者の正確な年齢を表示したいと思います。

例えば:

PatName   DOB (MM/dd/yyyy)   Age
a         06/02/1947         65 Years 1 Month/s 3 Days 
b         07/10/1947         64 Years 11 Month/s 25 Days 
c         -----------        -----------------------      

上記のフォーマットの年齢を表示したい。

私はすでにこれについてグーグルで検索しましたが、誰も助けませんでした。

このplsに関する質問がある場合は、それを投稿してください。

ありがとう

4

4 に答える 4

3

こんにちは以下のクエリを確認してください。

--DROP TABLE patient
CREATE TABLE patient(PatName varchar(100),DOB date,  Age varchar(100))
INSERT INTO patient
VALUES('a','06/02/1947',NULL),('b','07/10/1947',NULL),('c','12/21/1982',NULL)

;WITH CTE(PatName,DOB,years,months,days) AS 
(SELECT PatName,DOB,DATEDIFF(yy,DOB,getdate()),DATEDIFF(mm,DOB,getdate()),DATEDIFF(dd,DOB,getdate()) FROM patient)

--SELECT * FROM CTE

SELECT PatName,DOB,
      CAST(months/12 as varchar(5))+' Years'+CAST((months % 12) as varchar(5))+' month/s '+CAST(CASE WHEN DATEADD(MM,(months % 12),DATEADD(YY,(months/12),DOB)) <= GETDATE() then DATEDIFF(dd,DATEADD(MM,(months % 12),DATEADD(YY,(months/12),DOB)),GETDATE()) ELSE DAY(getdate()) END as varchar(5))+' days' as Age

FROM CTE
于 2012-07-06T10:30:49.553 に答える
2

このリンクをたどると、まさにそれを実行する関数が見つかります。

create function dbo.F_AGE_YYYY_MM_DD
    (
    @START_DATE     datetime,
    @END_DATE       datetime
    )
returns  varchar(10) 
as
/*
Function: F_AGE_YYYY_MM_DD

This function calculates age in years, months and days
from @START_DATE through @END_DATE and
returns the age in format YYYY MM DD.

Years is the number of full years between @START_DATE and @END_DATE.

Months is the number of full months since the last full year anniversary.

Days is the number of days since the last full month anniversary.

*/
begin

declare @AGE varchar(10)

declare @AGE_IN_YEARS       int
declare @AGE_IN_MONTHS  int
declare @AGE_IN_DAYS        int

-- Return null if @START_DATE > @END_DATE
if @START_DATE > @END_DATE begin return @AGE end

select
    @AGE_IN_YEARS = AGE_IN_YEARS,
    @AGE_IN_MONTHS = AGE_IN_MONTHS,
    @AGE_IN_DAYS =
        datediff(dd,
        dateadd(mm,AGE_IN_MONTHS,
        dateadd(yy,AGE_IN_YEARS,START_DATE))
        ,END_DATE)
from
(
select
    AGE_IN_MONTHS =
    case
    when AnniversaryThisMonth <= END_DATE
    then datediff(mm,dateadd(yy,AGE_IN_YEARS,START_DATE),END_DATE)
    else datediff(mm,dateadd(yy,AGE_IN_YEARS,START_DATE),END_DATE)-1
    end,
    *
from
(
select
    AGE_IN_YEARS =
    case
    when AnniversaryThisYear <= END_DATE
    then datediff(yy,START_DATE,END_DATE)
    else datediff(yy,START_DATE,END_DATE)-1
    end,
    *
from
(
select
    AnniversaryThisYear =
    dateadd(yy,datediff(yy,START_DATE,END_DATE),START_DATE),
    AnniversaryThisMonth =
    dateadd(mm,datediff(mm,START_DATE,END_DATE),START_DATE),
    *
from
(
select  START_DATE  = dateadd(dd,datediff(dd,0,@START_DATE),0),
    END_DATE    = dateadd(dd,datediff(dd,0,@END_DATE),0)
) aaaa
) aaa
) aa
) a

select @AGE =
    right('0000'+convert(varchar(4),@AGE_IN_YEARS),4) + ' ' +
    right('00'+convert(varchar(4),@AGE_IN_MONTHS),2) + ' ' +
    right('00'+convert(varchar(4),@AGE_IN_DAYS),2)

return @AGE

end
go

select [Age] = dbo.F_AGE_YYYY_MM_DD('2004-04-07','2006-02-03')

select [Age] = dbo.F_AGE_YYYY_MM_DD('2006-02-03','2006-02-03')

select [Age] = dbo.F_AGE_YYYY_MM_DD('2006-02-05','2006-02-03')

select [Age] = dbo.F_AGE_YYYY_MM_DD('1950-09-13', getdate())
于 2012-07-05T13:20:47.597 に答える
0
DECLARE @date datetime, @tmpdate datetime, @years int, @months int, @days int
SELECT @date = '06/02/1947'

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

print cast(@years as varchar(4)) + ' years, '+ cast (@months as varchar(2))+' months, '+ cast(@days as varchar(2)) + ' days'
于 2012-07-05T13:22:49.430 に答える
-1

これが解決策です

DATEDIFF(year、DOB、getdate())-(CASE WHEN(DATEADD(year、DATEDIFF(year、DOB、getdate())、DOB))> getdate()THEN 1 ELSE 0 END)as Years、
MONTH(getdate()-(DATEADD(year、DATEDIFF(year、DOB、getdate())、DOB)))-1(月/秒)、
DAY(getdate()-(DATEADD(year、DATEDIFF(year、DOB、getdate())、DOB)))-1 as Days
于 2013-03-28T11:26:38.077 に答える