MySQL - Search for entries with 3 decimal places - mysql

I have a table with 7.5 million entries, and while importing some of the data a few of the column breaks messed up and the first digit of a column ended up stuck onto the end of the previous column.
For example, on a row it should say ELWS=123.44 and t2=17.00, and instead it read in ELWS=123.441 and t2=7.00.
This only happened in a few places.
Is there some way to search for the entries where ELWS ended up with 3 decimal places? Also, all fields are double type.

SELECT * FROM someTable
WHERE (ELWS * 1000) % 10 != 0
SQLFiddle here

Related

mysql find rows with fractional part of number

Using the MySQL command prompt, I want to SELECT all rows conataining a specific fractional part of number.
I have a field of type FLOAT called priority & I want to find all rows where the priority is X.2 where X can be any whole number.
Is there a way to use the MOD or FLOOR function to extract the ".2" in a query?
I've tried:
SELECT * from table
WHERE priority-FLOOR(priority) = .2 *(note the decimal point before the 2)*
but it returns empty set when I know there are at least 100 rows containing a priority of X.2
Thanks
What about using the LIKE clause in MySQL?
SELECT * FROM table WHERE priority LIKE %.2%
Floating point numbers are notoriously fickle to work with. That is why SQL offers the decimal data type, which is fixed length.
Do what you want with between:
SELECT *
from table
WHERE priority-FLOOR(priority) between 0.15 and 0.25
Consider storing the priority as a decimal instead of a floating point number.
If you have "deeper" priorities, like 1.22 that you are trying to avoid, then do:
WHERE priority-FLOOR(priority) between 0.195 and 0.205
The range can be even narrower, if you need.

Is there a possibility to change the order of a string with numeric value

I have some strings in my database. Some of them have numeric values (but in string format of course). I am displaying those values ordered ascending.
So we know, for string values, 10 is greater than 2 for example, which is normal. I am asking if there is any solution to display 10 after 2, without changing the code or the database structure, only the data.
If for example I have to display values from 1 to 10, I will have:
1
10
2
3
4
5
6
7
8
9
What I would like to have is
1
2
3
4
5
6
7
8
9
10
Is there a possibility to ad an "invisible character or string which will be interpreted as greater than 9". If i put a10 instead of 10, the a10 will be at the end but is there any invisible or less visible character for that.
So, I repeat, I am not looking for a programming or database structure solution, but for a simple workaround.
You could try to cast the value as an number to then order by it:
select col
from yourtable
order by cast(col AS UNSIGNED)
See SQL Fiddle with demo
You could try appending the correct number of zeroes to the front of the data:
01
02
03
..
10
11
..
99
Since you have a mixture of numbers and letters in this column - even if not in a single row - what you're really trying to do is a Natural Sort. This is not something MySQL can do natively. There are some work arounds, however. The best I've come across are:
Sort by length then value.
SELECT
mixedColumn
FROM
tableName
ORDER BY
LENGTH(mixedColumn), mixedColumn;
For more examples see: http://www.copterlabs.com/blog/natural-sorting-in-mysql/
Use a secondary column to use as a sort key that would contain some sort of normalized data (i.e. only numbers or only letters).
CREATE TABLE tableName (mixedColumn varchar, sortColumn int);
INSERT INTO tableName VALUES ('1',1), ('2',2), ('10',3),
('a',4),('a1',5),('a2',6),('b1',7);
SELECT
mixedColumn
FROM
tableName
ORDER BY
sortColumn;
This could get difficult to maintain unless you can figure out a good way to handle the ordering.
Of course if you were able to go outside of the database you'd be able to use natural sort functions from various programming languages.

sorting numbers stored in text field in MYSQL database

i have a mySQL database with a text field in which is stored a number.
i need to produce a recordset sorted in descending numerical order.
this works fine until we get to numbers greater than 10 ie
9
8
7
6
5
4
3
2
10
1
is there a simple way of sorting this 'correctly' ?
(yes, i know i should have numbers in a numerical field, but i'm working with what i have :))
i'm using the results on an asp/vbscript/jquery page so maybe even a client-side solution is viable...
any suggestions?
ORDER BY ABS(text_column) DESC
Or, if you also have to deal with negative values:
ORDER BY CAST(text_column AS SIGNED) DESC
You need to type cast it to INTEGER using CAST function in MySQL:
ORDER BY CAST(text_column AS UNSIGNED INTEGER)
Try this one -
... ORDER BY text_column * 1

mysql pdo get range of rows

i have a table with about 100 rows , and i want every time to get rows between number and number , like this
if `i=1` i want to get the rows `0 1 2 3 4`
if `i=2` i want to get the rows `5 6 7 8 9`
if `i=3` i want to get the rows `10 11 12 13 14`
and maybe the last value of i will just take 3 or 4 rows not 5
i think the solution will be something like this
select * from question limit (i-1)*5 , i*5-1
but doesn't work , cos i don't know how to use variable in a query , and when i tried it for i=1 it doesn't work and i got a syntax error
The first parameter to LIMIT is the zero-based starting row index. The second one is the number of rows. So, if you're always looking for five rows:
SELECT * FROM question LIMIT (i-1)*5 , 5
(You'll have to calculate (i-1)*5 with whatever language you're using to build the query, and pass an actual number to MySQL).

Need MySQL Query to Chop String and add Serial Number (letter) to End, Where Duplicates

Let's say we have some 10 character skus like this:
AB1234ZYXW
AB1234ZYXN
AB1234ZYXP
AB1234ZYXR
ZZ1234ZYXR
But we need them to be 8 characters. Chopping them at 8 would make them non unique (except the last one).
The non-unique ones would all look like : AB1234ZY
So my solution is to chop one more character off of all them, giving AB1234Z, then adding a serial number (actually serial letter). AB1234ZA, AB1234ZB, ...C ...D.
My first thought was to query the DB and do all the processing in PHP arrays, then send queries back to update. But since there can be 30,000 to process at a time, this will result in 30,000 UPDATE queries (one for each chopped sku anyway).
If it could be done with a single MySQL statement it would be much faster.
Any ideas?
EDIT:
To add more detail:
Total number of records could be 2,000 - 35,000 per batch. With the chopping, it will create groups of duplicates. If each group has less than 26 members, then 1 digit of serialization is enough. Otherwise 2 digits (26 x 26 = 676 and it's very unlikely a group would be larger than that). Ideally, the query would take into account the number of duplicates in each group and apply 1 or 2 digits of serialization depending. I know it's a lot to ask. jonstjohn's answer looks like a good start. I will test it tomorrow. I haven't used mysql variables for anything yet but it looks promising.
Try the following:
SET #num = 64;
UPDATE skus SET sku = concat(left(sku, 7),
char(if((#num := #num + 1) <= 90, #num, #num := 65)));
Here's the explanation.
The first line assigns the integer 64 to the mysql variable #num. The character code for 'A' is 65 and 'Z' is 90, so we assign one before the 'A' since it will be incremented in the update query.
The update query then updates each row of the skus table using the first 7 characters of the sku, plus an incrementing character (A-Z) using the #num variable. When it hits 'Z', it resets itselft to 65 (and uses that value for that row).
It should be very fast and efficient.