SQL Server: Extracting Time from DateTime without Milliseconds
Directly casting a DateTime
value to Time
in SQL Server often includes milliseconds. This article demonstrates how to achieve a clean HH:MM:SS time format without these extra digits.
SQL Server's Time
datatype stores time as a numerical representation (clock ticks), not a formatted string. Therefore, formatting is handled during retrieval.
To get the desired HH:MM:SS output, use the CONVERT
function:
<code class="language-sql">SELECT CONVERT(VARCHAR(8), [time], 108) AS CSTTime</code>
This query converts the time
column to a VARCHAR string of length 8, formatted as HH:MM:SS using style 108, effectively removing milliseconds. Using VARCHAR(8)
ensures the output is precisely HH:MM:SS.
The above is the detailed content of How to Cast DateTime to Time in SQL Server without Milliseconds?. For more information, please follow other related articles on the PHP Chinese website!