Replace values with string mysql - mysql

I have a VARCHAR column in which some values end with the digit '5'. These
values may be of different lengths. I want to replace all such values
with the string 'UTR-5'. How can I do that?

Maybe not the fastest solution, but:
update myTable set value = 'UTR-5' where value like '%5'

Related

replace all column values equal to one string with another string

I have a MYSQL table called traceclasses and I have a column in this table called value with some of its cell values equal to E as shown below. I would like to replace all such values with U. Which query should I use to do this?
You are looking for update:
update t
set value = 'U'
where value = 'E';

whats wrong with this mysql?

can anyone please tell me what is wrong with my sql query
"INSERT INTO `userdetails`(`ID`,'Gender','RS') VALUES ('09090','0','1')";
i have set ID to be bigintand 'Gender' and 'RS' to be booleans.
Use back tics for column and table names. Single quotes are for values.
INSERT INTO `userdetails`
(`ID`,`Gender`,`RS`)
VALUES
('09090','0','1');
The problem is when you are inputing a value for the ID you are passing in a char instead of a bigint. Also, for the boolean values they shouldn't be in quotes the should be either true or false. The query should look something like this:
INSERT INTO userDetails(Id, Gender, RS)
VALUES(09090, true, false)
"INSERT INTO `userdetails`(`ID`,'Gender`,`RS`) VALUES ('09090','0','1')";
You should use the backtick (``) character to enclose column names. String values use regular quotes ('')

MySQL update query to prepend digit to existing column with integers values

I have a mysql database column named like telephoneNo
Telephone No
25645656
45454545
45565656
I want to prepend two digits XX to every value of telephoneNo column
Telephone No
xx25645656
xx45454545
xx45565656
I was trying to workout with concat but its not with integer values in my case please help with update query
You can use CAST() to convert your integers explicitely:
UPDATE t SET phone=CAST(CONCAT('10', phone) AS UNSIGNED)
That will work with integer prefixes. However, I don't see solid reason to store phone numbers as integers and not strings
That's a hack change your col to varchar or something.
UPDATE table SET telephoneNo=9200000000+telephoneNo;
EDIT:
This method requires that all your numbers are of the same length, and 8 digits long, if all your numbers are more or less than 8 digits modify the number of 0's after the 92 accordingly
If your telephone column is int, you can use concat string function to concat with int or string even like below
SELECT CONCAT(xx, telephone);
(OR)
SELECT CONCAT('xx', telephone);
(OR)
Explicitly cast your int column value
SELECT CONCAT('xx', CAST(telephone AS CHAR));

How to remove the first characters in a column

I have a column that contains string. It is a number but saved as string. The string might start with 00 or more than two zeros. I need to remove the zeros and insert the new value (after removing) into another column. The problem is the number of zeros at the beginning is not fixed. It can be 2 or more. Is this task possible to be done with MySQL ? How?
A good hint is provided here:
UPDATE your_table
SET column2 = TRIM(LEADING '0' FROM column1)
supposing that the original value is stored in column1 and you want to write the 0-trimmed one in column2
you can use CONVERT(col,UNSIGNED INTEGER) to convert it into the number, that should remove the leading zeros.
query can be
insert into <table> values(select CONVERT(col,UNSIGNED INTEGER),..... from <table2>);

REPLACE into keeping a value

I want to make a replace into in a table where cust_id is the primary key, but I do not want to modify the date field. So, a normal insert on this table would look like:
REPLACE INTO emails(cust_id, email, date)
VALUES(55, 'email#email.com', '2011-08-07 00:00');
Now, without having to modify the date field, it would be something such as:
REPLACE INTO emails(cust_id, email, date)
VALUES(55, 'email#email.com', date.value?);
But how do I exactly keep the date value?
Short answer, You can't keep the dates that way. from Mysql documentation
Values for all columns are taken from the values specified in the
REPLACE statement. Any missing columns are set to their default
values, just as happens for INSERT. You cannot refer to values from
the current row and use them in the new row
perhaps you want to use http://dev.mysql.com/doc/refman/5.0/en/insert-on-duplicate.html instead.
insert ignore will skip insertion if any duplication
if you need to update certain fields ,
you can do
insert into some_table values (...)
on duplicate update email=?;