Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert Integer value into Hour value (AM / PM) in SQL Server

I have a table with hour values. I need to convert the hour value into AM/PM format, as shown here:

val   hour 
---------------
 9     9:00 AM
19     7:00 PM
10    10:00 AM
19     7:00 PM
14     2:00 PM

I have tried this logic:

declare @timing varchar(5)='15:02'

declare @time time=cast(@timing as time)
select convert(varchar(50),@time, 100)

But this requires val to be converted into varchar. Is there any other way or inbuilt function available in SQL Server to do this?

like image 993
bmsqldev Avatar asked Dec 18 '25 21:12

bmsqldev


2 Answers

SELECT FORMAT(DATEADD(hh,val,'00:00:00'),'hh:mm tt') 
FROM YourTable
like image 139
mcr Avatar answered Dec 20 '25 13:12

mcr


Try the following:

SELECT TIMEFROMPARTS(val,0,0,0,0)
  FROM [table];
like image 29
Tyron78 Avatar answered Dec 20 '25 13:12

Tyron78



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!