Search code examples
sql-servertimevarchar

Convert varchar 12h a.m. p.m. to 24h time in SQL Server


How can i convert a string from a 12hr format to time in SQL?

CONVERT( TIME(0), '12:00:00 a.m.' )

Solution

  • Borrowing from Martin's comment you could replace the periods and convert if your data consistently looks like that.

    SELECT CONVERT(TIME(0), REPLACE('11:00:00 p.m.', '.', ''))
    

    OR if you time has a period in it.

    SELECT CONVERT(TIME(0), REPLACE('11:00:00.123 p.m.','.m.', 'm'))