Convert hours to minutes in SQL

I am trying to write a regex that converts hours to minutes. I currently have a value that gives me the number of hours to a point and the number of minutes after a point, for example 6.10 is 6 hours and 10 minutes.

Is it possible to use a regular expression to find the value before the point and multiply by 60 (to get the minutes), and then add the value after the point (which is already in minutes)?

So, as in the previous example, this would do: (6 * 60) + 10

Thank!

+4
source share
3 answers

, , . , - INDEX, SUBSTR, .

:

 select INDEX([yourstring], '.') 

( ):

 select SUBSTR([yourstring], 1, INDEX([yourstring], '.')  - 1)

( ):

 select SUBSTR([yourstring], INDEX([yourstring], '.') + 1) 
+1

"6.10" , :

SET @Input = CAST(@InputString AS FLOAT)

Modulus .

= * 100 + * 60.

, 6.10 @Input,

SET @Output = (@Input % 1) * 100 + FLOOR(@Input, 0)  * 60

, - :

SELECT (Input % 1) * 100 + FLOOR(Input, 0) * 60 Output 
FROM   Table

, , . 6 = 6,05. (6 = 6,5), , under.

+2

There is a function STRTOKto tokenize the string, and then simply:

select '12.34' as x, 
    cast(strtok(x,'.',1) as int) * 60 + cast(strtok(x,'.',2) as int)
+2
source

Source: https://habr.com/ru/post/1661644/


All Articles