首页 > 解决方案 > Create SQL column that subtracts two date columns

问题描述

This is how my columns look.

I want to create a column that contains the differences between the start time and end time columns.

Start_Time 
2019-02-01 22:38:54.0000000
2019-02-01 22:39:18.0000000
2019-02-01 22:44:43.0000000
2019-02-04 22:17:41.0000000
2019-02-04 22:18:09.0000000
End_Time
2019-02-01 22:38:57.0000000
2019-02-01 22:39:28.0000000
2019-02-01 22:44:44.0000000
2019-02-04 22:17:48.0000000
2019-02-04 22:18:21.0000000

Any help would be greatly appreciated!

标签: sql

解决方案


句法:

DATEDIFF(expr1,expr2)

TIMEDIFF(expr1,expr2)

描述:

DATEDIFF() 返回 (expr1 – expr2),表示为从一个日期到另一个日期的天数。expr1 和 expr2 是日期或日期和时间表达式。计算中仅使用值的日期部分。

TIMEDIFF() 返回 (expr1 - expr2) 表示为时间值。expr1 和 expr2 是时间或日期和时间表达式,但两者必须属于同一类型。

SELECT DATEDIFF(Start_Time, End_Time) AS DateDiff from table_name

资料来源:MariaDB DATEDIFF只接受两个参数。它根据 MySQL 实现而变化。


推荐阅读