我使用以下存储过程选择数据,dateDT保存为datetime。
有人可以告诉我如何在以下转换结果中替换年份,以便始终是当前年份(独立于表格中保存的年份)?
SELECT dateID,
CONVERT(VARCHAR(11), dateDT, 106) AS dateDT,
CONVERT(VARCHAR(10), dateDT, 126) AS dateDTShort,
countries,
regions
FROM DaysFixed
WHERE countries LIKE '%'+@selCountry+'%'
OR regions LIKE '%'+@selCountry+'%'
ORDER BY dateID
FOR XML PATH('datesDT'), ELEMENTS, TYPE, ROOT('root')
非常感谢,迈克。
答案 0 :(得分:2)
您可以更改转化后的年份。以下是日期格式的示例:
CONVERT(VARCHAR(7), dateDT, 106) + CAST(year(getdate() as varchar(4)) AS dateDT,
STUFF(CONVERT(VARCHAR(10), dateDT, 126), 1, 4, CAST(year(getdate() as varchar(4)))
) AS dateDTShort,
答案 1 :(得分:2)
DECLARE @dt DATETIME = '2012-04-27 14:00:39.343'
SELECT CAST(
STUFF(
CONVERT(VARCHAR(23), @dt,121)
,1, 4, CAST(YEAR(GETDATE()) AS VARCHAR(4))
)
AS DATETIME)
Result: 2014-04-27 14:00:39.343