I have a table which contains 3 columns, fname Lname and description. I want to take the values of Fname and Lname and append them to description (Move this data to the end of the description field without overwriting the current info in the field). Is this possible with MySQL at all?
Please update function as per req...and concatination of string :
Here Concat function is used for concat string..:
update table_name set description = CONCAT(description,'
',fname,' ',Lname);
update your_table_name set description = CONCAT(description,' ', fname,' ',Lname);
Related
enter image description here
what command do i have to write to have fullname columns = student_first_name + student_last_name.
i only know select concat command to filter, i want here that it will insert and display as select concat command.
add a new column to table and then use update command to ensure that you fill full name. Using + for string is ill-advised since it is mostly for varchar values not nvarchar values.
query in SQL Server
ALTER TABLE yourtable
ADD FullName VARCHAR(100);
UPDATE yourtable
SET FullName=CONCAT(student_first_name ,student_last_name)
--SET FullName=student_first_name +student_last_name
query in MYSQL
ALTER TABLE yourtable
ADD COLUMN FullName VARCHAR(100) AFTER student_last_name;
UPDATE yourtable
SET FullName = CONCAT(student_first_name ,student_last_name)
--SET FullName=student_first_name +student_last_name
I have the following issue. I have a table called titles with the following structure:
id int(10),
name varchar(100),
At some point later we added a new column called modified_name. It is defined as the same as name except that it is lower case and has all of the spaces replaced with a -. We added this column and so we needed to now get the right modified name value into each record of that column. To do this we wrote a PHP script that handled that by loading in values from the database and processing them, but that is highly inefficient. Is it possible to write a single UPDATE query that would add the correct value to each record in the titles table. I can think of ways to do this with a stored procedure and a while loop there in, but I want to know if something more efficient is possible. It there any way to achieve something like the following:
UPDATE `titles`
SET
`modified_name` = LOWER(REPLACE(SELECT `name` FROM `titles` WHERE id = PRESENT_VALUE), ' ', '-');
The goal being to SET the modified_title column of every record in the titles table to a unique value that results from that record's name column as followed:
# Before modification update query
name = "Hello Goodbye"
modified_name = ""
# After modification update
name = "Hello Goodbye"
modified_name = "hello-goodbye"
Thank you for your help, any advice on how best to do this would be appreciated.
UPDATE `titles` SET `modified_name` = LOWER(REPLACE(`name`, ' ', '-'))
Say, you got a table of 100 records. And field age contains a some integers. And you want all those integers to be incremented by 1.
or you got a textfield called name and a bunch of names in there. And you want all those names to be prefixed as Mr..
Is there a way to achieve this in one SQL command?
The alternative would be to compile a recordset of these 100 recs and going thru a loop and then running an individual update statement.
Use the update command
update yourtable
set age=age +1
update yourtable
set name = 'Mr. ' + name
where gender='M'
UPDATE mytable SET age = age+1
UPDATE mytable SET name = CONCAT('Mr. ', name)
If MySQL is in ANSI mode – specifically, PIPES_AS_CONCAT, you can use 'Mr. ' || name instead.
Below is how my table is
create table tab (id INT, fullname varchar(100));
Data is
insert into tab values
(1,'Full Name 1'),
(2,'Full Name 2'),
(3,'Full Name 3'),
(4,'Full Name 4'),
(5,'Full Name 5'),
(6,'Full Name 6');
I want to update the table with fullname as My Full Name is + actuallfullname. e.g. data for id 1 should be My Full Name is Full Name 1.
Any idea how to get this done in one query?
Using below query, it would be executing n times as I have so many records.
UPDATE tab SET fullname='My Full Name is Full Name 1';
sqlfiddle
Use CONCAT.
UPDATE tab
SET fullname = CONCAT('My Full Name is ', fullname)
SQLFiddle Demo
You can use MySQL's CONCAT() function:
UPDATE tab SET fullname = CONCAT('My Full Name is ', fullname);
But does this really belong in the database? Sounds like something one would normally perform at the presentation layer of one's application.
Ref CONCAT
UPDATE tab SET fullname=CONCAT('My Full Name is ',fullname);
UPDATE tab SET fullname = CONCAT('My Full Name is ' , fullname)
I have a MySQL table named "users" that has the columns "firstname" and "surname". There are a thousand or so rows in the table.
I have now added another column named "search" and I would like to populate it with the values of both the first name and the surname separated by a space. For example, if firstname = Bob and surname = smith, i would like to populate "search" with "Bob Smith".
Can anyone advise on an update statement that selects these values and inserts them in to the new column?
Best regards, Ben.
You could simply use...
UPDATE users SET search=TRIM(CONCAT(firstname, ' ', surname));
As an explanation, CONCAT simply concatenates (merges) the supplied fields/values, and TRIM will remove any leading or trailing spaces, hence ensuring that there are no issues if a firstname/lastname or indeed both are missing. See the MySQL String Functions manual page for more information on these functions.
However, I'd be tempted to call the new column "name" (or indeed "full_name") rather than "search", as this is, at best, a somewhat misleading field name.
update users
set search = CONCAT(firstname, ' ', surname)
This script will update it.
I'm not sure about MySql, but Sql Server lets you set a field as a calculated column. You can specify the calculation as "ISNULL(FirstName, ' ') + ' ' + ISNULL(Surname, ' ')" and set it as persisted so it doesn't calculate it each time. In that case, you wouldn't need an update script
update `users` set `search` = CONCAT(`firstname`, ' ', `surname`);