日期:2014-05-16  浏览次数:21429 次

VARCHAR类型的时间字段相减的SQL语句
string SQL = "SELECT A, B, C, (C-B) AS D from table";
其中B和C是VARCHAR类型的时间字段,例如C字段为18:20(即18点20),B字段为13:40(即13点45),两数相减,想得到相隔的时间,能相减吗?具体怎么写 ,谢谢。
------解决方案--------------------
select datediff(hh,字段一,字段二)
------解决方案--------------------
select datadiff('hh','2010-01-01 '+C,'2010-01-01 '+B)
------解决方案--------------------
首先将varchar转换成date  to_date('B','hh:ss')
select t.update_date,t.create_date ,round(to_number(t.update_date-t.create_date)) as d from table t --(以天为单位)
select t.update_date,t.create_date ,round(to_number(t.update_date-t.create_date)*24) as d from table t --(以小时为单位)
------解决方案--------------------
貌似 sql在转换的时候 会自动把varchar转成datetime