Home >Database >Mysql Tutorial >How to Format SQL Server Time Values as HH:MM:SS?

How to Format SQL Server Time Values as HH:MM:SS?

Patricia Arquette
Patricia ArquetteOriginal
2025-01-21 08:26:10893browse

How to Format SQL Server Time Values as HH:MM:SS?

Formatting SQL Server Time Values as HH:MM:SS

This article explains how to correctly format SQL Server time values as HH:MM:SS, addressing a common misconception about time data type storage.

SQL Server doesn't store time with a specific display format. The underlying storage is format-agnostic. This applies to all date and time types (Date, DateTimeOffset, DateTime2, SmallDateTime, DateTime, and Time). Therefore, simply casting to TIME won't guarantee the HH:MM:SS format.

To achieve the desired HH:MM:SS output, you need to convert the TIME value to a character string. Use the following CONVERT function:

<code class="language-sql">SELECT CONVERT(char(8), [time], 108) AS CSTTime</code>

This converts the TIME value to a character string of length 8 (HH:MM:SS) using style 108. Note the change from char(10) to char(8) to accurately reflect the length of HH:MM:SS.

For more detailed information, consult these resources:

The above is the detailed content of How to Format SQL Server Time Values as HH:MM:SS?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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