【问题标题】:Change a time format from 00:00:00 into seconds in Sql在 Sql 中将时间格式从 00:00:00 更改为秒
【发布时间】:2012-04-30 10:16:32
【问题描述】:

在我的 ASP.Net 应用程序中,我有一个 Sql 数据库。表中的一列称为 productDelivered,它使用数据类型 time(7);但是,我想将时间转换为秒并保留它。我有大约 50-60 条记录。

有没有办法将这些时间更改为秒?我不介意在表中为 productDeliveredSeconds 创建一个新列,但我认为存储过程或视图可能会这样做?

【问题讨论】:

  • 你标记了 mysql 和 sql-server。请澄清。
  • @John Dewey 我在 Visual Studio 中使用 Sql,但不确定它属于哪个类别。我现在已经删除了 sql-server。

标签: asp.net sql sql-server webforms


【解决方案1】:

您可以将computed column 添加到使用datediff 计算秒数的表中。

alter table YourTable add productDeliveredSeconds as datediff(second, 0, productDelivered)

或者您可以在查询中使用datediff

select datediff(second, 0, productDelivered) as productDeliveredSeconds 
from YourTable

【讨论】:

    【解决方案2】:

    TIME_TO_SEC(time) 返回时间参数,转换为秒。

    mysql> SELECT TIME_TO_SEC('22:23:00');
            -> 80580
    mysql> SELECT TIME_TO_SEC('00:39:38');
            -> 2378
    

    【讨论】:

    • 我对你的回答有点迷茫,从哪里收集并输出结果?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-10-07
    • 1970-01-01
    • 2011-06-27
    • 2013-07-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多