我有一个包含日期和时间的专栏。我需要将该列转换为整数格式,如下所示。我试过
SELECT
CAST(SUBSTRING(CAST(CAST(date AS DATETIME) AS BINARY(8)),1,4) AS INT) as Rowdate
FROM Spend
但我没有得到以下格式。你能帮帮我们吗?
日期:
2012-09-11 11:40:51.000
2012-09-11 11:49:59.000
2012-09-11 11:37:08.000
2012-09-11 11:58:44.000
2012-09-11 12:02:35.000
2012-09-11 11:47:08.000
2012-09-11 12:05:34.000
2012-09-11 11:53:42.000
2012-09-11 12:02:51.000
2012-09-11 12:06:09.000
2012-09-11 11:58:56.000
2012-09-11 11:58:14.000
2012-09-11 12:03:28.000
2012-09-11 11:53:24.000
2012-09-11 11:59:15.000
2012-09-11 11:57:08.000
Rowdate:
2012091116
2012091116
2012091116
2012091116
2012091116
2012091116
2012091116
2012091116
2012091116
2012091116
2012091116
2012091116
2012091116
2012091116
2012091116
2012091116
答案 0 :(得分:1)
DECLARE @TABLE TABLE(Col DATETIME)
INSERT INTO @TABLE VALUES
(GETDATE()),(GETDATE()-1),(GETDATE()-3)
SELECT CONVERT(VARCHAR(8),Col,112) + '16' --<-- Dont know why but here is your 16
FROM @TABLE
<强> RESULT 强>
2014030416
2014030316
2014030116
我添加了这个16,因为它显示在你的欲望输出中,但是如果你只想要那个带有'YYYYMMDD'的部分,那么以下就可以完成这项工作。
SELECT CONVERT(VARCHAR(8), ColumnName ,112)
FROM Table_Name
<强> RESULT 强>
20140304
20140303
20140301