2017-03-08 116 views
0

我想從MSSQL 2014中僅顯示12小時格式和不使用AM/PM格式的日期時間數據。我已經試過各種建議在互聯網如下:將時間轉換爲12小時格式,不使用AM/PM

time_adm = convert(varchar(15), CAST(hadmlog.admdate AS TIME), 100), --//2:30PM 

我也試過format()但它返回一個錯誤「公共語言運行時(CLR)的執行是不是在輕型池支持...等」

time_adm22 = FORMAT(CAST(hadmlog.admdate AS DATETIME),'hh:mm') --error 
+0

試試這個:'DECLARE @t VARCHAR(7)=轉換(VARCHAR(15),CAST(GETDATE()AS TIME),100) SELECT SUBSTRING(@t,1,LEN(@t)-2)' – Sathish

+0

在你的情況下,它會是這樣的:'DECLARE @t varchar(7)= convert(varchar(15), CAST(hadmlog.admdate AS TIME),100) SELECT SUBSTRING(@t,1,LEN(@t)-2)' – Sathish

回答

0

你可以嘗試這樣的事情

DECLARE @tbl TABLE(SomeDate DATETIME); 
INSERT INTO @tbl VALUES({ts'2017-01-01 07:13:52'}),({ts'2017-01-01 17:13:52'}) 

SELECT t.SomeDate 
     ,a.FormattedTime 
     ,LEFT(a.FormattedTime,PATINDEX('%[a,p]m',a.FormattedTime)-1) 
FROM @tbl AS t 
CROSS APPLY(SELECT CONVERT(VARCHAR(20),CAST(t.SomeDate AS TIME),100) AS FormattedTime) AS a 

結果

SomeDate     FormattedTime YourOutput 
2017-01-01 07:13:52.000 7:13AM   7:13 
2017-01-01 17:13:52.000 5:13PM   5:13 
0

你可以嘗試做一個字符串Replace消除AM和PM:

select hadmlog.admdate, 
    REPLACE(REPLACE(convert(varchar(7), CAST(hadmlog.admdate AS Time) , 100),'AM',''),'PM','') as Converted 
from hadmlog 
相關問題