Convert DB2 SQL Decimal to time

落花浮王杯 提交于 2020-07-10 07:33:10

问题


Convert DB2 SQL Decimal to time I need to convert Decimal to time. I have a decimal time field that contains data like this :

123490 --12:34:90
4506 --00:45:06
171205 --17:12:05

etc

I would like to convert into time format then i calculate min between two times columns

Is there anyway to do this with SQL commands or DB2 logic in a select statement?


回答1:


To convert it to a valid time format you have to do some string manipulations like

cast( substr( right( '00' || '123456', 6) ,1,2) || ':' || substr( right( '00' || '123456', 6) ,3,2) || ':' || substr( right( '00' || '123456', 6) ,5,2) as time)

where 123456 is your decimal. This would work for your 4506 example as well. You could of cause also use a case statement if you want to avaoid adding the "00" each time.

For calculating the difference in minues there might be other calc options. You can check out the minutes_between function provided by Db2 11.1



来源:https://stackoverflow.com/questions/51080212/convert-db2-sql-decimal-to-time

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!