![](/img/trans.png)
[英]How to display the date as mm/dd/yyyy hh:mm Am/PM using sql server 2008 r2?
[英]How to display Date in DD/MM/YYYY H:MM AM/PM format in SQL Server
我的表中有一个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中使用它:
print convert(nvarchar(10), getdate(), 103) + right(convert(nvarchar(30), getdate(), 0), 8)
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))
这将为您提供期望的输出。
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)
尝试这个
SELECT convert(varchar(20), GetDate(), 0);
仅提取AM / PM
substring(convert(varchar(30), GetDate(), 9), 25, 2);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.