要将 mm/dd/yyyy 字符串转换为真实日期,请使用:
convert(datetime, your_nonDB2_date_string_here ,101)
更多...
“我有没有让这件事变得更复杂?”是的。
不要将日期存储为字符串,真的就这么简单。
我必须假设您的问题中的“SQL”是指 Microsoft SQL Server(MS 不拥有我所知道的“SQL”)。因此,如果您从 DB2 获取 YYYYMMDD 格式的字符串,这恰好是在 SQL Server 中转换为“真实日期”的最安全格式。
因此,当读入 SQL Sever 表时,请执行以下操作:(112 是 YYYYMMDD 的“样式”)
convert(datetime, your_DB2_date_string_here ,112)
您在上面看到“日期时间”的地方说明您可以在 SQL Server 中使用不同的类型:
date --<< date only (no time)
smalldatetime
datetime
datetime2 --<< highest precision
对于“真实日期字段”的 OUTPUT,您可以再次使用 convert 函数,或者如果您有 SQL 2012 或更高版本,您可以使用 format() 函数
对于输出作为 MM/DD/YYYY 使用
convert(varchar,real_date_field,101)
format(real_date_field,'MM/dd/yyyy') --<< nb case sensitive: MM not mm
见:
http://msdn.microsoft.com/en-us/library/ms187928(v=sql.100).aspx
http://www.sql-server-helper.com/sql-server-2012/format-function-vs-convert-function.aspx