bmsqldev bmsqldev - 4 months ago 17
SQL Question

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?

mcr mcr
Answer
SELECT FORMAT(DATEADD(hh,val,'00:00:00'),'hh:mm tt') 
FROM YourTable
Comments