【问题标题】:How to display Date in DD/MM/YYYY H:MM AM/PM format in SQL Server如何在 SQL Server 中以 DD/MM/YYYY H:MM AM/PM 格式显示日期
【发布时间】:2014-12-11 10:38:11
【问题描述】:
我的表格中有一个 DATETIME 列,我需要在表格中显示日期
以下格式“DD/MM/YYYY H:MM AM/PM”
CreatedBy
2013-07-30 12:44:06.000
2013-07-30 12:45:57.000
2013-08-05 16:51:26.000
2013-08-05 19:08:18.000
2013-08-05 19:11:46.000
2013-09-12 12:44:27.000
我需要这样的日期--> "30/07/2013 12.44 PM"
【问题讨论】:
标签:
sql
sql-server
datetime
【解决方案1】:
在 SQL 中使用它:
print convert(nvarchar(10), getdate(), 103) + right(convert(nvarchar(30), getdate(), 0), 8)
【解决方案2】:
declare @Date datetime = '2013-07-30 12:44:06.000',
@Time time = '12:44:06.000',
@VarcharDate varchar(100),
@VarcharTime varchar(100)
set @VarcharDate= CONVERT(varchar,@Date,111) -- get the date in the format you want
set @VarcharTime = CONVERT(varchar,@Date) -- get the time in format you want
print @VarcharDate + ' '+SUBSTRING(@VarcharTime,13,LEN(@VarcharTime))
这会给你想要的输出。
【解决方案3】:
试试这个
SELECT convert(varchar(20), GetDate(), 0);
仅提取 AM/PM
substring(convert(varchar(30), GetDate(), 9), 25, 2);
【解决方案4】:
DECLARE @Date AS DATETIME
SET @Date = '2013-07-30 12:44:06.000'
SELECT CONVERT(VARCHAR(16), @Date, 103) + ' ' +
substring(convert(varchar(30), @Date, 100), 13, 8)