select where in(Float, .....) - mysql

I am trying to select some values from mysql with the next sentence:
SELECT
id, lead_id, form_id, field_number, value
FROM
wp_rg_lead_detail
WHERE
field_number IN (6.3, 6.6, 11, 12, 17, 14)
ORDER BY
lead_id, field_number
the field field_number is a float and the select is listing just the 11, 12, 17 and 14 fields but not values from the 6.3 and 6.6
What is wrong with the sentence?
Thanks.

The reason is that 6.3 and 6.6 are not actually stored in your table. Computers cannot perfectly represent most fractional values. When the value stored is compared to 6.3 or 6.6, it does not match, even though it looks like it should. The difference might be at the 9th or 10th decimal place, which you won't normally see.
http://dev.mysql.com/doc/refman/5.0/en/problems-with-float.html
If you need to exactly compare the values, make them integers or decimals. If nothing will ever have more than 1 number to the right of the decimal point, you can multiply all values by 10 to only work with integers, for example.

There is nothing wrong with the query. The problem is that float is not precise type. Use decimal instead (or change the query to something like ... WHERE (field_number BETWEEN 6.2999 AND 6.3001) OR (field_number BETWEEN 6.5999 AND 6.6001 OR ()... )

Related

MySQL STR_TO_DATE Problem while using this function

As title, I'm trying to convert a VARCHAR column in a DATE column, and data is populated in that format "DDMMYYYY" ex. XMAS is "25122022" and in this case the correct formula should be STR_TO_DATE(column, '%d%m%Y')Well, when I execute this query I get an error since in some cases I have values with a "missing" char, I mean, for example, "1012023" when the day is <10 the query fails, cause it checks for "01122023" instead.I could solve this easily by adding a 0 to all fields having length 7, but I'd like to make it more clean.Reading better the usage of STR_TO_DATE I noticed that I could replace %d with %e since the second choice should theorically consider days from 0 to 31 instead of 01 to 31.Unexpectedly the query didn't work and gave me the same erorr at the first instance of a length 7 string.Am I doing something wrong?Thanks in advance.
We can try left padding your date string with zero to a length of 8:
WITH yourTable AS (
SELECT '1012023' AS dt
)
SELECT STR_TO_DATE(LPAD(dt, 8, '0'), '%d%m%Y') AS dt_out -- 2023-01-01
FROM yourTable;
Demo

SUBSTRING_INDEX Not Warking in Mysql

I am trying to find max invoice:
SELECT IFNULL(MAX(SUBSTRING_INDEX(invoice,'I', -1)) + 1, 1) AS invoice
FROM sales
SQL Fiddle
When I run this SQL query, it can not count more than 10.
invoice
20221026P1I1
20221026P1I2
20221026P1I3
20221026P1I4
20221026P1I5
20221026P1I6
20221026P1I7
20221026P1I8
20221026P1I9
20221026P1I10
20221026P1I11
20221026P1I12
I am trying to find max invoice 12 + 1 = 13
Your use of SUBSTRING_INDEX() is correct, however you should cast the string value to a bona fide integer:
SELECT COALESCE(MAX(CAST(SUBSTRING_INDEX(invoice, 'I', -1) AS UNSIGNED)), 1) AS invoice
FROM sales;
The problem with trying to find the max of the text substrings themselves is that text numbers sort lexicographically, e.g.
1
10
11
2
23
But this isn't the behavior you want, you want the numeric maximum. Hence we should cast these substrings and then compare.
Side note: You could have avoided this problem entirely by maintaining a pure numeric invoice number column. You may want to change your table design to include such a column.

How do you round floats conditionally?

I am writing a query that is used by report generating software.
Part of this is querying for the hours needed to complete a project. We record this a 2 decimal float so that we can estimate to the quarter hour.
However, if we are using it in our report and the hour we recorded is something like 8.00, I want to query it and format it so that 8.00 is just 8. However any hours with something past the decimal, like 8.25, should remain as 8.25. How can I make this work?
hours Queried Result
====== -> My Query -> ==============
8.00 8
8.25 8.25
I am using MySQL 5.6
You can use the REPLACE() function to remove .00:
REPLACE(hours, '.00', '') AS hours
You can convert it to a string and check the rightmost 2 characters and trim those if they are '00'.
SELECT TRIM(TRAILING '.00' FROM CAST(column_name AS VARCHAR));
SELECT REPLACE(Round(8.00), '.00', ' ');
I will give more example so you can clear your Logic:
MySQL ROUND() rounds a number specified as an argument up to a number specified as another argument.
Syntax:
ROUND(N,[D]);
Where 'N' is rounded up to D decimal places.
and 'D' is indicating up to how many decimal places N will be rounded.
Example 1:-
SELECT ROUND(4.43);
Output :-
4
The above MySQL statement will round the given number 4.43. No decimal places have been defined, so the default decimal value is 0.
Example 2:-
SELECT ROUND(-4.53);
Output:-
-5
The above MySQL statement will round the given number -4.53. No decimal places have been defined, so the default decimal value is 0.

how to update a VARCHAR column value with RegEx?

I need to update the values from a VARCHAR column in a MySQL database from YYMMDDSXXXXX to YYMMDDSXXXX, where YY is year (i.e. 11 for 2011), MM is month (i.e. 09 for September), DD is day (i.e. 15), S is an one-digit order number (1 to 0) and XXXXX is a sequential number from 00001 to 99999.
I need to reduce ten-fold the sequential number, which should go from 0001 to 9999.
I thought about something like:
update TABLE_NAME set FIELD_NAME = replace(FIELD_NAME, ‘find this string’, ‘replace found string with this string’);
But I'm not very good with MySQL, so I'm not sure how to do it. Can someone help?
Thanks in advance!
Regex replace can be implemented easily using this:
https://launchpad.net/mysql-udf-regexp
The regex you will need (in perl/posix std notation e.g. with sed)
s/([0-9]{7})0-9/$1$2/g
That is assuming you want to shorten the counter from the left (most significant) to keep the counts you have already unique (and obviously if you don't need that many places you want to remove the useless - most significant - bit).
That should get you well on your way, have fun :)

SQL query for sorting numeric fields

I have one column of the type VARCHAR which stores numbers like 10, 11, 16.5, 24, 43, 12, 100, etc.
I want to order these fields but it sorts like this:
10
11
12
16.5
100
24
43
And I'd like this result:
10
11
12
16.5
24
43
100
How can I do this?
VARCHAR is a string type, so it's ordering them alphabetically, which is the expected behavior. If possible you should change the column to a number type. If that's not possible you can cast the value to a number like so:
SELECT ... ORDER BY CAST(column_name AS DECIMAL)
However, this will impact your performance if you have a lot of rows in your database.
order by to_number(mycol)
this will of course give an error if one value is not numeric. which begs the question - why not make it a numeric column in the first place.
You want to cast the field to be a numeric value instead, so that MySQL will order it with a numeric comparison, rather than a string comparison.
SELECT your_column
FROM your_table
ORDER BY CAST(your_column AS DECIMAL) ASC
The above will work with negative numbers too, if you have any of those in your table.
Although if the field only contains numeric data, you really should be storing it as such, rather than as a varchar. Having a cast in your query will dramatically affect performance as you get more rows.
Just add zero to your field without any cast or convert:
order by 0+yourfield ASC