Monday, March 26, 2018

How to change datetime into time with AM/PM



How to change date time into time with AM/PM..

SELECT  LEFT(CONVERT(VARCHAR(20), CAST(CONVERT(VARCHAR,GETUTCDATE(),108) AS TIME)), 5) + ' '
+ RIGHT(CONVERT(VARCHAR(20), CAST(CONVERT(VARCHAR,GETUTCDATE(),108) AS TIME), 109), 2)

SELECT LEFT(CONVERT(VARCHAR(5), CAST(CONVERT(VARCHAR,GETUTCDATE(),108) AS TIME), 108),5) + ' '
+ RIGHT(CONVERT(VARCHAR(30),CAST(CONVERT(VARCHAR,GETUTCDATE(),108) AS TIME), 9),2)

you can use below FORMAT() in for higer version on SQL server.

DECLARE @dt AS DATETIME = GETDATE()
SELECT FORMAT(@dt, 'hh:mm tt')

DECLARE @t AS DATETIME = GETDATE()
SELECT FORMAT(CAST(@t AS DATETIME), 'hh:mm tt') 

No comments: