How to prepend a string to a column value in MySQL? - mysql

I need a SQL update statement for updating a particular field of all the rows with a string "test" to be added in the front of the existing value.
For example, if the existing value is "try" it should become "testtry".

You can use the CONCAT function to do that:
UPDATE tbl SET col=CONCAT('test',col);
If you want to get cleverer and only update columns which don't already have test prepended, try
UPDATE tbl SET col=CONCAT('test',col)
WHERE col NOT LIKE 'test%';

UPDATE tablename SET fieldname = CONCAT("test", fieldname) [WHERE ...]

Many string update functions in MySQL seems to be working like this:
If one argument is null, then concatenation or other functions return null too.
So, to update a field with null value, first set it to a non-null value, such as ''
For example:
update table set field='' where field is null;
update table set field=concat(field,' append');

That's a simple one
UPDATE YourTable SET YourColumn = CONCAT('prependedString', YourColumn);

UPDATE table_name SET Column1 = CONCAT('newtring', table_name.Column1) where 1
UPDATE table_name SET Column1 = CONCAT('newtring', table_name.Column2) where 1
UPDATE table_name SET Column1 = CONCAT('newtring', table_name.Column2, 'newtring2') where 1
We can concat same column or also other column of the table.

Related

Update column with value from another column

In a SQL table I have two columns: the first contain a path and the second contains a value.
colunm1
/path/
colunm2
12345
I need to update the first column with the value that exists in the second column. to get this result :
colunm1
/path/12456/
I tried this, but not working
update tablename p
set p.colunm1 = "/path/'colunm2'/"
You have the right idea, but the SQL you shared uses column2 as a string literal. You could use the concat to concatenate the two columns:
UPDATE tablename
SET column1 = CONCAT(column1, column2)
You have to use CONCAT
update tablename p
set p.colunm1 = CONCAT("/path/",`colunm2`,"/");

Execution of UPDATE based on condition

Is there a way in MySQL to insert a condition based on which the entire UPDATE query will be executed? I know that you can use IF or CASE within the query itself to insert different values, but I'm talking about this scenario:
IF ( condition is true ) UPDATE ...
Let's say I wanted to validate data and execute the UPDATE based on result (I know it's a bad idea and data validation should be done scripting wise, I'm just reviewing the theoretical possibilities). Like here below where I test a value against regexp to check if it's numerical value:
UPDATE executed:
IF ( "12345" REGEXP "[0-9]+" ) UPDATE table SET numdata = "12345" WHERE...;
UPDATE not executed:
IF ( "a1234" REGEXP "[0-9]+" ) UPDATE table SET numdata = "a1234" WHERE...;
Thanks,
Prez
Just put the REGEXP in your WHERE clause:
DECLARE mynumdata varchar(10) = "a1234";
UPDATE table SET numdata = mynumdata
WHERE #numdata REGEXP "[0-9]+"
AND <other update conditions>;
One way to do this would be to add the condition to the WHERE clause.
So if you originally had
UPDATE table SET numdata = "a1234" WHERE id=1
you could write this as
UPDATE table SET numdata = "a1234" WHERE id=1 AND "1234" REGEXP "[0-9]+"

Know how to write a mysql update query with trim function in set clause?

I need to make a update query that can allow me to update in trimming leading '0' in a particular column. I know I can write a select query like this to select trimmed value below:
SELECT TRIM(LEADING '0' FROM SkuCode) FROM MyTable WHERE Id=1;
But how can implement this is in update query. Please help me.
if the column is a text or varchar type try
UPDATE MyTable set SkuCode = SUBSTRING(SkuCode,2) WHERE Id = 1
If this SELECT statement is returning the value you want to assign to the skucode column:
SELECT t.skucode AS current_skucode
, TRIM(LEADING '0' FROM t.skucode) AS proposed_skucode
FROM MyTable t
WHERE t.id=1 ;
Then you can convert that into an UPDATE statement, by replacing SELECT ... FROM with UPDATE, and adding a SET clause before the WHERE clause, to assign the expression that returns the proposed value for sku code to the sku code column, e.g.
UPDATE MyTable t
SET t.skucode = TRIM(LEADING '0' FROM t.skucode)
WHERE t.id=1 ;

Adding text to each column of MYSQL Database

I have a database table in MYSQL with around 1000 rows. In the table I have a column called 'overview'. In each row, this column has some value and at the end of that value I have a specific line (text) starting with: 'Source...'
Now what I want is, I want to remove this line from each column and replace it with some other text content.
I believe it can be accomplished with some smart query.
You can simply use REPLACE in your query like this
UPDATE your_table SET col_name = REPLACE(col_name , ‘Source...’, ‘new_val’)
WHERE col_name LIKE '%Source...';
Check Out the SQLFIDDLE.
MySQL database has a handy and simple string function REPLACE() that allows table data with the matching string (from_string) to be replaced by new string (to_string).
The syntax of REPLACE is:
REPLACE (text_string, from_string, to_string)
In your case, you can do this way:
UPDATE `tableName` SET `column` = REPLACE(column , 'Source...', 'Replaced Value')
Use Replace
update TBL
set overview = Replace(picture, 'Source..', 'replacement')
keep a backup of the table before anything.Or you can do it on a copy.
you can do this by following:
update table_name set col_name = replace(column_name , ‘Source...’, ‘Replaced String...’);

Insert the same fixed value into multiple rows

I've got a table with a column, lets call it table_column that is currently null for all rows of the table. I'd like to insert the value "test" into that column for all rows. Can someone give me the SQL for this?
I've tried INSERT INTO table (table_column) VALUES ("test"); but that only populates that last row. How do I do all of the rows at once?
You're looking for UPDATE not insert.
UPDATE mytable
SET table_column = 'test';
UPDATE will change the values of existing rows (and can include a WHERE to make it only affect specific rows), whereas INSERT is adding a new row (which makes it look like it changed only the last row, but in effect is adding a new row with that value).
This is because in relational database terminology, what you want to do is not called "inserting", but "UPDATING" - you are updating an existing row's field from one value (NULL in your case) to "test"
UPDATE your_table SET table_column = "test"
WHERE table_column = NULL
You don't need the second line if you want to update 100% of rows.
To update the content of existing rows use the UPDATE statement:
UPDATE table_name SET table_column = 'test';
What you're actually doing is adding rows. To update the content of existing rows use the UPDATE statement:
UPDATE table SET table_column = 'test';
UPDATE `table` SET table_column='test';
The SQL you need is:
Update table set table_column = "test";
The SQL you posted creates a new row rather than updating existing rows.
To create a new empty column and fill it with the same value (here 100) for every row (in Toad for Oracle):
ALTER TABLE my_table ADD new_column INT;
UPDATE my_table SET new_column = 100;