在 SQL 中,我有以下代码片段:
DECLARE
@DayPart as datetime,
@TimePart as datetime
SET @DayPart='2012-01-10 00:00:00.000'
SET @TimePart='2012-08-30 15:41:10.403'
现在我需要:
'2012-01-10 15:41:10.403'
我怎么才能得到它?
在 SQL 中,我有以下代码片段:
DECLARE
@DayPart as datetime,
@TimePart as datetime
SET @DayPart='2012-01-10 00:00:00.000'
SET @TimePart='2012-08-30 15:41:10.403'
现在我需要:
'2012-01-10 15:41:10.403'
我怎么才能得到它?
您应该得到@DayPart'yyyy-mm-dd'
格式和@TimePart格式'HH:MI:SS:MMM(24H)'
并连接两个字符串。
试试这个
SELECT
CONVERT(char(10), @DayPart,126) + ' ' +
CONVERT(VARCHAR(12), @TimePart, 114)
有关 SQL Server 日期格式的更多信息
SQL2K8;
select @DayPart + cast(@TimePart as time)
SELECT CONVERT(VARCHAR(10),@DayPart,111) + ' ' +
CONVERT(VARCHAR(10),@TimePart,108);
SELECT REPLACE(CONVERT(VARCHAR(10),@DayPart,102),'.','-') + ' ' +
CONVERT(VARCHAR(10),@TimePart,108);
但如果您使用的是 SQL Server 2008+
SELECT CONVERT(date, @DayPart) + ' ' + CONVERT(time, @TimePart)