MySql arithmetic differs from Excel - mysql

I have plugged the same calculation into Excel and MySql and they are quite different.
Excel:
=(12^1.2299)/(12^1.16793+12^1.20053)*2.29761*11
MySql
select ((12^1.2299)/(12^1.16793+12^1.20053))*2.29761*11.0 AS Wrong
I believe Excel gives the correct answer of 14.14 while MySql gets 12.63.
I have put additional brackets and decimals in but it makes no difference,
Why is MySql so wrong?

You should be using the MySQL POWER() function instead of using the caret symbol. For example, instead of using 12^1.2299 you will want to use POWER(12,1.2299).

The ^ operator is a bitwise XOR in MySQL. You should use the POWER() function to calculate exponents instead of ^.
Example:
SELECT POWER(12,1.2299)/(POWER(12,1.16793)+POWER(12,1.20053))*2.29761*11.0 AS Correct
Returns 14.143896230490112

Related

Is it possible to select numeric characters only from a VARCHAR field in MySQL without the use of a custom function?

I'm importing data to a 3rd party CRM and the restrictions on the phone number field are tighter than those of the source database (MySQL). I would like to grab the first 10 numeric characters in the select. I don't have flexibility to sanitize the existing data. I can only work with what is there. All I can find in forums so far seems to be creating a custom function. Is a custom function the only way? The environment is MySQL 5.7.19.
Thanks in advance!
In MySQL 5.7, here's how I'd do it:
SELECT LEFT(mycolumn, 10)+0 FROM ...
The builtin function LEFT() uses only the first N characters.
The +0 makes sure to cast it to a number, just in case the first N characters include some non-numeric characters. Casting to a numeric in MySQL just uses the leftmost number characters, and when it reaches a non-numeric character, ignores the rest.
You can get rid of hyphens first, then use the result of that as I showed above:
SELECT LEFT(REPLACE(mycolumn, '-', ''), 10)+0 FROM ...

Format a string with a decimal separator between every 3 digits

In a MySQL database, I have a string like 123456789. I want to add a decimal separator between every 3 digits, and thus turn it into 123.456.789.
How can I do this?
If you are using MySQL 8.0+, it is possible to use the REGEXP_REPLACE function:
SELECT REGEXP_REPLACE(YOUR_COLUMN, '([:digit:]{3})(?!$)', '$0.');
It adds a dot for each group of 3 digits, excepted for the last one.
Here are some examples:
123456789 returns 123.456.789
1234567891 returns 123.456.789.1
12345678910 returns 123.456.789.12
You can try it out on this DB Fiddle and you can change the regular expression to fit what you need.
However, this approach is probably not the best in terms of performance. If your query is called a lot of times, your database server will suffer since calculation is centralized on it. As #xNoJustice said, it is better to handle this string operation in the client part, where it will be divided between every client execution.
Use FORMAT() function with suitable locale.
fiddle, which shows all suitable locales.

How to use regular expression in MySQL

I am using MySQL,I have table called constructions where there is a column called phone, phone numbers are in format 9876543210 but I want to clean the column wherever there are alphabets and symbols.
I was using:
select * from constructions where phone like '[a-Z]%';
but this is wrong, i guess it works in SQL-Server.
No to SQL Server unless you add a regular expression utility function. You have the built in find replace functions but those are very simple.
For MySQL look to the reference manual here >>> http://dev.mysql.com/doc/refman/5.0/en/regexp.html
Instead of LIKE use the RLIKE version that works with regular expressions.

Count Regular expression in mysql

I have to find how many numbers in the following String,
'~1~~2~~123~~~~12~~1~~~~' and the output should be 5.
Any help?
MySQL cannot do that directly. You would have to dig into UDF (User defined functions). It would allow you to create a function that returns the number of number substrings in a string.
Regular expression need pattern to match and there no pattern to use regular expression in provided example. You can go here to see regular expression in mysql.

Regexp - back references, translating code from PHP to MySQL

I have a regular expression that works in PHP, but not the MySQL REGEXP function.
'(.)\1{2,}'
In PHP this matches any char that is repeated 2 or more times, how can I translate this to work with the MySQL function.
Sorry, you can't. MySQL uses POSIX regex, which doesn't support back-references. If you must perform such matches in MySQL, your only option is to install a UDF such as lib_mysqludf_preg.