I have several tables that have a common field (column) called LastName in a MySQL database. Several of the rows in these tables are in mixed case so they don't get selected properly when doing a SELECT.
How can I convert those columns to all UPPER CASE? I can easily handle any new entries to convert them to upper case, but the existing records I'm not so sure about.
would do the job
update table set LastName=UPPER(LastName);
NOTE - if you are running from MySQL workbench you may have to disable safety mode or add a where clause (eg WHERE id>0) otherwise it wont run.
this would work:
UPDATE table_name SET `column_name` = UPPER( `column_name` )
You can use the string function UPPER() to make the column value to upper
update Your_table set LastName=UPPER(LastName)
Related
The webhosting I use has enabled StrictMode for the Databases. All my php scripts now stopped working because they report I haven't defined a default value for some columns.
As I have a lot of columns in a lot of tables, is there a way to set all the columns with "default value = none" with "default value = NULL" ?
In this way it won't report me the error anymore.
Of course, If there's another (better) way, I am available for it.
I tried looking on the net, but I couldn't find anything suitable for this case.
you can alter column
ALTER TABLE table_name
MODIFY COLUMN col datatype DEFAULT null
A general approach here which should work for each column causing an error would be to set a default value, and then maybe do an update to backfill records missing a value.
ALTER TABLE yourTable ALTER some_text_column SET DEFAULT 'None';
And here is the update:
UPDATE yourTable SET some_text_column = 'None' WHERE some_text_column IS NULL;
You are not required to do this update, but it might make sense to bring older records missing values in line with what newer records would look like.
How would I go about mass inserting an ending </div> tag into a defined field name in MySQL?
Thanks heaps.
Consider using the CONCAT command:
UPDATE jos_content SET fulltext = CONCAT( fulltext, '</div>' ) WHERE id = 7;
If you wanted to update ALL your records in a single query, simply remove the WHERE clause above.
Please note, depending on what server side language you're using, you'll want to setup a parameterized query for this to prevent SQL injection or other hacks.
My web application receives an update form for a db record, submitted by the user.
I would like to create an sql which will update all the values to exactly what the user submitted, except those which the user left blank. For those, I want the value to be set to its DB default.
Is there a way to do it?
I'm looking for something like
update my_table set col1=17, col2=DEFAULT, col3='some text'
Please notice that I'm updating an existing row, thus I cannot just live some columns out from the update sql, as they might have had a value before which needs to be erased now.
There is a DEFAULT(col_name) function:
Returns the default value for a table column. An error results if the
column has no default value.
UPDATE my_table SET col1=17, col2=DEFAULT(col2), col3='some text'
Well apparently it's just that simple, exactly as I wrote:
update my_table set col1=17, col2=DEFAULT, col3='some text'
http://dev.mysql.com/doc/refman/5.0/en/update.html
Is this possible in phpMyAdmin, to execute that query on all records within a table (to get rid of any whitespace)
You may have to list the field name but you'd only need to do so once per field.
UPDATE 'table_name' SET 'field_name' = TRIM('field_name')
(I would advise testing this before running it on your live data)
Try to cheat:
update venues set postcode=TRIM(postcode)+''
I have a database where old code likes to insert '0000-00-00' in Date and DateTime columns instead of a real date. So I have the following two questions:
Is there anything that I could do on the db level to block this? I know that I can set a column to be not-null, but that does not seem to be blocking these zero values.
What is the best way to detect the existing zero values in date fields? I have about a hundred tables with 2-3 date columns each and I don't want to query them individually.
Followup:
The default is already set to null. A long time ago, the default was '0000-00-00'. Some code still explicitly places '0000-00-00'. I would prefer to force that code to throw an error so I could isolate and remove it.
Is there anything that I could do on the db level to block this?
Yes, enable the NO_ZERO_DATE mode:
SET sql_mode = 'NO_ZERO_DATE';
The behaviour is documented. Additionally, you might want to also set the mode to include NO_ZERO_IN_DATE...
Also make sure the sql_mode includes either STRICT_ALL_TABLES or STRICT_TRANS_TABLES; without these NO_ZERO_IN_DATE only give a warning, but insert still succeeds.
What is the best way to detect the existing zero values in date fields? I have about a hundred tables with 2-3 date columns each and I don't want to query them individually.
Separate columns means they have to be checked individually--nothing you can do about that.
Assuming you can't easily fix the data and "SET sql_mode = 'NO_ZERO_DATE';", you could create a view on the table...
CREATE VIEW filter AS
SELECT other_column,
CASE
WHEN realtable.dodgy_date = 0 THEN NULL
ELSE realtable.dodgy_date
END AS dodgy_date
FROM realtable;
SET SQL_MODE='ALLOW_INVALID_DATES';
SET GLOBAL sql_mode=(SELECT REPLACE(##sql_mode,'NO_ZERO_DATE',''));
SET GLOBAL sql_mode=(SELECT REPLACE(##sql_mode,'NO_ZERO_IN_DATE',''));
Just run above queries on database. It will solve the issue permanently.
If it doesn't matter what date goes in there (ie as long as it's non-zero) you can change the column definition to use NOW() as the default. Probably not an ideal solution, but it does satisfy the criteria :
1) Not-null
2) Non-zero
I'm actually really not proud of that suggestion
You could make the columns nullable and have NULL as the default value, but it sounds like you already have that and it's not working. ALTHOUGH... it could be the tool you're using to display the data doesn't like displaying NULL dates... what tool are you using? Or is the '0000-00-00' showing up in data retreived by code?
You could set a default value that is non-null and also easily recognizable as a default such as 1900-01-01 (assuming you don't normally deal with dates that are close to this date).
A trigger can be used to enforce values for columns.
set the timestamp by default is maybe an option for you, use table change statement for that:
ALTER TABLE mytable CHANGE date_update timestamp NOT NULL DEFAULT CURRENT_DATE()
MySQL CURRENT_DATE() documentation at w3c resource
To remove Zero Dates and replace them by e.g. the current date do this:
UPDATE mytable SET date_update = CURRENT_DATE() where date_update = "0000-00-00"
This is the trigger I use:
delimiter //
CREATE TRIGGER bad_date BEFORE INSERT ON some_table
FOR EACH ROW
BEGIN
IF NEW.the_date='0000-00-00' THEN
SET NEW.the_date= CURDATE();
END IF;
END;//
If updates are a concern, add a separate trigger (BEFORE UPDATE) to do the same thing.