在 SQL 中显示 TIME
Displaying TIME in SQL
如何将 SQL 中的数据类型 TIME 显示为 09:00 或 09:00AM 而不是 09:00:00.000000
例如,我有一个列 start_time
,它的类型是 TIME
当我向其中插入数据时,INSERT INTO Time(start_time) VALUES('09:00:00')
显示为:09:00:00.0000000
你需要看看Format function in T-SQL. This will allow you to format date and time values (and another values) any way you want to. The link I provided is for SQL 2014. Format was added in version 2012. You have your question tagged as 2008, so I'm not sure this answer will apply to you. If not, take a look at Cast and/or Convert here。
顺便说一下,您可能不知道这一点,但是您如何将 date/time 值输入到 SQL 服务器与它的显示方式无关。而且,通常,日期和时间值的显示是在应用程序的某些 UI 层中完成的,而不是在 SQL 服务器级别,即使 T-SQL 提供了格式化方法日期和时间。
您需要根据您的要求设置日期格式。
看看
http://www.sql-server-helper.com/sql-server-2008/sql-server-2008-date-format.aspx
或
http://www.w3schools.com/sql/func_convert.asp
样本
SELECT CONVERT(VARCHAR(10), SYSDATETIME(), 101) AS [MM/DD/YYYY]
SELECT CONVERT(VARCHAR(10), SYSDATETIME(), 103) AS [DD/MM/YYYY]
........
如何将 SQL 中的数据类型 TIME 显示为 09:00 或 09:00AM 而不是 09:00:00.000000
例如,我有一个列 start_time
,它的类型是 TIME
当我向其中插入数据时,INSERT INTO Time(start_time) VALUES('09:00:00')
显示为:09:00:00.0000000
你需要看看Format function in T-SQL. This will allow you to format date and time values (and another values) any way you want to. The link I provided is for SQL 2014. Format was added in version 2012. You have your question tagged as 2008, so I'm not sure this answer will apply to you. If not, take a look at Cast and/or Convert here。
顺便说一下,您可能不知道这一点,但是您如何将 date/time 值输入到 SQL 服务器与它的显示方式无关。而且,通常,日期和时间值的显示是在应用程序的某些 UI 层中完成的,而不是在 SQL 服务器级别,即使 T-SQL 提供了格式化方法日期和时间。
您需要根据您的要求设置日期格式。
看看
http://www.sql-server-helper.com/sql-server-2008/sql-server-2008-date-format.aspx
或
http://www.w3schools.com/sql/func_convert.asp
样本
SELECT CONVERT(VARCHAR(10), SYSDATETIME(), 101) AS [MM/DD/YYYY]
SELECT CONVERT(VARCHAR(10), SYSDATETIME(), 103) AS [DD/MM/YYYY]
........