可能重复:
How to calculate age in T-SQL with years, months, and days
我在Sql Server 2005上遇到了年龄计算问题。我有一个客户表,其日期为birth column
。我想要的是创建一个age column
的视图,这个age column
应该通过从今天减去出生日期来显示person
的年龄(非常简单)。
我在这里找到了一些很好的资源,但我想要的是按年和月显示年龄。例如(47岁,3个月)。这意味着我希望同时具有年和月值,并且我想将string
('years', 'yrs', 'y', 'months', 'mon', 'm', ...
}添加到适当的值并将其显示为例如。 (25 years, 2 months
)。
如果此人的年龄仅为3 months
,则应显示' 3 months
'而不是'0 years, 3 months
'。
如果此人的年龄是几年而没有任何月份,则应显示例如。 ' 5 years
'不是'5 years, 0 months
'
哦,我已经使用c#完成了这个,但我想在sql视图上创建一个列,这样我就可以使用视图填充我的gridview,而无需对我的c#代码进行任何计算。 如果有帮助我可以在这里发布我的c#代码。 感谢
答案 0 :(得分:0)
我认为您应该考虑使用该解决方案的本地化问题(以及不同的标准来计算不同国家/地区的年龄)。如果你不关心,你可以:
答案 1 :(得分:0)
你需要这样的东西:
SELECT *,
case when DATEDIFF(MONTH, Birthday, GETDATE()) < 12
then
CONVERT(nvarchar(16), DATEDIFF(MONTH, Birthday, GETDATE())) + ' months'
else
CONVERT(nvarchar(16), DATEDIFF(YEAR, Birthday, GETDATE())) + ' years, ' +
CONVERT(nvarchar(16), DATEDIFF(MONTH, Birthday, GETDATE()) % 12 ) + ' months'
end
FROM dbo.CUSTOMER
答案 2 :(得分:0)
也许是这样的:
测试数据
DECLARE @tbl TABLE(birthDay DATETIME)
INSERT INTO @tbl
VALUES
('2004-04-05'),
('2001-02-05')
<强>查询强>
;WITH Years
AS
(
SELECT
DATEDIFF(yy, tbl.birthDay, GETDATE()) - CASE WHEN (MONTH(tbl.birthDay) > MONTH(GETDATE())) OR (MONTH(tbl.birthDay) = MONTH(GETDATE()) AND DAY(tbl.birthDay) > DAY(GETDATE())) THEN 1 ELSE 0 END AS Years,
tbl.birthDay
FROM
@tbl AS tbl
), BithDayWithMonth
AS
(
SELECT
Years.Years,
DATEDIFF(m, years.tmp, GETDATE()) - CASE WHEN DAY(Years.birthDay) > DAY(GETDATE()) THEN 1 ELSE 0 END AS Months
FROM
(
SELECT
DATEADD(yy, Years.Years, Years.birthDay) AS tmp,
Years.Years,
Years.birthDay
FROM
Years
) AS years
)
SELECT
(CASE WHEN BithDayWithMonth.Years=0 THEN '' ELSE CAST(BithDayWithMonth.Years AS VARCHAR(10))+' years' END)+
(CASE WHEN BithDayWithMonth.Months=0 THEN '' ELSE (CASE WHEN NOT BithDayWithMonth.Years=0 THEN ', ' ELSE '' END) +
CAST(BithDayWithMonth.Years AS VARCHAR(10))+' months' END)
FROM
BithDayWithMonth
答案 3 :(得分:0)
CREATE FUNCTION format_age(
@DOB datetime,
@now datetime
)
RETURNS nvarchar(30)
BEGIN
DECLARE @years int, @months int;
IF @DOB > @now RETURN N'not born yet';
SET @years = DATEDIFF(year, @DOB, @now);
IF MONTH(@DOB) * 100 + DAY(@DOB) > MONTH(@now) * 100 + DAY(@now)
SET @years = @years - 1;
SET @months = DATEDIFF(month, DATEADD(year, @years, @DOB), @now);
IF DAY(@DOB) > DAY(@now)
SET @months = @months - 1;
RETURN CASE
WHEN @years = 0 THEN CONVERT(nvarchar(30), @months) + N' months'
WHEN @months = 0 THEN CONVERT(nvarchar(30), @years) + N' years'
ELSE CONVERT(nvarchar(30), @years) + N' years, ' +
CONVERT(nvarchar(30), @months) + N' months'
END;
END
GO
SELECT '2011-02-16' as dt, dbo.format_age('2011-02-16', '2012-03-16') as age
UNION
SELECT '2011-03-16', dbo.format_age('2011-03-16', '2012-03-16')
UNION
SELECT '2011-03-17', dbo.format_age('2011-03-17', '2012-03-16')
UNION
SELECT '2012-03-16', dbo.format_age('2012-03-16', '2012-03-16')
UNION
SELECT '2012-03-17', dbo.format_age('2012-03-17', '2012-03-16')
dt age
2011-02-16 1 years, 1 months
2011-03-16 1 years
2011-03-17 11 months
2012-03-16 0 months
2012-03-17 not born yet
参考日期为2012-03-16