2016-06-21 287 views
2

我想弄清楚如何将学生年龄转换为年龄+月份。因此,例如,如果学生的出生日期为1/1/2000,则学生年龄将显示为: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

如果你不关心的精度,这可以让你在棒球场:

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 | 
+-------+--------+ 

我再说一遍,如果你不关心精度只使用。当它实际上是“6”时,这可能容易报告“7”个月。