将一串日期时间拆分为不同的列

时间:2017-05-16 03:07:12

标签: sql sql-server

我有这一行:

,DATEADD(s, C.[Timestamp], CONVERT(DATETIME, '1-1-1970 00:00:00')) As "Timestamp"

将时间戳解析为日期时间格式。

输出结果为:

2017-04-19 19:40:20.000

哪个好,但我想将我的日期和时间分成多个列。像YYYY,MM,DD,HH等。

有人有任何想法吗?

PS:纯粹的sql。 SQL Server 2016

1 个答案:

答案 0 :(得分:2)

使用DATEPART

https://docs.microsoft.com/en-us/sql/t-sql/functions/datepart-transact-sql

<!DOCTYPE HTML>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Bad Math (typeConv)</title>
</head>

<body>
<h1>Doing bad math... will she run??</h1>
<button type="button" onclick="addWrong()">addWrong</button>
<button type="button" onclick="sayHi()">sayHi</button>

<script>    
function addWrong() {
    var x = prompt("X?");
    var y = prompt("Y?");

    var sum = x + y;

    alert(x " + " y " = " + sum);
    } // end addWrong

function sayHi() {
    alert("Hi");
}   
</script>    
</body>
</html>