如何在 SQL Server 中将 nvarchar m/d/yy 转换为 mm/dd/yyyy?

2021-09-11 00:00:00 sql type-conversion sql-server

输入是 nvarchar,格式从 m/d/yymm/dd/yyyy.如何在 SQL 中将其转换为 mm/dd/yyyy?到目前为止,CONVERT() 函数适用于格式 m/d/yyyy 但对于 m/d/yy 的错误.

Input is nvarchar that varies in format from m/d/yy to mm/dd/yyyy. How can this be converted to mm/dd/yyyy in SQL? So far the CONVERT() function works for the format m/d/yyyy but errors for m/d/yy.

推荐答案

将其转换为日期,如下所示:

Cast it as a date like so:

cast(@m + '/' + @d + '/' + @y as datetime)

或类似

cast('m/d/yy' as datetime)

cast(Fieldnamehere as datetime)

相关文章