2016-06-21 22 views
2

私は年齢+月齢に変換する方法を理解しようとしています。たとえば、Student's Birthdateが1/1/2000の場合、Student Ageは16と6月のように表示されます。年齢の形式 - 年と月

Student Ageが整数として表示されるようにスクリプトを作成できますその月をどのように含めるかわからない。私は何を変更する必要がありますか?

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」月を簡単に報告することができます。