Wednesday, September 18, 2013

TSQL: Time in Military Format in 00:00 or 0000 format

Here is a way you can get time in desired military format. I need this to be in 0000 format.

For example If the current time is 12:35 AM, I need to be display this in 1235 in integer format or if you want with 12:35 you can use the following to fulfill your requirement.

DECLARE @time TABLE (
id INT IDENTITY(1, 1),
dt DATETIME)

INSERT INTO @time
VALUES ('7:34:00 PM')

-- time in 24 hour military format
SELECT CONVERT(VARCHAR(5), dt, 114) AS FormatedDT
FROM @time
-- time without colon in military format
SELECT REPLACE(CONVERT(VARCHAR(5), dt, 114),':','') AS FormatedDT
FROM @time

Hope this is useful!!!

No comments:

Post a Comment