本文介紹了DATEDIFF() 只返回帶有 2 個小數(shù)點的年齡的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!
問題描述
我有一個 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'
結(jié)果類似于2.056125
.
我在另一個帖子中看到了 轉(zhuǎn)換為秒并除以 86400.0
,但我仍然得到 6 個小數(shù)點.
I saw on another post to convert to seconds and divide by 86400.0
, but I was still getting 6 decimal points.
我回顧的是將年齡設(shè)為 2.05
甚至四舍五入到 2.00
.
What I was looking back was to get the age as 2.05
or even round to 2.00
.
謝謝
推薦答案
您可以以所需的精度強制轉(zhuǎn)換為小數(shù):
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.
這篇關(guān)于DATEDIFF() 只返回帶有 2 個小數(shù)點的年齡的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!
【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!