I want to convert feet and inches to centimeters format
Format in my DB is:
4'6" (4 feet, 6 inches)
Formula for converting into centimeters
4*30.48 = 121.92 (convert feet to centimeters = multiply by 30.48)
6*2.54 = 15.24 (convert inches to centimeters = multiply by 2.54)
So Result = 121.92 + 15.24 = 137.16 cm
eg:
Actual Table: inches
SELECT * FROM inches
id height
1 4'6"
2 4'7"
3 5'8"
4 5'9"
I expect the following result as centimeters when I do SQL query
id height
1 137.16
2 139.7
3 172.72
4 175.26
Thanks in advance :)
Probably far easier to do on the application level, but if you really had to, you could do it in SQL like this, using the SUBSTR and INSTR functions, and some basic math:
SET #height = '4''6"';
SELECT
SUBSTR(#height, 1, INSTR(#height, '''') - 1) * 12 * 2.54 +
SUBSTR(#height, INSTR(#height, '''') + 1, INSTR(#height, '"') - INSTR(#height, '''') - 1) * 2.54;
-- yields 137.16
Or, applied to your table structure:
SELECT id,
SUBSTR(height, 1, INSTR(height, '''') - 1) * 12 * 2.54 +
SUBSTR(height, INSTR(height, '''') + 1, INSTR(height, '"') - INSTR(height, '''') - 1) * 2.54 AS height
FROM inches;
Application side process will be better,
However,
SELECT
(CAST(SUBSTR(height,1, LOCATE("'",height)-1) AS UNSIGNED) * 30.48) +
(CAST(SUBSTR(height, LOCATE("'",height)+1) AS UNSIGNED) * 2.54 ) AS cm
FROM
inches;
Related
I'm using MySQL version: 5.7.22
I've got two columns Latitude and Longitude both in Degrees/Minutes/Seconds format that I want to convert to Decimal format ex: 48° 52.250' N to 48.93611111
I have the following script but I'm stuck at how to split the degrees minutes and seconds. I cannot hard-code the values as I've done here left(Latitude,2) since the degrees might have 3 decimals as well
SELECT Latitude,
left(Latitude, 2) +
(TRUNCATE((Latitude - TRUNCATE(Latitude)) * 100) / 60) +
(((Latitude * 100) - TRUNCATE(Latitude * 100)) * 100) / (60 * 60) AS DECIMAL_DEGREES
FROM small_ocean_data
The formula for the conversion is this: D + M/60 + S/3600 * -1 if direction in ['W', 'S'] else 1
Any help would be grateful!
Well if I use this formula of yours: D + M/60 + S/3600
Where I believe D is Degrees and M are Minutes and S are Seconds.
With this select:
select SUBSTRING_INDEX('48° 52.250', '°',1) +
(SUBSTRING_INDEX('48° 52.250',' ',1)/60) +
(SUBSTRING_INDEX('48° 52.250','.',1)/3600);
Or for your database:
select SUBSTRING_INDEX(Latitude, '°', 1) +
(SUBSTRING_INDEX(Latitude ,' ',1)/60) +
(SUBSTRING_INDEX(Latitude ,'.',1)/3600) "DECIMAL_DEGREES"
FROM small_ocean_data;
I get 48.81333333333333
48 + 0.8 + 0.013333333333333334
Here is the DEMO
I am trying to generate a random value between 0.01 - 0.50 to enter into mysql. I have 2.7 million rows that I need to execute this on.
Here is my script:
UPDATE FBAInventory SET buyBox = ROUND( 0.01 + RAND( ) * 8,2 );
It is generating values such as 4.20, 3.89 etc. I only want it to span from 0.01 - 0.50 and not to exceed this.
Does anyone know how to do this?
Thanks!
How about...
round(rand() * 0.49 + 0.01, 2);
You can use the floor function to generate a range of random numbers.
FLOOR(RAND() * (<max> - <min> + 1)) + <min>
where the max and min are inclusive. So in your case you would want
FLOOR(RAND() * 1.49 ) + 0.01
I have a MySQL table which looks like this:
id load_transit load_standby hours_transit hours_standby
1 40 20 8 4
2 30 15 10 10
3 50 10 3 9
I need to do the following calculations:
(intermediate calculations)
hours_transit_total = 8+10+3 = 21
hours_standby_total = 4+10+9 = 23
(desired result)
load_transit_weighted_mean = 40*(8/21) + 30*(10/21) + 50*(3/21) = 36.667
load_standby_weighted_mean = 20*(4/23) + 15*(10/23) + 10*(9/23) = 13.913
Is it possible to do this in a single query? What would the best design be?
Note that
40*(8/21) + 30*(10/21) + 50*(3/21) =
(40*8)/21 + (30*10)/21 + (50*3)/21 =
(40*8 + 30*10 + 50*3)/21
and
20*(4/23) + 15*(10/23) + 10*(9/23) =
(20*4)/23 + (15*10)/23 + (10*9)/23 =
(20*4 + 15*10 + 10*9)/23
Which allows you to get the results you want using
SELECT sum(hours_transit * load_transit) / sum(hours_transit),
sum(hours_standby * load_standby) / sum(hours_standby)
FROM your_table
I just had this same question and built this little query I think makes it clear how to find the weighted average in a single query:
select sum(balance), sum(rate * balance / 5200) as weighted_rate, -- what I want
-- what you cannot do: sum(rate * balance / sum(balance))
sum(balance * rate) / sum(balance) as weighted_rate_legit -- ah thank you transitive math properties
from (
select '4600' as balance, '2.05' as rate from dual
union all
select '600' as balance, '2.30' as rate from dual
) an_alias;
Can I ask for help on a SQL Statement please, I have to do the calculation inline and cannot declare variables for it
Calculation:
-91000000 / 2700000 = -33.7037037037
I need the remainder (7037037037 - but only up to 6 characters ) to be multiplied by 15000
703703 / 15000 = Final Answer of 49.913533
I thought I could do this:
select cast(ParseName(abs(cast(-91000000 as decimal)/ 2700000 ) %1,1) as numeric(8,8)) / 15000
WITH cte AS
(
SELECT -91000000 AS x, 2700000 AS y
)
SELECT ABS(ROUND((CAST(x AS decimal) / CAST(y AS decimal)) - (x/y), 6)) * 1000000 / 15000 FROM CTE
I have a field on my table which represents seconds, I want to convert to minutes
Select (100/60) as Minute from MyTable
-> 1.66
How can I get 1 minute and 40 seconds 00:01:40 and then round to 00:02:00 and if 00:01:23 round to 00:01:30
Using Mysql.
There are two ways of rounding, using integer arithmetic and avoiding floating points, a value to the nearest thirty seconds...
((seconds + 15) DIV 30) * 30
(seconds + 15) - (seconds + 15) % 30
The latter is longer, but in terms of cpu time should be faster.
You can then use SEC_TO_TIME(seconds) to get the format hh:mm:ss, and take the right 5 characters if you really need hh:mm.
If you wanted to avoid SEC_TO_TIME(seconds), you can build up the string yourself.
minutes = total_seconds DIV 60
seconds = total_seconds % 60
final string = LPAD(minutes, 2, '0') | ':' | LPAD(seconds, 2, '0')
i am not sure about how to round it but you can convert seconds into time i.e hh:mm:ss format using SEC_TO_TIME(totaltime)
Desired result :
A = 30
B = 60
C = 90
D = 120
select
(25 + 15)-(25 + 15) % 30 as A,
(32 + 15)-(32 + 15) % 30 as B,
(90 + 15)-(90 + 15) % 30 as C,
(100 + 15)-(100 + 15) % 30 as D
Result :
A = 30
B = 30
C = 90
D = 90
I try with this:
select
30* ceil(30/30) as A,
30* ceil(32/30) as B,
30* ceil(90/30) as C,
30* ceil(100/30) as D
Result :
A = 30
B = 60
C = 90
D = 120
Thank you for your help !
You can simply write your own function http://dev.mysql.com/doc/refman/5.0/en/create-procedure.html
But I'd rather do that in a programing language (PHP, Python, C), not on the database side.