How do I remove .0 from a db table column? - mysql

I converted an Excel file into a SQL file. But, when I imported it into my database. It added .0 at the end of some values. The column type is varchar. How can I update the column values to remove .0 from all the string values?
For example:
Imported Values
12345.0
A119B
65489.0
BD585
123.0
124.0
12.0
Desired Result
12345
A119B
65489
BD585
123
124
12

If the decimal value is not certain, you should consider using SUBSTRING_INDEX() ; either in SELECT or UPDATE. Now you're just showing example of .0 but what if it has more variation than just .0? I assume at least that the unwanted value is always going to be after . so:
SELECT SUBSTRING_INDEX(val,'.',1) FROM mytable;
result:
+----------------------------+
| SUBSTRING_INDEX(val,'.',1) |
+----------------------------+
| 12345 |
| A119B |
| 65489 |
| BD585 |
| 123 |
| 124 |
| 12 |
+----------------------------+
Use same function for the update:
UPDATE mytable
SET val=SUBSTRING_INDEX(val,'.',1)
WHERE val LIKE '%.%';
Demo fiddle

UPDATE table SET colName = REPLACE(colName, ".0", "")
Credit goes to: #WOUNDEDStevenJones
Note: change table with your table name and colName with your column Name.

Related

Looping through an array in SQL column

I have a SQL table that looks something like this:
| ID | Value |
| --- | ----------------------------------------------------- |
| 1 | {"name":"joe", "lastname":"doe", "age":"34"} |
| 2 | {"name":"jane", "lastname":"doe", "age":"29"} |
| 3 | {"name":"michael", "lastname":"dumplings", "age":"40"}|
How can I using SQL select function, select only the rows where "age" (in value column) is above 30?
Thank you.
The column Value as it is it contains valid JSON data.
You can use the function JSON_EXTRACT() to get the the age and convert it to a numeric value by adding 0:
SELECT *
FROM tablename
WHERE JSON_EXTRACT(Value, "$.age") + 0 > 30;
See the demo.

WEBI find duplicate rows (with exception of certain columns) and highlight

consider the following table:
+------+------+----------+----------------+
| Col1 | Col2 | Col3 | Numeric Column |
+------+------+----------+----------------+
| ValA | ABC | Value 3 | 101 |
| ValF | DEF | Value 10 | 101 |
| ValC | DEF | Value 10 | 101 |
| ValB | GHI | Value 12 | 103 |
+------+------+----------+----------------+
I would like to find duplicate rows by comparing values across multiple columns, and highlight the values in the [Col1] column when duplicate rows are found (OR highlight the whole row, whatever is easier). So in the above table I would like to compare values of the [Col2], [Col3] and [Numeric Column] columns.
And in this example, ValF and ValC in [Col1] would be highlighted. I am not sure how to go about this.
I figured it out, see steps below.
Create a dimension variable and concatenate all the columns you like to compare into a variable.
Variable name: Concat_col
​Variable formula: =[Col2]+[Col3]+[Numeric Column]
Create a measure variable and refer to the first variable.
​Variable name: Count_col
​​Variable formula: =RunningCount(NoFilter([Concat_col]);([Concat_col]))
Create a measure variable and refer to the first and second variable.
Variable name: Max_col
Variable formula: =Max(NoFilter([Count_col])) In ([Concat_col])
Now create a Formatting rule:
And populate as follow:
To apply to a whole row, select the columns one by one and click Formatting Rules > 'Conditional Format' (this is the name of the formatting rule we just created earlier).

How to update specific value without updating a whole value in MySQL

I have a table like this
+-----+------------------+
| id | name |
+-----+------------------+
| 1 | John;Black;Mike |
+-----+------------------+
| 2 | White;Mike;John |
+-----+------------------+
| 3 | Jacob;Mike |
+-----+------------------+
| 4 | Will;Mason;Mike |
+-----+------------------+
as result of
SELECT * FROM people WHERE name LIKE '%Mike%';
Is there any query on how to update specific name Mike to Michael without updating a whole value. like John;Black;Mike to John,Black,Michael in all rows automatically.
You could use replace
update people
set name = replace( name, 'Mike', 'Michael')
where name LIKE '%Mike%';
anyway you should avoid storing comma separated value .. you should think to a proper normalized table for this data ..

MySQL query to concat prefix to existing value in a field

My table structure is as follows,
ID Name Source
1 John first.jpg
2 Doe second.jpg
3 Mary third.jpg
4 Kurian four.jpg
I would like to update the "Source" by prepending with the host and primary key as follows
http://example.com/1/first.jpg
http://example.com/2/second.jpg
http://example.com/3/third.jpg
http://example.com/4/four.jpg
tried with CONCAT("http://example.com/"+id,Source) but fails with Truncated incorrect DOUBLE value:
Any suggestion will be greatly apreciated.
Try
UPDATE table_name
SET Source = CONCAT('http://example.com/', ID, '/', Source);
Result
| ID | Name | Source |
|----|--------|---------------------------------|
| 1 | john | http://example.com/1/first.jpg |
| 2 | Doe | http://example.com/2/second.jpg |
| 3 | Mary | http://example.com/3/third.jpg |
| 4 | Kurian | http://example.com/4/fourth.jpg |
checkout this
SELECT CONCAT("http://example.com/" , CONCAT(ID , CONCAT("/" , Source))) FROM table_name;
or simply
SELECT CONCAT("http://example.com/" ,ID , "/" , Source) FROM table_name;
You can iterate in sql but the easiest way is to create a php script in which you create an array with the rows of the table
Then use the dot si tax to concat ID with the domain name and the source, something like:
$newval = ‘{$table[“ID”]}’ .“/Domainname.com/” . ’{$table[“Source”]}’;
Where $table is a variable storing the associative array of the table
And then make a new query in which you override the source column for each row in the table array
Please try below code:
UPDATE table_Name
SET Source = concat(concat(CONCAT('http://example.com/',ID),'/'),source);

How to replace all values of a MysQL field like a regular expression

I want to add some value in a mysql field (please note i don't want to append). For example my data looks like:
+------------+---------------+
| col1 | value |
+------------+---------------+
| DCM4CHEE01 | "aaaa","bbbb" |
| DCM4CHEE01 | "xxxx","yyyy" |
+------------+---------------+
I want to add a piece to string to the value field (not append) like:
+------------+-------------------+
| col1 | value |
+------------+-------------------+
| DCM4CHEE01 | "aaaa","bbbb-mgr" |
| DCM4CHEE01 | "xxxx","yyyy-mgr" |
+------------+-------------------+
I want to replace the last occurance of " with -mgr". How can I achieve this by running one single mysql query?
You can try like this:
select concat(left('"aaaa","bbbb"',length('"aaaa","bbbb"') -1),'-mgr"')
DEMO
I am aware that it is not using Replace but I think this is a better way of achieving what you are asking. If you want to update all column then it will be like
UPDATE mytable
SET value =concat(left(value ,length(value) -1),'-mgr"')
use INSERT
INSERT('"aaaa","bbbb"', length('"aaaa","bbbb"'), 5, '-mgr"')