Just asking if this is possible:
I have a mysql database 'data' and a column 'count' which contains numbers such as
3741
49215
345
4686794
I was wondering if there is a possibility to remove the last figure from each number in this column, so there will be these values:
374
4921
34
468679
(for 100,000 rows so i can't do it manually :) )
Thank you!
This will work if the field is numeric or string and is always > 0:
UPDATE `table` SET `count` = FLOOR(`count`/10)
Note single digit values will become 0
If some numbers are negative, it will not give the right answer and a substring approach like the other answers is better.
Use the MySQL SUBSTRING function to extract portion of a string. Use CHAR_LENGTH function to calculate the number of characters in the string.
SELECT
col,
SUBSTRING(col, 1, CHAR_LENGTH(col) - 1) AS col_trimme
FROM tbl
is the data type is string you can
update my_table
set my_column = substr(my_column, 1, length(my_column) -2)
if is an int you can cast
update my_table
set my_column = cast(substr(cats(my_column AS varchar(16)), 1, length(my_column AS varchar(16)) -1), AS INT)
This will work if the field is numeric or string:
UPDATE table SET count = SUBSTRING(count, 1, CHAR_LENGTH(count) - 1);
Related
I have a database table called druginfo. It contains prices in WSprice column. Type of WSprice column is Double. This means It can contain like 23.5698 values. But I want to show all the values in the column WSprice 2 decimal places rounded like 23.57. How to apply that to all values in the column? Help me to do this.
If you only want to display your DOUBLE column to 2 decimal places, you can use the ROUND function:
SELECT ROUND(column_name, 2)
FROM your_table
This will display a value of 23.5698 as 23.57 in the result set.
If you want to change the format of the entire column you can use this:
ALTER TABLE your_table MODIFY column_name DECIMAL(9, 2)
I think we need to use CAST() instead of ROUND().
The reason behind is ROUND() return decimal values when decimal value exists in the database.
Example:
SELECT ROUND(columnName, 2)
FROM tableName
if columnName = 10.5 Output will be like 10.50
But if columnName = 10 Output will be like 10
And CAST() will return decimal value. But we need DECIMAL with CAST
SELECT CAST(columnName AS DECIMAL(10,2))
FROM tableName
The output will be like 10.00
In a MySQL table i have a field, containing this value for a given record : "1908,2315,2316"
Here is my sql Query :
SELECT * FROM mytable WHERE 2316 IN (myfield)
I got 0 results!
I tried this :
SELECT * FROM mytable WHERE 2315 IN (myfield)
Still 0 results
And then i tried this :
SELECT * FROM mytable WHERE 1908 IN (myfield)
Surprisingly i obtained the record when searching with 1908! What should i do to also obtain the record when searching with 2315 and 2316 ? What am i missing ?
Thanks
You appear to be storing comma delimited values in a field. This is bad, bad, bad. You should be using a junction table, with one row per value.
But, sometimes you are stuck with data in a particular structure. If so, MySQL provides the find_in_set() functions.
SELECT *
FROM mytable
WHERE find_in_set(2316, myfield) > 0;
You can't use IN() over comma separated list of no.s its better to normalize your structure first for now you can use find_in_set to find results matching with comma separated string
SELECT * FROM mytable WHERE find_in_set('1908',myfield) > 0
This question has been asked and answered before, but I don't want to hunt for it; this question should be closed as a duplicate. But, to answer your question:
The commas in the string, the column value, are just characters. Those are part of the string. They aren't seen as "separators" between values in the SQL text. The way SQL sees it, the column contains a single value, not a "list" of values.
So, in your query, the IN (field) is equivalent to an equals comparison. It's equivalent to comparing to a string. For example:
... WHERE 2316 = '1908,2315,2316'
And those aren't equal, so the row isn't returned. The "surprisingly" finding of a match, in the case of:
... WHERE 1908 IN ('1908,2315,2316')
that's explained because that string is being evaluated in a numeric context. That is, the comparison returns true, because all of these also true:
... WHERE 1908 = '1908,2315,2316' + 0
... WHERE 1908 = '1908xyz' + 0
... WHERE 1908 = '1907qrs' + 1
(When evaluated in a numeric context, a string gets converted to numeric. It just happens that the string evaluates to a numeric value that equals the integer value it's being comparing to.)
You may be able to make use of the MySQL FIND_IN_SET function. For example:
... WHERE FIND_IN_SET(2316,'1908,2315,2316')
But, please seriously reconsider the design of storing comma separated list. I recommend Bill Karwin's "SQL Antipatterns" book...
http://www.amazon.com/SQL-Antipatterns-Programming-Pragmatic-Programmers/dp/1934356557
In mysql IN clause is utilized as
SELECT * FROM mytable WHERE column_name IN (set_of_values) ;
Mention column name instead of values
Please try
SELECT * FROM mytable WHERE LOCATE(CONCAT (',', 2316 ','), CONCAT (',',myfield,',' ) ) <>0
I have a column in a table with data type of tiny int. Its having values like 1 and 0. I tried a select query as
SELECT * from table_name where filed_name = 'Y';
This results me all records which is having zero in it. I didn't understand how query execution happens. Please help me to understand this
When the string is converted to a number, it becomes the value 0.
Thats why your are getting all records with value 0
SELECT * from table_name where filed_name = 'Y';
here 'Y' automatically converted to 0
Use instead
SELECT * from table_name where filed_name = 1;
TINYINT does not store strings values like 'Y' or 'N', it stores integers 0 or 1. If you want all datarows with value 'Y' you have to filter by 1 (integer) example:
SELECT * FROM myTable WHERE myTinyColumn = 1
I have an events table with a field called breaks. This is populated with data in a comma separated format, i.e. 1,2,3 or 1 or 1,4,5 - the same format that MySQL's IN command uses.
I'd then like to run a query - on the slots table - to return all rows apart from those specified in events.breaks.
The query, theoretically, should be something like this:
SELECT
`slots`.`id` AS id,
RIGHT(`slots`.`time`, 8) AS `time`
FROM
`slots`, `event`
WHERE
`slots`.`id` NOT IN (`event`.`breaks`)
But that doesn't appear to work - if event.breaks is 4,5,7, the only row from the slots table that doesn't return is 4!
SQLFiddle here: http://sqlfiddle.com/#!2/913fe/1/0
You're passing a single field to the NOT IN () clause, not a subexpression. Think of it like this
(1, 2, 3)
is roughly the same as
SELECT 1
UNION
SELECT 2
UNION
SELECT 3;
as a subexpression. What you're doing instead is
('4,5,7')
which is roughly equivalent to
SELECT '4,5,7';
which in turn MySQL probably converted to a number for the comparison and the result is
NOT IN (4)
What you're actually trying to do isn't really supposed to be done like that. It'd be better if you added an AxB relation table so you can select several rows with the IDs you don't want.
Give this a try:
SELECT slots.id AS id, RIGHT(slots.time, 8) time
FROM slots, event
WHERE FIND_IN_SET(slots.id, event.breaks) = 0
This is how the FIND_IN_SET(str,strlist) function works:
Returns a value in the range of 1 to N if the string str is in the string list strlist consisting of N substrings. A string list is a string composed of substrings separated by “,” characters. [...] Returns 0 if str is not in strlist or if strlist is the empty string.
Also note that IN (val1, val2, val3) is NOT the same as IN (val4) where val4 is a commma-separated string. The IN clause will compare by equality.
you may need a subselect to return the split string
... NOT IN (SELECT your_split_fnc(`event`.`breaks`) FROM `events`)
See answers here for a way to split strings in MySQL Can Mysql Split a column?
instr() MySQL function could be of help also
... INSTR(event.breaks,id) = 0
http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_instr
Is it possible to convert text into a number within MySQL query? I have a column with an identifier that consists a name and a number in the format of "name-number". The column has VARCHAR type. I want to sort the rows according to the number (rows with the same name) but the column is sorted according to do character order, i.e.
name-1
name-11
name-12
name-2
If I cut off the number, can I convert the 'varchar' number into the 'real' number and use it to sort the rows? I would like to obtain the following order.
name-1
name-2
name-11
name-12
I cannot represent the number as a separate column.
edited 2011-05-11 9:32
I have found the following solution ... ORDER BY column * 1. If the name will not contain any numbers is it safe to use that solution?
This should work:
SELECT field,CONVERT(SUBSTRING_INDEX(field,'-',-1),UNSIGNED INTEGER) AS num
FROM table
ORDER BY num;
You can use SUBSTRING and CONVERT:
SELECT stuff
FROM table
WHERE conditions
ORDER BY CONVERT(SUBSTRING(name_column, 6), SIGNED INTEGER);
Where name_column is the column with the "name-" values. The SUBSTRING removes everything up before the sixth character (i.e. the "name-" prefix) and then the CONVERT converts the left over to a real integer.
UPDATE: Given the changing circumstances in the comments (i.e. the prefix can be anything), you'll have to throw a LOCATE in the mix:
ORDER BY CONVERT(SUBSTRING(name_column, LOCATE('-', name_column) + 1), SIGNED INTEGER);
This of course assumes that the non-numeric prefix doesn't have any hyphens in it but the relevant comment says that:
name can be any sequence of letters
so that should be a safe assumption.
Simply use CAST,
CAST(column_name AS UNSIGNED)
The type for the cast result can be one of the following values:
BINARY[(N)]
CHAR[(N)]
DATE
DATETIME
DECIMAL[(M[,D])]
SIGNED [INTEGER]
TIME
UNSIGNED [INTEGER]
You can use CAST() to convert from string to int. e.g. SELECT CAST('123' AS INTEGER);
SELECT *, CAST(SUBSTRING_INDEX(field, '-', -1) AS UNSIGNED) as num FROM tableName ORDER BY num;
one simple way SELECT '123'+ 0
cast(REGEXP_REPLACE(NameNumber, '[^0-9]', '') as UNSIGNED)
To get number try with SUBSTRING_INDEX(field, '-', 1) then convert.
if your primary key is a string in a format like
ABC/EFG/EE/13/123(sequence number)
this sort of string can be easily used for sorting with the delimiter("/")
we can use the following query to order a table with this type of key
SELECT * FROM `TABLE_NAME` ORDER BY
CONVERT(REVERSE(SUBSTRING(REVERSE(`key_column_name`), 1, LOCATE('/', REVERSE(`key_column_name`)) - 1)) , UNSIGNED INTEGER) DESC
I found it easier to use regex_replace function to strip off all non numeric values from the field and then sort.
SELECT field , CONVERT(REGEXP_REPLACE(field,'[^0-9]',''),UNSIGNED) AS num FROM your_table ORDER BY num;
select
`a`.uuid,
concat('1',REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(`a`.uuid,'-',''),'b','11'),'c','12'),'d','13'),'e','14'),'f','15'),'a','10')),
A generic way to do :
SELECT * FROM your_table ORDER BY LENTH(your_column) ASC, your_column ASC