2016-04-06 32 views
1

我在表中的一個浮動場小時插入時間如下:浮動時間轉換在我的SQL

If 6.3 means 6 hours and 30 minutes 
If 8.04 means 8 hours and 4 minutes 

現在我想所有這些浮點值與時間格式

添加到另一列new_hours

我需要添加類似以下的條目:

If 6.3 in hours column then 06:30:00 in new_hours column 

If 8.04 in hours column then 08:04:00 in new_hours column 

我有很多時間列條目的,我想從添加new_hours小時由一個單一的我的SQL查詢。

任何幫助將appriciated.Thanks。

回答

2

你可以嘗試以下查詢

SELECT STR_TO_DATE(REPLACE(ROUND(YOUR_COL(i.e. 6.3),2), '.', ':'), '%h:%i %p') 

或者你也可以試試這個

SELECT MAKETIME(LPAD(SUBSTRING_INDEX('6.3', '.', 1),2,0),RPAD(SUBSTRING_INDEX('6.3', '.', -1),2,0),00) 
+0

哈哈...感謝的人:) –

+0

嗯。我更喜歡那個之前的那個。 – PerlDuck

+0

是的,但是這1是要達到所需的輸出... 0.3 30分鐘和0.03 3分鐘..:p –

1

有很多方法;我使用MySQL的sec_to_time ,它將一段時間內變成一個給定的秒數。

的第一步是你的花車轉換成秒(從午夜開始),然後把它包裝成sec_to_time

MariaDB [(none)]> select sec_to_time(cast(floor(8.04)*60*60 + ((8.04-floor(8.04))*100*60) as int)); 
+-----------------------------------------------------------------------------+ 
| sec_to_time(cast(floor(8.04)*60*60 + ((8.04-floor(8.04))*100*60) as int)) | 
+-----------------------------------------------------------------------------+ 
| 08:04:00                 | 
+-----------------------------------------------------------------------------+ 
1 row in set (0.00 sec) 


MariaDB [(none)]> select sec_to_time(cast(floor(6.3)*60*60 + ((6.3-floor(6.3))*100*60) as int)); 
+--------------------------------------------------------------------------+ 
| sec_to_time(cast(floor(6.3)*60*60 + ((6.3-floor(6.3))*100*60) as int)) | 
+--------------------------------------------------------------------------+ 
| 06:30:00                 | 
+--------------------------------------------------------------------------+ 
1 row in set (0.00 sec) 

而且我敢肯定有一個更簡單的方法來提取8.04.04 .. 。

update聲明將被:

update t set new_hours = 
    sec_to_time(cast(floor(hours)*60*60 
        + ((hours-floor(hours))*100*60) as int));