I thought this would be easy... maybe not. I have a table with 'fullname' and I want to split the first name and last name into 2 columns (fname and lname).
The following syntax gives me the data I want:
SELECT
`fullname` ,
SUBSTRING_INDEX( SUBSTRING_INDEX(`fullname` ,' ', 2) ,' ' ,-1) AS fname,
SUBSTRING_INDEX(`fullname` ,' ', 1)AS lname
FROM MyTable
... but how do I then take the 'fname' and 'lname' fields and save them to separate columns in the same table?
[Example data -- If the persons name is John Michael Jones, the 'fullname' field looks like this: JONES JOHN MICHAEL ]
You can just update your table and set the columns to the substrings of the fullname
update MyTable
set fname = SUBSTRING_INDEX(SUBSTRING_INDEX(`fullname` ,' ', 2) ,' ' ,-1),
lname = SUBSTRING_INDEX(`fullname` ,' ', 1)
If you want to perform the update on the same table, you can use the following:
update mytable
set fname = SUBSTRING_INDEX( SUBSTRING_INDEX(`fullname` ,' ', 2) ,' ' ,-1),
lname = SUBSTRING_INDEX(`fullname` ,' ', 1)
See SQL Fiddle with Demo
you want to update into first name ans last name value then you need to used update :
update MyTable
set fname = SUBSTRING_INDEX( SUBSTRING_INDEX(`fullname` ,' ', 2) ,' ' ,-1),
lname = SUBSTRING_INDEX(`fullname` ,' ', 1)
and if you want to set in upper case then user upper function as well as
update MyTable
set fname = upper(SUBSTRING_INDEX( SUBSTRING_INDEX(`fullname` ,' ', 2) ,' ' ,-1)),
lname = upper(SUBSTRING_INDEX(`fullname` ,' ', 1))
Related
I would like rows returned in a MySQL query to be sorted alphabetically by surname for which I have an SQL query like:
SELECT
id,
substring_index(name, ' ', -1) as surname
FROM
my_table
ORDER BY
surname asc
However, some names have parenthesis to denote some special circumstance such as: Laura Angel (retired)
How can I modify my SQL above to ignore the parenthesised text, to sort by surname alphabetically?
Try with nested replaces to remove the parentheses.
SELECT
id,
substring_index(name, ' ', -1) as surname
ORDER BY
REPLACE( REPLACE( surname , '(' , '') , ')' , '') ASC;
Test and modify according to you version of SQL.
Not tested.
You can use this solution:
SELECT id,
substring_index(rtrim(substring_index(name, '(', 1)), ' ', -1) as surname
FROM test.test
ORDER BY
surname asc;
I have the Following Query:
SELECT BUSINESS_NAME, 'KEYWORD', REPLACE(BUSINESS_NAME, ' ', '-')
FROM clearindia.business b
LEFT OUTER JOIN `clearindia`.`keywords_master` km ON km.KEYWORD_TEXT = b.BUSINESS_NAME
WHERE km.KEYWORD_TEXT IS NULL
AND b.business_name='Dey Radio Service'
GROUP BY BUSINESS_NAME
It gives me the following results:
# BUSINESS_NAME, KEYWORD, REPLACE(BUSINESS_NAME, ' ', '-')
'Dey Radio service', 'KEYWORD', 'Dey-Radio service'
REPLACE(BUSINESS_NAME, ' ', '-') is not working correctly and does not replace the second space with a '-'. Why is that?
Please Note: BUSINESS_NAME has a collation of utf_unicode_ci.
I have this code:
GROUP_CONCAT(
CONCAT(
DATE_FORMAT(je.date_entered, '%m/%d/%Y - %h:%i%p'),
' by ',
'\n',
REPLACE(
REPLACE(
REPLACE(
REPLACE(
REPLACE(je.description, '<br />', '\n'),
''', '\''),
'"', '"'),
'<', '<'),
'>', '>')
),
'\n\n' ORDER BY je.date_entered DESC SEPARATOR ''
) AS enteries
It works fine. When I add this subquery:
' by ', (SELECT first_name FROM users WHERE id = je.created_by),
resulting in this Group Concat:
GROUP_CONCAT(
CONCAT(
DATE_FORMAT(je.date_entered, '%m/%d/%Y - %h:%i%p'),
' by ',
(SELECT first_name FROM users WHERE id = je.created_by),
'\n',
REPLACE(
REPLACE(
REPLACE(
REPLACE(
REPLACE(je.description, '<br />', '\n'),
''', '\''),
'"', '"'),
'<', '<'),
'>', '>')
),
'\n\n' ORDER BY je.date_entered DESC SEPARATOR '')
AS enteries
it breaks the whole dang thing, causing no errors, but just returning null.
This may look hairy and tangled but I don't have the option to put everything together in php -- it has to all be done in mysql during the selection process.
p.s. I didn't post the enter query because it is ridiculously long BUT I can if you want me to.
When you use a subquery inside group_concat and concat, they will return:
NULL if the subquery returns 0 rows OR a NULL value
something (presumably good) if the subquery returns 1 row
an error (1242) if the subquery returns more than one row
Presumably your subquery didn't return any rows or returned a NULL value.
Here is my final, working solution:
GROUP_CONCAT(CONCAT(DATE_FORMAT(je.date_entered, '%m/%d/%Y - %h:%i%p'), ' by ', IF((SELECT first_name FROM users WHERE id LIKE je.created_by) IS NULL, '', (SELECT first_name FROM users WHERE id LIKE je.created_by)), ' ', IF((SELECT last_name FROM users WHERE id LIKE je.created_by) IS NULL, '', (SELECT last_name FROM users WHERE id LIKE je.created_by)), '\n', REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(je.description, '<br />', '\n'), ''', '\''), '"', '"'), '<', '<'), '>', '>')), '\n\n' ORDER BY je.date_entered DESC SEPARATOR '') AS enteries
How can I get these four columns to be written concatenated into another column?
SELECT CONCAT( calle, ' ', num, ', ', colonia, ', ', cd ) AS geoco
FROM TABLE_1
ORDER BY id
UPDATE table_1
SET geoco = CONCAT( calle, ' ', num, ', ', colonia, ', ', cd )
UPDATE TABLE_1
set col_to_update = CONCAT( calle, ' ', num, ', ', colonia, ', ', cd )
where ...
Using the function to split strings: http://blog.fedecarg.com/2009/02/22/mysql-split-string-function/
SELECT SUBSTRING_INDEX( SUBSTRING_INDEX( fullname, ' ', 1 ) , ' ', -1 ) AS firstname,
SUBSTRING_INDEX( SUBSTRING_INDEX( fullname, ' ', 2 ) , ' ', -1 ) AS lastname
FROM users;
I get the following resuts:
firstname | lastname |
john doe
jane doe
I have the following questions:
1.)How can I insert the values on columns that already exist under the same table?
2.)How can I insert the values on columns that don't exist under the same table?
Of course, the two columns being firstname | lastname
To insert these values:
INSERT INTO users (firstname, lastname)
SELECT SUBSTRING_INDEX( SUBSTRING_INDEX( fullname, ' ', 1 ) , ' ', -1 ) AS firstname,
SUBSTRING_INDEX( SUBSTRING_INDEX( fullname, ' ', 2 ) , ' ', -1 ) AS lastname
FROM users;
To update existing values, for the same row:
update users
set firstname = SUBSTRING_INDEX( SUBSTRING_INDEX(fullname, ' ', 1 ) , ' ', -1 ),
lastname = SUBSTRING_INDEX( SUBSTRING_INDEX(fullname, ' ', 2 ) , ' ', -1 )
To update existing values, but a different row:
update users users_a, users users_b
set users_b.firstname = SUBSTRING_INDEX( SUBSTRING_INDEX( users_a.fullname, ' ', 1 ) , ' ', -1 ),
users_b.lastname = SUBSTRING_INDEX( SUBSTRING_INDEX( users_a.fullname, ' ', 2 ) , ' ', -1 )
where users_a.??? = users_b.??? /* join condition as appropriate */