How to delete first word from a field in MYSQL - mysql

I have a mysql table where i want to delete the first word of a filed.
For example table have a column "product name". which contains values -
Dabur Honey
haldiram Namkeen
Colgate Toothpaste etc.
I want to delete Dabur, haldiram, Colgate and make it.
Honey
Namkeen
Toothpaste
is that possible to do this using mysql query? How can i do that?
Devesh

That will be
UPDATE t SET product_name=SUBSTRING(product_name, LOCATE(' ', product_name))

SELECT SUBSTRING_INDEX(field, ' ', -1) FROM table
It'll take everything after last space.

select substring(`product name`, instr(`product name`, ' ')) as new_prod_name
from your_table
or if you like to update the table
update your_table
set `product name` = substring(`product name`, instr(`product name`, ' '))
SQLFiddle demo

Related

remove whitespace in between Column values in mySQL

How to remove all spaces between a column field?. The spaces occur in the middle of the text so trim won't work and also replace is not working.
my code is
UPDATE temp_emp t1, master_employee t2
SET t1.lm= t2.emp_id
where REPLACE(t1.lm, ' ', '') = REPLACE(CONCAT(t2.first_name,'',t2.last_name), ' ', '');
for example when i run the query ,
select REPLACE(lm, ' ', '') AS concat from temp_emp1
i get the output as follows
concat
----------------------------------------
rick joe
james cole
albert Th
i want the output to be ;like this
concat
----------------------------------------
rickjoe
jamescole
albertTh
Without knowing the table structures and data, it is difficult for me to follow what you are doing. However, to accomplish the ouput of two concatenated columns is very straightforward.
Assume you have a table master_employee with just two columns and you want to output the FIRST and LAST names concatenated with no spaces in between. You simply use the function concat()for MySQL:
SELECT CONCAT(first_name, last_name)
from master_employee;
In Oracle, the concatenation is two pipes (||):
SELECT first_name || last_name
from master_employee;
Hope this helps.
If you want to update the existing column which has multiple spaces into one then this update query will be helpful:
UPDATE your_table SET column_that_you_want_to_change=
REGEXP_REPLACE(column_that_you_want_to_change, '[[:space:]]+', ' ');
If you don't want any spaces then this should work:
UPDATE your_table SET column_that_you_want_to_change=
REGEXP_REPLACE(column_that_you_want_to_change, '[[:space:]]+', '');

Select Comma Separator Column Data

I have data 1,2,3,1 like one column
in table
When i was display the these data in frond end
Stock column data its not taking space,
so wants select wit space for each value in stock
columnGROUP_CONCAT(msw_incoming_stock_detail SEPARATOR ', ')
You can use replace():
select replace(col, ',', ', ')
from table t

Shouldn't I see multiple inserts right away?

I'm trying to insert in Table2 multiple rows with multiple columns from Table1, but while doing so, I'm editing some of the values going into Table2. Both are large tables (several million rows). Here's the query:
insert into table2 (board_id,title,content,domainurl)
select (id, replace(title, '_', ' '), description, CONCAT("http://somesite.com/", title))
from table1
When running this query, shouldn't the insert start happening right away (being that I'm not doing a join)? I've started the query several minutes ago (phpmyadmin still shows "loading") but I don't see any rows being added to table2...?
EDIT: The query just stopped with the message "#1241 - Operand should contain 1 column(s)". I have no idea what that means!
Try this:
INSERT INTO table2 (board_id,title,content,domainurl)
SELECT
id AS board_id,
REPLACE(title, '_', ' ') AS title,
description AS content,
CONCAT("http://somesite.com/", title) AS domainurl
FROM table1
As per here all I had to do is remove the parenthesis from the SELECT clause.
select id, replace(title, '_', ' '), description, CONCAT("http://somesite.com/", title)
worked perfectly and fast!

Getting Full Name Query

I'm trying to get employee's full name and combine them using MySQL function "Concat". Some of our employee don't have middle name and in this case SQL throws an error. How can i get full name of an employee even if the employee doesn't have middle initial.
SELECT CONCAT(`Employee`.`F_NAME`,
' ',
LEFT(`Employee`.`M_NAME`, 1),
'. ',
`Employee`.`L_NAME`)
FROM `Employee`
Try to use IFNULL
SELECT CONCAT(`Employee`.`F_NAME`,
' ',
IFNULL(CONCAT(LEFT(`Employee`.`M_NAME`, 1),'. '),''),
`Employee`.`L_NAME`)
FROM `Employee`

SQL: List unique substring from fields in table

I'm running a query to retrieve the first word from a string in a colum like so..
SELECT SUBSTRING_INDEX( `field` , ' ', 1 ) AS `field_first_word`FROM `your_table`
But this allows duplicates, what do I need to add to the statement to get unique list of words out?
DISTINCT
For example:
SELECT DISTINCT SUBSTRING_INDEX( `field` , ' ', 1 ) AS `field_first_word` FROM `your_table`
Note: There are performance implications for DISTINCT. However, in your case, there is likely limited pure MySQL alternatives.
To remove duplicates in a SELECT statement, change to SELECT DISTINCT column. In this case:
SELECT DISTINCT SUBSTRING_INDEX( `field` , ' ', 1 ) AS `field_first_word`FROM `your_table`