【问题标题】:MS SQL Server : calculate age with accuracy of hours and minuetsMS SQL Server:以小时和分钟的精度计算年龄
【发布时间】:2014-06-08 04:42:30
【问题描述】:

我需要一个 SQL 函数来计算年龄。它必须准确并涵盖所有极端情况。
是给婴儿的医院病房,所以30小步的年龄是很常见的情况。

我查看了其他答案,但找不到适用于所有情况的答案。

例如:

  • 2014-04-29 12:59:00.000 出生的宝宝。
  • 现在是 2014-04-29 13:10:23.000,

年龄应为 0 岁、0 个月、0 天、0 小时、11 分钟

如果有人可以提供该功能的最终和最终版本,那就太好了。

(恐怕简单的 DateDiff 解决方案不够好。正如更流行的question 所述:“Datediff 函数不能很好地处理年份边界......”

【问题讨论】:

  • 这是一个 MS-SQL 数据库。
  • 所有这些都必须在查询中完成还是可以在调用应用程序中完成?如果是这样,您为该应用使用的语言是什么?
  • Select DATEDIFF(MINUTE,'2014-04-23 05:23:59.660',GETDATE())??
  • 您能否编辑您的问题并提供更多示例来说明您希望在这个年龄段看到什么?
  • @Bob Brinks 都应该在 SQL 函数中完成。

标签: sql sql-server datetime datediff


【解决方案1】:

此查询将以分钟为单位为您提供日期差异,

select datediff(mi, '2014-04-23 05:23:59.660',getdate())

然后您可以简单地计算minutes/60hoursminutes mod 60minutes

select datediff(mi, '2014-04-23 05:23:59.660',getdate())/60 as [Hours], select datediff(mi, '2014-04-23 05:23:59.660',getdate()) % 60 as [Minutes]

【讨论】:

【解决方案2】:

我们可以使用DATEDIFF 得到年、月、日的差异,然后简单除以秒、分钟和小时的差异。

我已使用@CurrentDate 重新创建原始请求,但@CurrentDate = GETDATE() 将返回执行时的年龄。

DECLARE @BirthDate DATETIME
DECLARE @CurrentDate DATETIME
SET @BirthDate = '2014-04-29 12:59:00.000'
SET @CurrentDate = '2014-04-29 13:10:23.000'

DECLARE @DiffInYears INT
DECLARE @DiffInMonths INT
DECLARE @DiffInDays INT
DECLARE @DiffInHours INT
DECLARE @DiffInMinutes INT
DECLARE @DiffInSeconds INT
DECLARE @TotalSeconds BIGINT


-- Determine Year, Month, and Day differences
SET @DiffInYears = DATEDIFF(year, @BirthDate, @CurrentDate)
IF @DiffInYears > 0
    SET @BirthDate = DATEADD(year, @DiffInYears, @BirthDate)
IF @BirthDate > @CurrentDate
BEGIN
    -- Adjust for pushing @BirthDate into future
    SET @DiffInYears = @DiffInYears - 1
    SET @BirthDate = DATEADD(year, -1, @BirthDate)
END

SET @DiffInMonths = DATEDIFF(month, @BirthDate, @CurrentDate)
IF @DiffInMonths > 0
    SET @BirthDate = DATEADD(month, @DiffInMonths, @BirthDate)
IF @BirthDate > @CurrentDate
BEGIN
    -- Adjust for pushing @BirthDate into future
    SET @DiffInMonths = @DiffInMonths - 1
    SET @BirthDate = DATEADD(month, -1, @BirthDate)
END

SET @DiffInDays = DATEDIFF(day, @BirthDate, @CurrentDate)
IF @DiffInDays > 0
    SET @BirthDate = DATEADD(day, @DiffInDays, @BirthDate)
IF @BirthDate > @CurrentDate
BEGIN
    -- Adjust for pushing @BirthDate into future
    SET @DiffInDays = @DiffInDays - 1
    SET @BirthDate = DATEADD(day, -1, @BirthDate)
END

-- Get number of seconds difference for Hour, Minute, Second differences
SET @TotalSeconds = DATEDIFF(second, @BirthDate, @CurrentDate)

-- Determine Seconds, Minutes, Hours differences
SET @DiffInSeconds = @TotalSeconds % 60
SET @TotalSeconds = @TotalSeconds / 60

SET @DiffInMinutes = @TotalSeconds % 60
SET @TotalSeconds = @TotalSeconds / 60

SET @DiffInHours = @TotalSeconds


-- Display results
 SELECT @DiffInYears AS YearsDiff,
        @DiffInMonths AS MonthsDiff,
        @DiffInDays AS DaysDiff,
        @DiffInHours AS HoursDiff,
        @DiffInMinutes AS MinutesDiff,
        @DiffInSeconds AS SecondsDiff

【讨论】:

    【解决方案3】:

    它会在几秒钟内给你date diffselect datediff(s, '2014-04-23 05:23:59.660',getdate())

    【讨论】:

    • 你可以继续..select datediff(s, '2014-04-23 05:23:59.660',getdate())/60会给你几分钟
    • select datediff(mi, '2014-04-23 05:23:59.660',getdate()) 也会给你几分钟
    • @VijaySinghRana m 将提供 monthsnmi 用于 minutes
    猜你喜欢
    • 2011-06-10
    • 1970-01-01
    • 2021-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多