I have the following MySQL code:
UPDATE opened_pw SET opened_date_week = CONCAT('WK', WEEK(opened_date))
What I intend to do here is change the opened_date_week columns with a prefix of 'WK' followed by the week conversion of the opened_date column.
EDIT:
How do I add a 'WK ' standard prefix to all the conversions so that whatever is stored in opened_date_week will be like this WK 13 WK 14?
If I execute: UPDATE opened_pw SET opened_date_week = WEEK(opened_date) It makes the changes but that statement doesn't include the prefix of 'WK '
You need to change the datatype of opened_date_week column to VARCHAR.
ALTER TABLE opened_pw MODIFY opened_date_week VARCHAR(10);
After altering the datatype you can now execute your update query and check the result.
UPDATE opened_pw SET opened_date_week = CONCAT('WK ', WEEK(opened_date));
Related
Is there a way to change the name of a column to the current date? I don't need it to dynamically update as the date changes -- just the date when the code was executed. I tried the below code but get a syntax error
ALTER TABLE table_name CHANGE old_column_name CURDATE() DATE;
In the ALTER TABLE statement, the new column name must be supplied like any other identifier in the SQL text.
The new name for the column cannot be supplied as the return from a function, or as bind placeholder. It has to be supplied as an identifier.
That is, the SQL statement you submit to the database will need to have the new column name actually spelled out, as part of the statement:
ALTER TABLE table_name CHANGE old_column_name new_column_name DATE
So, the short answer to your question is no, it can't be done in a single SQL statement.
Obviously, you can perform operations in separate steps, to get the current date, and to create a string containing SQL statement you want to execute. IT seems like you would also need to identify the current name of the column you want to change.
Beyond the question that was asked...
I'm having difficulty fathoming a use case where something like this would be an appropriate solution.
What problem is this type of functionality attempting to solve? Why would you need the name of the column changed. Any SQL statements that reference the column will also need to be changed. Could you store this "date" as a value in a row of another table?
The only thing I can think why someone would want to do this would be a misguided attempt to specify a column name in a resultset from a SELECT * query.
While it is probably a bad idea, what you are going to do - it is possible by using a prepared statement:
SET #stmt := CONCAT('ALTER TABLE table_name CHANGE old_column_name `', CURDATE(), '` DATE;');
PREPARE stmt from #stmt;
EXECUTE stmt;
I have a database with several hundred fields but my data structure is wrong. It is currently in uk format as follows:
d/m /y
01/01/85
01/01/96
23/12/87
What would be the most efficient way to change the dates in bulk to sql standard of year/month/day
eg.
02/01/85 --> 1985/01/02
Create a new DATE type column in your table:
ALTER TABLE myTable ADD newColumn DATE;
Use MySQL's STR_TO_DATE() function to parse the existing values into your new column:
UPDATE myTable SET newColumn = STR_TO_DATE(oldColumn, '%d/%m/%y');
Change your codebase to use the new column. If this can't happen immediately, you could define BEFORE INSERT and BEFORE UPDATE triggers to keep the new column consistent with the old one:
CREATE TRIGGER myTable_bi BEFORE INSERT ON myTable FOR EACH ROW
SET NEW.newColumn = STR_TO_DATE(oldColumn, '%d/%m/%y');
CREATE TRIGGER myTable_bu BEFORE UPDATE ON myTable FOR EACH ROW
SET NEW.newColumn = STR_TO_DATE(oldColumn, '%d/%m/%y');
Drop the old column:
ALTER TABLE myTable DROP oldColumn;
select date_format(date, '%Y-%m-%d') use this to change it to required format.I have used date_format function. You can get more information about date_format here
How can I write a SQL query to replace all occurrences of space in a table with underscore and set all characters to lowercase?
To update a single column in a single table, you can use a combination of LOWER() and REPLACE():
UPDATE table_name SET column_name=LOWER(REPLACE(column_name, ' ', '_'))
To "duplicate" the existing column, and perform the updates on the duplicate (per your question in a comment), you can use MySQL's ALTER command before the UPDATE query:
ALTER TABLE table_name ADD duplicate_column_name VARCHAR(255) AFTER column_name;
UPDATE table_name SET duplicate_column_name = LOWER(REPLACE(column_name, ' ', '_'));
Just be sure to update the data-type in the ALTER command to reflect your actual data-type.
When using the UPDATE statement in SQL, always remember to include a WHERE clause -- so says MYSQL Workbench! :D
My Answer though:
REPLACE(string1, find_in_string, replacementValue);
Is table name prefix for fields to be update not allowed in SQL? Like:
UPDATE tablename
SET tablename.mycolums = true
WHERE ...
What is the SQL standard?
The above query perfectly works in SQL SERVER environment.
The SQL standard for UPDATE statement has following form:
UPDATE table_name SET column_name = value [, column_name = value ...] [WHERE condition]
But Some databases uses non standard form by using FROM in UPDATE statement:
UPDATE alias_name
alias_name.mycolums=true
FROM tablename alias_name
It is not valid to prefix the column in the SET section with table alias in PostgreSQL, mentioned in the documentation:
column
The name of a column in table. The column name can be qualified with a subfield name or array subscript, if needed. Do not include the table's name in the specification of a target column — for example, UPDATE tab SET tab.col = 1 is invalid.
It is also mentioned that this behavior conforms to the SQL standard.
Yes, ofcourse it is allowed in Sql-Server. If you write a query like below, its working fine.
UPDATE KKDb SET KKDb.StdName = 'Sai' WHERE (KKDb.StdNo = 1)
hIs there any way to update all the columns of a mysql table for a particular record in one go to a particular value.
For e.g. I have a table that has around 70 columns , and they are by default set to 0 at the time of creating the table,when I add a new record via PHPmyadmin by just filling in one or two values and submitting it all the other fields are set to 0 , but I want to set all the fields to 1
many times ,so I need to set all the columns to 1 individually via PHPmyadmin
To speed-en up the process and
I tried
UPDATE tablename SET * = '1' WHERE id = '2' , but it does not work.
If anyone can provide a solution on similar lines , it would be great.
EDIT:
Is there a way without specifying all the 70 columns in the SQL statement? that what I am looking for. I do know how to update normally specifying columns in the SQL statement. Thank you.
If you are looking for a way to update all 70 columns to a single value with a short, simple statement, then I recommend that you write a stored procedure to do the update. That way you only need to write out the full update syntax once, and can re-use it over and over by calling the stored procedure.
CREATE PROCEDURE update_all_columns (p_new_value SMALLINT, p_id INT) ...
CALL update_all_columns(1,2);
Another trick is to use the information_schema.columns table to generate the update statement, making it less tedious to code the stored procedure.
Something like this:
SELECT concat('UPDATE ',
table_name,
' SET ',
group_concat(column_name separator ' = p_new_value, '),
' = p_new_value',
' WHERE id = p_id;') as sql_stmt
FROM information_schema.columns
WHERE table_schema = 'your_schema'
AND table_name = 'tablename'
AND column_name != 'id'
You have to name each column in an update statement.