Format values on entire column SQL - mysql

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 , '-' , '.' );

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')

How to insert attribute to json string in mysql?

I had a json string in mysql database like following.
{"name":"Georg","position":"Manager"}
I need to add another attribute like "date_of_birth":"1989-06-08"
You can also use JSON_INSERT function:
SELECT JSON_INSERT(#`json`, '$.date_of_birth', '1989-06-08');
See dbfiddle.
You could try using a replace() function on the json string
select REPLACE ( column_name, "}", ', "date_of_birth":"1989-06-08"}')
from my_table
or for update the value in you table
UPDATE my_table
SET column_name = REPLACE ( column_name, "}", ', "date_of_birth":"1989-06-08"}')

Query Update first string from the left

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));

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;

how to trim leading zeros from alphanumeric text in mysql function

What mysql functions are there (if any) to trim leading zeros from an alphanumeric text field?
Field with value "00345ABC" would need to return "345ABC".
You are looking for the trim() function.
Alright, here is your example
SELECT TRIM(LEADING '0' FROM myfield) FROM table
TIP:
If your values are purely numerical, you can also use simple casting, e.g.
SELECT * FROM my_table WHERE accountid = '00322994' * 1
will actually convert into
SELECT * FROM my_table WHERE accountid = 322994
which is sufficient solution in many cases and also I believe is performance more effective. (warning - value type changes from STRING to INT/FLOAT).
In some situations, using some casting function might be also a way to go:
http://dev.mysql.com/doc/refman/5.0/en/cast-functions.html
If you want to update one entire column of a table, you can use
USE database_name;
UPDATE `table_name` SET `field` = TRIM(LEADING '0' FROM `field`) WHERE `field` LIKE '0%';
I believe you'd be best off with this:
SELECT TRIM(LEADING '0' FROM myField)
SELECT TRIM(LEADING '0' FROM *columnName*) FROM *tableName* ;
This also work correctly
just remove space between TRIM ( LEADING
use
SELECT * FROM my_table WHERE TRIM(LEADING '0' FROM accountid ) = '00322994'
simply perfect:
SELECT TRIM(LEADING '0' FROM myfield) FROM table
TRIM will allow you to remove the trailing, leading or all characters. Some examples on the use of the TRIM function in MySQL:
select trim(myfield) from (select ' test' myfield) t;
>> 'test'
select trim('0' from myfield) from (select '000000123000' myfield) t;
>> '123'
select trim(both '0' from myfield) from (select '000000123000' myfield) t;
>> '123'
select trim(leading '0' from myfield) from (select '000000123000' myfield) t;
>> '123000'
select trim(trailing '0' from myfield) from (select '000000123000' myfield) t;
>> '000000123'
If you want to remove only a select amount of leading/trailing characters, look into the LEFT/RIGHT functions, with combination of the LEN and INSTR functions