2016-06-21 132 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」個月。