Replace space with underscore in table - mysql

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

Related

Adding prefix in an update statement

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

Hidden character in SQL column

I've copied and pasted an SQL statement which simply adds a column into the table:
ALTER TABLE `users` ADD COLUMN `favourites​` TEXT;
However, where I have copied and pasted, the favourites name has some how managed to pick up a hidden character.
I have left the hidden character in the example above for you to see/or not see as it may be!
It's favourites?, with what appears to be a question mark.
THE PROBLEM: I need to delete this column and re-add it manually so that the hidden character is not present. The problem is that any SQL statement I do, it doesn't recognise the the column name favourites because of the hidden character and I don't know how to target it.
Has anyone got any idea how to get around this?
Do the same use show
SHOW COLUMNS FROM your_table;
for obtain the column name and then copy the column you need in your delete command
alter table your_table drop column your_column_copied
and the add the column with the right name
alter table your_table add column your_column
otherwise, if is impossible get the column_name, you can create a temp table without the wrong column with create/select command
create table (col1, col2, col3)
select col1,col2, col3
from you_table
then drop the original table and rename the temporary table and last add your column with right name
You could use dynamic query:
DECLARE #sql nvarchar(800)
SELECT #sql = 'ALTER TABLE users DROP COLUMN ' + COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS where TABLE_NAME = 'users' and COLUMN_NAME LIKE '%favour%'
EXEC sp_executesql #sql
You can obtain the column name by querying INFORMATION_SCHEMA and prepare statement with the obtained column name. Something like this:
DECLARE #StrangeColumnName NVARCHAR(16) := ''
SELECT #StrangeColumnName := COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'users' AND COLUMN_NAME LIKE 'favourites%'
DECLARE #SqlText NVARCHAR(32) := 'ALTER TABLE status DROP COLUMN ?'
EXECUTE #SqlText USING #StrangeColumnName
Maybe open the information schema of the table and copy the column name from there? i don't know which Database are you using. Please update for more information.
If you have access to phpMyAdmin or, if you can create a small script to run this script:
SELECT COLUMN_NAME, FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_name = 'your_tbl_name'
OR
SHOW COLUMNS
FROM 'your_tbl_name
and copy the column name from the page.
next, you can drop that column using
alter table your_tbl_name drop column column_name;
and you already know how to add a column in mysql so, i guess that should solve your problem.
I hope you do know that you can not comment if your reputation is below 50 and if you didn't provide enough information, those who might actually have an answer for you, but have below 50 rep, will have to post it in answers. or would you like to eliminate those who are 50 rep as candidates for helping you?
In order to delete a column you can use:
alter table <tblname> drop column <colname>
and then after deleting the column you can add the column by writing below code:
ALTER TABLE users ADD COLUMN favourites​ TEXT;
Some possibilities:
Using phpmyadmin
Using a tool to talk directly to the database like navicat etc

Renaming a column with the current date in MySQL

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;

mysql_query INSERT in to table where

I want to insert into a table where user id = to something
And change o to 1 which means user is online
Tried this
mysql_query("INSERT INTO `20s` VALUES('','','','','','','',1) WHERE `uid`='$user_id ");
But that doesn't get me anywhere. What's the right syntax?
Also what's the best way to keep a record of online friends in the database?
TIP:it's better to use update here
Correct syntax for insert query is:
INSERT INTO table_name
VALUES (value1, value2, value3,...)
but you need here is update query, so you may update already existing row:
UPDATE table_name
SET column1=value, column2=value2,...
WHERE some_column=some_value
You only need to change one column bit that show online so dont change other columns:
UPDATE `tableName` SET `columnForOnline`=1 WHERE user_id=online_user_id
There is no WHERE clause in INSERT statements (see MySQL documentation).
If you want to update a value in an existing row, use UPDATE:
UPDATE `20s` SET `online`=1 WHERE user_id=your_user_id
mysql_ functions are deprecated, use PDO or mysqli_ instead!
MySQL also supports REPLACE INTO, which follows the same syntax as INSERT. Be careful though, columns that you do not supply will be set to their defaults.
In your case:
REPLACE INTO `20s` VALUES('','','','','','','',1) WHERE `uid`= '$user_id'
(Plus, you are missing a closing quote ' at the end of your query)
Use update query instead of insert
UPDATE `tableName` SET `columnForOnline`=1 WHERE user_id=online_user_id

Set all the columns of a mysql table to a particular value

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.