Home > Database > Mysql Tutorial > How to Cast DateTime to Time in SQL Server without Milliseconds?

How to Cast DateTime to Time in SQL Server without Milliseconds?

DDD
Release: 2025-01-21 08:36:14
Original
231 people have browsed it

How to Cast DateTime to Time in SQL Server without Milliseconds?

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>
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template