MS SQL Server:以小时和分钟为精度计算年龄
我需要一个SQL函数来计算年龄。它必须准确,覆盖所有角落的情况。MS SQL Server:以小时和分钟为精度计算年龄,sql,sql-server,datetime,datediff,Sql,Sql Server,Datetime,Datediff,我需要一个SQL函数来计算年龄。它必须准确,覆盖所有角落的情况。 这是婴儿病房,所以30分钟的年龄是很常见的 我看过其他答案,但找不到一个能处理所有案件的答案 例如: 2014-04-29 12:59:00.000出生的婴儿 现在是2014-04-29 13:10:23.000 年龄应为0岁、0个月、0天、0小时、11分钟 如果有人能提供该功能的最终和最终版本,那就太好了 (我担心使用DateDiff的简单解决方案还不够好。。正如一篇比较流行的文章所说:“DateDiff函数不能很好地处理年
这是婴儿病房,所以30分钟的年龄是很常见的 我看过其他答案,但找不到一个能处理所有案件的答案 例如:
- 2014-04-29 12:59:00.000出生的婴儿李>
- 现在是2014-04-29 13:10:23.000
(我担心使用DateDiff的简单解决方案还不够好。。正如一篇比较流行的文章所说:“DateDiff函数不能很好地处理年份边界…”它将在几秒钟内为您提供
选择DateDiff(s,'2014-04-23 05:23:59.660',getdate())
此查询将在几分钟内为您提供日期差
select datediff(mi, '2014-04-23 05:23:59.660',getdate())
然后您可以简单地计算minutes/60
的hours
和minutes mod 60
的minutes
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]
我们可以使用
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
这是一个MS-SQL数据库。所有这些都必须在查询中完成,还是可以在调用应用程序中完成?如果是,那么该应用程序使用的语言是什么?
选择DATEDIFF(分钟,'2014-04-23 05:23:59.660',GETDATE())
?你能编辑你的问题并给出一些你想看到的关于这个年龄的例子吗?@Bob Brinks所有这些都应该在一个SQL函数中完成。你可以继续..选择datediff(s,'2014-04-23 05:23:59.660',getdate())/60
也会给你分钟选择datediff(mi,'2014-04-23 05:23:59.660',getdate())
将为您提供minutes@VijaySinghRanam
将给出months
,n
或mi
表示minutes
@eggyal-DateDiff在MySql中接受2个参数,但在SQL Server中接受3个参数,