本文介紹了DATEDIFF() 只返回帶有 2 個小數點的年齡的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
我有一個 SELECT 語句,要求提供測試時個人的年齡:
I have a SELECT statement requesting the age of he individual when a test was made:
SELECT
DATEDIFF(dd,BIRTH_DATE,TEST_DATE)/365.25 [Age at Result]
FROM TABLE
WHERE ID = '100'
結果類似于2.056125
.
我在另一個帖子中看到了 轉換為秒并除以 86400.0
,但我仍然得到 6 個小數點.
I saw on another post to convert to seconds and divide by 86400.0
, but I was still getting 6 decimal points.
我回顧的是將年齡設為 2.05
甚至四舍五入到 2.00
.
What I was looking back was to get the age as 2.05
or even round to 2.00
.
謝謝
推薦答案
您可以以所需的精度強制轉換為小數:
You can cast to a decimal with the precision you want:
SELECT CAST(DATEDIFF(day, BIRTH_DATE, TEST_DATE)/365.25 as DECIMAL(10, 2)) as [Age at Result]
FROM TABLE
WHERE ID = 100;
注意:我刪除了100"周圍的單引號.如果 id
是字符串,則僅使用單引號.
Note: I removed the single quotes around "100". Only use single quotes if it the id
is a string.
這篇關于DATEDIFF() 只返回帶有 2 個小數點的年齡的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!