mysql - Subdividing a string and using substrings as fraction components -


i'm trying return set of string values mysql query. separate them @ '/' character (which contain). , divide value before '/' character, value follows it.

i've tried casting substring int , several decimal variations. gratefully accepted.

        "         cast(             cast(                 substring(                     customerfeedback.total_score,                      0, locate('/', table.column_a-1)                 )as decimal(6,3)             )             /             cast(                 substring(                     customerfeedback.total_score,                     locate('/', table.column_a+1), length(table.column_a)                 )as decimal(6,3)             )             decimal(6,3)         )         " 

use substring_index extract numerator , denominator parts , use them in division.

example:

select    @str:='225/12',    @n:=substring_index( @str, '/', 1 ) numerator,    @d:=substring_index( @str, '/', -1 ) denominator,    @n/@d quotient; 

result of above example:

+----------------+-----------+-------------+----------+ | @str:='225/12' | numerator | denominator | quotient | +----------------+-----------+-------------+----------+ | 225/12         | 225       | 12          |    18.75 | +----------------+-----------+-------------+----------+ 

you can cast or round of quotient desired.


Comments

Popular posts from this blog

windows - Single EXE to Install Python Standalone Executable for Easy Distribution -

c# - Access objects in UserControl from MainWindow in WPF -

javascript - How to name a jQuery function to make a browser's back button work? -