我想弄清楚如何将学生年龄转换为年龄+月。例如,如果学生的出生日期是2000年1月1日,则学生年龄将显示为:16和6个月
我能够编写查询脚本,以便学生年龄显示为整数,但我不知道如何包含月份。我需要改变什么?
ALTER procedure [dbo].[RP_Student_Attendance] (@perf_code varchar(10))
as
-- set @perf_code = '3496A'
SELECT
i.short_name,
i.text1 as perf_desc,
i.text2 perf_dates,
i.text3 perf_times,
p.perf_no,
h.[Role],
h.customer_no,
c.sex, b.birthday,
g.grade_level,
--(DATEDIFF(yy,b.birthday,GETDATE())) as student_age,
student_age = CASE
WHEN dateadd(year, datediff (year, b.birthday, GETDATE()),b.birthday) > GETDATE()
THEN datediff (year, b.birthday, GETDATE()) - 1
ELSE datediff (year, b.birthday, GETDATE())end,
c.fname + ' ' + c.lname as student_name
FROM T_INVENTORY i
JOIN T_PERF p on i.inv_no = p.perf_no
JOIN T_TICKET_HISTORY h on p.perf_no = h.perf_no and [Role] = 4
JOIN T_CUSTOMER c on h.customer_no = c.customer_no
LEFT JOIN (select customer_no, key_value as birthday from TX_CUST_KEYWORD where keyword_no = 1) b on c.customer_no = b.customer_no
LEFT JOIN (select customer_no, key_value as grade_level from TX_CUST_KEYWORD where keyword_no = 428) g on c.customer_no = g.customer_no
WHERE p.perf_code = @perf_code
答案 0 :(得分:0)
如果你不关心精确度,这会让你进入球场:
mysql> select floor(datediff(now(), '2000-01-01') / 365) as years, ceil(mod(datediff(now(), '2000-01-01'), 365) / 30) as months;
+-------+--------+
| years | months |
+-------+--------+
| 16 | 6 |
+-------+--------+
我再说一遍,如果你不关心精度,只使用。这可能很容易报告“7”个月,而实际上是“6”。