I have a SQL Server 2008 table with a DateTime column that I’d like to convert into 24h format.
The canonical guidance is to use something along the lines of:
select CONVERT(datetime, GetDate(), 121)
which yields:
2012-01-24 15:22:27.340
Perfect. Ok, so applying this knowledge, let’s replace GetDate() with a column for the CONVERT expression:
select top(5) CONVERT(datetime, LogTable.Timestamp, 121) from LogTable
order by LogTable.Id DESC
And we get:
2012-01-24 03:25:47.933
2012-01-24 03:25:46.917
2012-01-24 03:25:46.547
2012-01-24 03:25:46.543
2012-01-24 03:25:46.543
What am I doing wrong? How should the expression be crafted to get CONVERT to work on a column as opposed to the output of the GetDate() function?
I’ve managed to find the resolution to this issue: The service writing timestamps into the SQL table was, through bad formatting, writing “naked” datetimes into the field, effectively making them appear as “AM” irrespective of when they arrived.
I managed to re-write the source code to write 24h datetimes into the field and it is now performing as expected. I am relieved to know my intuition was correct and it was an errant service that was the source.
Thanks to everyone for their suggestions and guidance – very, very much appreciated!