How do I replace all my NULL values in a particular field in a particular table? - mysql

I've looked all over the internet for my answer, and perhaps I'm just doing things wrong. I have a column in my MySQL table that I need to replace all the NULL values with a text string in my SQL Query using phpMyAdmin. I don't want the output to come out that way, I want to actually replace the null values with the text string.
I've tried
UPDATE `tablename` SET fieldname = replace (fieldname, "", "textstring")
I've read up on
SELECT ISNULL(field,"replacetext)
But this only shows the output, but doesn't actually replace it in the table.
I can't figure this out, and I've wasted so much time trying to find an answer.

update tablename set fieldname = "textstring" where fieldname is null;

Have you tried
UPDATE `tablename` SET fieldname = '' where fieldname is null

Related

Choose which field based on IFNULL

I have the following query which I am trying to run in MySQL. Basically I am trying to work out how to update the corresponding field which isn't null. I know its going to be one of two possible fields but I want to update the one which isn't empty. Maybe I am missing something blindingly obvious, but this is what I've tried thus far:
UPDATE `table`
#SET IF(FieldA IS NULL,FieldB,FieldA) = 1234
#SET IFNULL(FieldA,FieldB) = 1234
WHERE `FieldC` = '5678'
AND (
`FieldA` = '1234'
OR `FieldB` = '1234'
)
I suspect there may be a CASE solution but I'd prefer a shorthand/simple version option if it exists.
(My)SQL has no syntax that allows you to dynamically change the column you want to update, e.g. something like update ... set {PickFieldBaseOnCondition:FieldA|FieldB} = 1234. You have to specify a column there. The moment you start your query, the basic structure of the query (and all fields involved) have to be clear and fixed, only the values can change.
So you need to update both fields in your query if it shall be able to modify two different columns. But you can of course decide to just not modify the content of a field based on its content, e.g. keep it if it is null already:
update `table`
set FieldA = IF(FieldA IS NOT null, 1234, FieldA),
FieldB = IF(FieldB IS NOT null, 1234, FieldB)
where ...
Note that the requirement "update the other field if a field is null" only works if your initial condition that one field is null and one is not null is fulfilled, which you said is given. Otherwise, you should include a test if both fields are null or both fields are not null (in the comparison for the first field), which you could e.g. do with
update `table`
set FieldA = IF(FieldB IS null, 1234, null),
FieldB = IF(FieldB IS NOT null, 1234, FieldB)
where ...
FieldA can now be changed from content to null if both fields are not null (and from null to content if both fields are null), to enforce the condition that exactly one field is not null.
Please also note that IF() is a MySQL-only shorthand for CASE and doesn't work in all databases. You prefered a non-case solution, but it can trivially be rewritten using the sql-standard CASE.

MySQL 5.7 date field trouble

I have installed recently MySQL 5.7 . Something weird is happening with a date column.
If I execute a SELECT query using that field in a WHERE section, I have a resultset, but when I use the same WHERE condition to UPDATE a row I get an Invalid date format error message.
Here is the SELECT sentence:
SELECT *
FROM
FDpoCargTran
WHERE FDpoCargTran.Banco = '001'
AND (FDpoCargTran.Conciliacion = '' OR FDpoCargTran.Conciliacion IS NULL)
AND FDpoCargTran.Fecha = '2016-09-27'
This sentence returns 2 rows resultset, that's ok.
Now, Here's the UPDATE sentence:
UPDATE
FDpoCargTran
SET
Edo = 'C'
WHERE FDpoCargTran.Banco = '001'
AND (FDpoCargTran.Conciliacion = '' OR FDpoCargTran.Conciliacion IS NULL)
AND FDpoCargTran.Fecha = '2016-09-27'
AND Deposito = 1041
And I get this error message:
Data truncation: Incorrect date value: '' for column 'Conciliacion'
The Conciliacion columns is defined as:
`Conciliacion` date DEFAULT NULL,
If I remove the Conciliacion = '' condition, everything works fine.
Why empty string is not valid to evaluate the column in an UPDATE sentence and not in a SELECT?
Please, an idea!!!
Basically, for date datatype, you cannot store something like White
Spaces or ' ' strings. You need to make the column to accept NULL
values and insert an actual NULL into it. This is not a problem in MySQL 5.7, its how date is set in databases.
Update is a DML statement, when you actually want to write something into table, or check for a condition, MySQL is unable to understand what ' ' is for the date column type.
So, you cannot have ' ', instead you can have NULL set. Thats how MySQL can check the condition and make appropriate changes!!
I would suggest, change it to NULL.
Alter your datetime column to varchar.
Import whatever table/database.
Update FDpoCargTran set Conciliacion=NULL where Conciliacion='';
Alter column back to datetime.

mySQL Query Remove Null Records in Column

I have a large mySQL database and I want to remove every record that is empty, not null in a certain column. What is the best way to do write a SQL query for this?
Currently I have tried:
DELETE FROM Businesses WHERE WEBADDRESS IS NULL
But it did not delete anything. There are 44,000 records and almost 80% of them are null in that column.
DELETE FROM myTable WHERE myColumn IS NULL
Link to MySQL page for DELETE syntax: http://dev.mysql.com/doc/refman/5.7/en/delete.html
IF the column is not NULL but just blank you would need to do something like:
DELETE FROM myTable WHERE myColumn = ''
Based on the information you also provided in the comments, the values are likely being loaded as empty ('') and not NULL: http://dev.mysql.com/doc/refman/5.7/en/problems-with-null.html
The second query should work.
delete from your_table
where certain_column is null
DELETE from `<tablename>`
WHERE `<columnname>` is null
delete from table_name where column=''
delete from table_name where column_name is null;
this query will definitely work for you.
Don't worry about count of data. Mysql can handle it. Just write:
DELETE FROM `Businesses` WHERE `WEBADDRESS` = '';

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

concat to text field

I have a table with 1 column "textField". Our version of mysql won't allow for full text fields to have a default value, the default is null.
When we want to update our table we want to append a value to whatever is in textField, like so:
update table set textField = concat( textField ,'value')...this works when there is something already in the field, but won't work if the field is null. Since mysql won't allow for a default value in a full text field the above statement won't work.
A solution is to do 2 queries:
1. check if that field is null
2. if so, then don't do a concat & just update w/ the value...if not null then do the concat
We'd rather not do 2 queries if we can avoid it...is there a one-liner alternative that would work?
Try this:
update table set textField = concat(COALESCE(textField, '') ,'value')...
concat(ifnull(textfield, ''), 'value')