Query Update first string from the left - mysql

How Can I update strings which is containing 1 in my column.
#Select column from table
sample column: 100001, 100002, 100003, 100004, 100005, 100006, 100007, 100008
#How to update one at a time query?
update column: E00001, E00002, E00003, E00004, E00005, E00006, E00007, E00008

If your values are not stored as comma separated the you can use substring() to get the string after 1,and left() function in where clause to check the value should have 1 as starting character
update t
set `column` = CONCAT('E',SUBSTRING(`column`,2))
where left(`column` , 1) ='1'
Demo
Edit from comments
update t
set `column` = CONCAT(left(`column` , 1),'E',SUBSTRING(`column`,3))
where right(left(`column` , 2),1) ='1'
Demo 2

Try this
UPDATE table
SET `column` = CONCAT(
REPLACE(
LEFT(`column`,1), '1', 'E'),
SUBSTRING(`column`, 2));

Related

concatenate a string for all column values in sql update

I have a component table which has id, query columns. I want to concatenate a string type=bug to the query column for all values. Can it be done using update statement?
udpate dbname.tablename set query=query+"some_string", wiil this work?
If i understand correctly, you can do this with CONCAT(value, ' type=bug') at every field like :
UPDATE `col` SET `col1` = CONCAT(col1, ' type=bug'), SET `col2` ...
SELECT CONCAT(Id,' string') AS 'Column' FROM Table.
UPDATE TABLE SET COLUMN = CONCAT(Id, ' string')
OR
SELECT (Id + 'string') AS 'Column' FROM Table.
UPDATE TABLE SET COLUMN = (Id + 'String')

Substring Binary Data in MYSQL

I have a binary column in my table in mysql. I need to update all the rows for example.
Replace 2nd byte with 1. Doesn't matter what the value was.
SELECT HEX(data) FROM table;
Then
UPDATE table SET data[1] = 1; // the idea
But how do you do something like this in mysql?
Here how you can do it. First get the binary representation with hex function.
Then on the returned value use substring and concat function to replace the 2nd value with 1 and finally update.
Here is an example.
CREATE TABLE t (c BINARY(10));
INSERT INTO t SET c = 'b';
select (HEX(c)) from t;
62000000000000000000
SELECT concat(
substring(HEX(c),1,1),
'1',
substring(HEX(c),3,length(HEX(c)))
)
from t
61000000000000000000
Now putting all together in the update command will replace the value
update t set c = unhex(
concat(
substring(HEX(c),1,1),
'1',
substring(HEX(c),3,length(HEX(c)))
)
);
select (HEX(c)) from t;
61000000000000000000

Format values on entire column SQL

I have a column which has values of format "01-01-2012" and I need to change it to "01.01.2012". I have to do the same for all the entries of that coloumn.
Could you please suggest a way to do it? I need a general SQL statement.
Thanks
Try this using REPLACE:
update tablename set column_name=REPLACE(column_name,'-','.')
One way to do this would be to use a basic UPDATE statement. In this case, you would update your entire table and for each row, set the value of the column to the formatted value. The basic outline would be like so:
UPDATE [TableName]
SET [ColumnName] = [FormattingExpression]
An example of [FormattingExpression] could be:
REPLACE ([ColumnName], '-' , '.' )
This would replace all instances of '-' with '.' for the values in the [ColumnName] column of your [TableName] table.
WITH TEST_DATA AS (
SELECT '12-12-2012' AS mydate FROM DUAL UNION ALL
SELECT '07-23-2013' FROM DUAL
)SELECT REPLACE(mydate, '-', '.') mynewdate
FROM TEST_DATA
Just replaced the - with .
REPLACE ( '01-01-2012' , '-' , '.' )
Try str_to_date
UPDATE `table` SET `date_col` = str_to_date(`date_col`,'%d.%m.%Y')
STR_TO_DATE(str,format)
update table_name set column_name = replace (column_name , '-' , '.' );

operation on column in SQL

I have a table in SQL database, where one column contains the character "C" or "P".
I want to define a new column, where the value is 1, if this character is "C" and zero otherwise.
kind regards,
Daniel
if you want to define through projection, try
SELECT columnName, IF(columnName = 'C', 1, 0) newValue
FROM tableName
As an alternative suggested by Michael Berkowski,
SELECT columnName, (columnName = 'C') newValue
FROM tableName
SELECT
your_fields,
IF(your_column = 'C', 1, 0) AS new_field
FROM your_table
Do you want it to be a permanent column? If so, create a new column in your table with a default value of 0. Then issue an update statement as follows:
UPDATE tableName SET newColumn = 1 WHERE oldColumn = 'C';
ALTER MY_TABLE ADD NEW_COLUMN INT
UPDATE MY_TABLE SET NEW_COLUMN=1 WHERE OTHER_COLUMN='C'
UPDATE MY_TABLE SET NEW_COLUMN=0 WHERE OTHER_COLUMN='P'
INSERT
WHEN Col_name1='c' THEN
INTO your_Table (Col_name2)
VALUES (1)
WHEN Col_name1='P' THEN
INTO your_table (Col_name2)
VALUES (0);
SELECT * FROM your_Table;
It will work.

How do I remove all spaces from a field in a mysql database in an update query?

What would be the proper syntax used to run an update query on a table to remove all spaces from the values in a column?
I have a user table that had user names with spaces imported into it & I need to remove the spaces. i.e. "john smith sr." needs to be "johnsmithsr."
there are about 500+ occurrences.
You could try something like this:
UPDATE `table` SET `column` = REPLACE( `column` , ' ' , '' )
UPDATE <table>
SET name = REPLACE(name, ' ', '') ;
500+ occurences is not that much so this should execute in no time
Try this
update table_name set column_name = replace(column_name, ' ', '');
The second argument will be replaced by the third argument.
I think this is what we are looking for
SELECT some_columns FROM table_name WHERE REPLACE(col_name, ' ', '') LIKE 'some string';
This is probably your answer:
SELECT replace(col_name , ' ','') FROM table_name;