I need help to convert the data in a following format from 2 columns (ID & description ) to 6 further columns shown below . Will appreciate the suggestions provided.
Id Description
------ ------------------------------------------------------------------------------------------
1 Company : RAFIQ BRAZING WORK
1 Factory Address : Plot No. 2, 87/B/12, Shop No. 1 , Nr. Lalit
1 Factory Address : Engineering, G.I.D.C., Umber, Dist, Valsad - 7482
,
1 Factory Address : ITHDH
1 Contact Name : Mr. Adam Noor / Mr. Noor,
1 Mobile : 8888761 9323
1 Product : MS Steel,
1 Product : Copper
2 Company : ComapSAPNA STEEL
2 Factory Address: Plot No. 1909, Ill Phase, GIDC, Umbergoan,
2 Factory Address : Dist. Valsad 5er 5334, Arat
2 Mobile : 0260-32517320 Fax: 0260-2562133
2 Contact Name: Mr. Farukh Abdulla Mobile: 6667027032
2 Email: farbdulla# gmail.com
2 Office address: Gala no. 3, B.T. Compound, malad west, Mumbai - 407777
2 Contact Name: Mr. Hamsa Abdulla Mobile:093333732768
2 Product: Specialist in Profile Cutting, Traders of M.S.Plate,
2 Product : Angels Channels, etc.
ID Company contactperson mobilenumber products factoryaddress
1 RAFIQ BRAZING WORK Mr. Adam Noor/ Mr. Noor +8888761 9323 MS Steel, Copper Plot No. X, 19/B/12, Shop No. 1 , Nr. Lalit Engineering, G.I.D.C., Umber, Dist, Valsad - 7482 ,ITHDH
That’s a poor data model. Each attribute should be stored as a column rather than buried in a string.
For your setup, assuming that ' : ' consistently separates the attribute name from its value, you could use string functions and conditional aggregation like this:
select id,
group_concat(case when attr = 'Company' then val end) as company,
group_concat(case when attr = 'Factory Address' then val end) as factory_address,
...
from (
select t.*,
left(description, locate(' : ', description) - 1) as attr,
substring(description, locate(' : ', description) + 3) as val
from mytable t
) t
group by id
The subquery parses the string as an attribute/value pair, then the outer query aggregates.
Unlike some other RDBMS MySQL doesn't have native support for pivoting operations of this sort by design (the developers feel it's more suited to the presentation, rather than database, layer of your application).
If you absolutely must perfom such manipulations within MySQL, building a prepared statement is the way to go—although rather than messing around with CASE, I'd probably just use MySQL's GROUP_CONCAT() function:
SELECT CONCAT(
'SELECT `table`.id', GROUP_CONCAT('
, `t_', REPLACE(name, '`', '``'), '`.value
AS `', REPLACE(name, '`', '``'), '`'
SEPARATOR ''),
' FROM `table` ', GROUP_CONCAT('
LEFT JOIN `table` AS `t_', REPLACE(name, '`', '``'), '`
ON `table`.id = `t_', REPLACE(name, '`', '``'), '`.id
AND `t_', REPLACE(name, '`', '``'), '`.name = ', QUOTE(name)
SEPARATOR ''),
' GROUP BY `table`.id'
) INTO #qry FROM (SELECT DISTINCT name FROM `table`) t;
PREPARE stmt FROM #qry;
EXECUTE stmt;
Note that the result of GROUP_CONCAT() is limited by the group_concat_max_len variable (default of 1024 bytes: unlikely to be relevant here unless you have some extremely long name values).
Related
I know that I can combine multiple columns in mysql by doing:
SELECT CONCAT(zipcode, ' - ', city, ', ', state) FROM Table;
However, if one of the fields is NULL then the entire value will be NULL. So to prevent this from happening I am doing:
SELECT CONCAT(zipcode, ' - ', COALESE(city,''), ', ', COALESCE(state,'')) FROM Table;
However, there still can be situation where the result will look something like this:
zipcode-, ,
Is there a way in MySQL to only have to comma and the hyphen if the next columns are not NULL?
There is actually a native function that will do this called Concat with Separator (concat_ws)!
Specifically, it seems that what you would need is:
SELECT CONCAT_WS(' - ',zipcode, NULLIF(CONCAT_WS(', ',city,state),'')) FROM TABLE;
This should account for all of the null cases you allude to.
However, it is important to note that a blank string ('') is different than a NULL. If you want to address this in the state/city logic you would add a second NULLIF check inside the second CONCAT_ws for the case where a city or a state would be blank strings. This will depend on the database's regard for the blank field and whether you are entering true NULLS into your database or checking the integrity of the blank data before you use it. Something like the following might be slightly more robust:
SELECT CONCAT_WS(' - ', zipcode, NULLIF(CONCAT_WS(', ', NULLIF(city, ''), NULLIF(state, '')), '')) FROM TABLE;
For more, check out the native documentation on concat_ws() here:
https://dev.mysql.com/doc/refman/8.0/en/string-functions.html#function_concat-ws
I think you need something like this
Set #zipcode := '12345';
Set #city := NULL;
Set #state := NULL;
SELECT CONCAT(#zipcode, ' - ', COALESCE(#city,''), ', ', COALESCE(#state,''));
Result: 12345 - ,
SELECT CONCAT(#zipcode, IF(#city is NULL,'', CONCAT(' - ', #city)), IF(#state is NULL,'', CONCAT(', ',#state)))
Result 12345
You need to make the output of the separators conditional on the following values being non-NULL. For example:
SELECT CONCAT(zipcode,
CASE WHEN city IS NOT NULL OR state IS NOT NULL THEN ' - '
ELSE ''
END,
COALESCE(city, ''),
CASE WHEN city IS NOT NULL AND state IS NOT NULL THEN ', '
ELSE ''
END,
COALESCE(state, '')
) AS address
FROM `Table``
Output (for my demo)
address
12345 - San Francisco, CA
67890 - Los Angeles
34567 - AL
87654
Demo on dbfiddle
I asked a similar question but it has become a larger problem.
The below question catered for 2 options, but not if a single option was stored in the database. See: Use SUBSTRING_INDEX(). Answer by Joyal George:
MYSQL SELECT multiple values between "" in Column
I have a form in WordPress which captures info on people who want to receive updates in certain areas. They can select 1 or more areas.
I then have a reporting plugin which only accepts SQL to retrieve the data for the report. No application layer, only SQL queries to a MYSQL database
If someone only selects 1 area, I need to extract only that area. If they select more than 1 area I need to extract each area separated by a comma. They can select up to 9 areas.
Data in the column is as follows:
1 area:
Western Cape
Multiple areas:
a:3:{i:0;s:10:"North-West";i:1;s:12:"Western Cape";i:2;s:13:"Northern Cape";}
I am using a case statement (previous issue with this database structure)
select
a.entry_id,
MAX(case when field_id = 74 then entry_value end) as FirstName,
MAX(case when field_id = 75 then entry_value end) as LastName,
MAX(case when field_id = 76 then entry_value end) as Email,
MAX(case when field_id = 78 then entry_value end) as Phone,
MAX(case when field_id = 79 then
(select concat(
SUBSTRING_INDEX(
SUBSTRING_INDEX(
SUBSTRING_INDEX(
entry_value,
'"',
4
),
'"',
2
),
'"',
-1
),
",",
SUBSTRING_INDEX(
SUBSTRING_INDEX(
SUBSTRING_INDEX(
entry_value,
'"',
4
),
'"',
4
),
'"',
-1
)
))
end) as InterestedIn,
MAX(case when field_id = 81 then entry_value end) as Province from ch_arf_entry_values a GROUP BY a.entry_id
I need to adjust the 'as InterestedIn' to cater for only 1 input value.
I need to find a solution for the last case 'as Province'
Any assistance would be greatly appreciated.
You should find a better way to represent what you want, but I think the following comes close:
select concat_wc(',', substring_index(substring_index(entry_value, '"', 2), '"' -1),
substring_index(substring_index(entry_value, '"', 4), '"' -1),
. . .
)
You might have to put a stop condition based on the number of values in the number of values in the string, resulting in something like:
select concat_ws(',',
case when num_entry_values >= 1 then substring_index(substring_index(entry_value, '"', 2), '"' -1) end,
case when num_entry_values >= 2 then substring_index(substring_index(entry_value, '"', 4), '"' -1) end,
. . .
)
If you don't have this number, you can calculate it by counting the number of double quotes in the string.
EDIT:
To count the number of entries, count the ":
from (select aev.*,
(length(entry_value) = length(replace(entry_value, '"', '')) ) / 2 as num_entry_values
from ch_arf_entry_values aev
) aev
Mysql:
I have table : id, First_name (varchar), Last_name (varchar);
How to convert to table : id, Name (varchar); ?
And
table : id, Address (varchar);
convert to :
table : id, Street_name(varchar), house_number(varchar), room_number(varchar);
In other words, how to join or split columns VARCHAR type?
1- The joining is straitforward. If we assume your first table is "originalTable", then you need two concat the two fields and put everything in a new table, named t1 for example:
Select into t1 id, concat(First_name, last_name) as Name
From originalTable
The new table, t1, will be automatically created with two fields (int and varchar).
2- For the splitting, provided that there are three non-empty sub-fields (Street_name, House_number and Room_number), you can use the following SQL code:
SELECT
substring(Address, 1, locate(' ', Address)-1) as Street_name,
substring(
substring(Address FROM locate(' ', Address)+1),
1,
locate(' ', substring(Address FROM locate(' ', Address)+1))-1
) as House_number,
substring(
substring(
Address FROM locate(' ', Address)+1
)
FROM locate(
' ',
substring(Address FROM locate(' ', Address)+1)
)+1
)
as Room_number
From t1
For the details of the two string functions (substring and locate) used here, see MySQL documentation about string functions:
http://dev.mysql.com/doc/refman/5.0/en/string-functions.html
Currently I'm working on a database redesign project. A large bulk of this project is pulling data from the old database and importing it into the new one.
One of the columns in a table from the old database is called 'name'. It contains a forename and a surname all in one field (ugh). The new table has two columns; forenames and surname. I need to come up with a clean, efficient way to split this single column into two.
For now I'd like to do everything in the same table and then I can easily transfer it across.
3 columns:
Name (the forename and surname)
Forename (currently empty, first half of name should go here)
Surname (currently empty, second half of name should go here)
What I need to do: Split name in half and place into forename and surname
If anyone could shed some light on how to do this kind of thing I would really appreciate it as I haven't done anything like this in SQL before.
Database engine: MySQL
Storage engine: InnoDB
A quick solution is to use SUBSTRING_INDEX to get everything at the left of the first space, and everything past the first space:
UPDATE tablename
SET
Forename = SUBSTRING_INDEX(Name, ' ', 1),
Surname = SUBSTRING_INDEX(Name, ' ', -1)
Please see fiddle here. It is not perfect, as a name could have multiple spaces, but it can be a good query to start with and it works for most names.
Try this:
insert into new_table (forename, lastName, ...)
select
substring_index(name, ' ', 1),
substring(name from instr(name, ' ') + 1),
...
from old_table
This assumes the first word is the forename, and the rest the is lastname, which correctly handles multi-word last names like "John De Lacey"
For the people who wants to handle fullname: John -> firstname: John, lastname: null
SELECT
if( INSTR(`name`, ' ')=0,
TRIM(SUBSTRING(`name`, INSTR(`name`, ' ')+1)),
TRIM(SUBSTRING(`name`, 1, INSTR(`name`, ' ')-1)) ) first_name,
if( INSTR(`name`, ' ')=0,
null,
TRIM(SUBSTRING(`name`, INSTR(`name`, ' ')+1)) ) last_name
It works fine with John Doe. However if user just fill in John with no last name, SUBSTRING(name, INSTR(name, ' ')+1)) as lastname will return John instead of null and firstname will be null with SUBSTRING(name, 1, INSTR(name, ' ')-1).
In my case I added if condition check to correctly determine lastname and trim to prevent multiple spaces between them.
This improves upon the answer given, consider entry like this "Jack Smith Smithson", if you need just first and last name, and you want first name to be "Jack Smith" and last name "Smithson", then you need query like this:
-- MySQL
SELECT
SUBSTR(full_name, 1, length(full_name) - length(SUBSTRING_INDEX(full_name, ' ', -1)) - 1) as first_name,
SUBSTRING_INDEX(full_name, ' ', -1) as last_name
FROM yourtable
Just wanted to share my solution. It also works with middle names. The middle name will be added to the first name.
SELECT
TRIM(SUBSTRING(name,1, LENGTH(name)- LENGTH(SUBSTRING_INDEX(name, ' ', -1)))) AS firstname,
SUBSTRING_INDEX(name, ' ', -1) AS lastname
I had a similar problem but with Names containing multiple names, eg. "FirstName MiddleNames LastName" and it should be "MiddleNames" and not "MiddleName".
So I used a combo of substring() and reverse() to solve my problem:
select
SystemUser.Email,
SystemUser.Name,
Substring(SystemUser.Name, 1, instr(SystemUser.Name, ' ')) as 'First Name',
reverse(Substring(reverse(SystemUser.Name), 1, instr(reverse(SystemUser.Name), ' '))) as 'Last Name',
I do not need the "MiddleNames" part and maybe this is not the most efficient way to solve it, but it works for me.
Got here from google, and came up with a slightly different solution that does handle names with more than two parts (up to 5 name parts, as would be created by space character). This sets the last_name column to everything to the right of the 'first name' (first space), it also sets full_name to the first name part. Perhaps backup your DB before running this :-) but here it is it worked for me:
UPDATE users SET
name_last =
CASE
WHEN LENGTH(SUBSTRING_INDEX(full_name, ' ', 1)) = LENGTH(full_name) THEN ''
WHEN LENGTH(SUBSTRING_INDEX(full_name, ' ', 2)) = LENGTH(full_name) THEN SUBSTRING_INDEX(del_name, ' ', -1)
WHEN LENGTH(SUBSTRING_INDEX(full_name, ' ', 3)) = LENGTH(full_name) THEN SUBSTRING_INDEX(del_name, ' ', -2)
WHEN LENGTH(SUBSTRING_INDEX(full_name, ' ', 4)) = LENGTH(full_name) THEN SUBSTRING_INDEX(del_name, ' ', -3)
WHEN LENGTH(SUBSTRING_INDEX(full_name, ' ', 5)) = LENGTH(full_name) THEN SUBSTRING_INDEX(del_name, ' ', -4)
WHEN LENGTH(SUBSTRING_INDEX(full_name, ' ', 6)) = LENGTH(full_name) THEN SUBSTRING_INDEX(del_name, ' ', -5)
ELSE ''
END,
full_name = SUBSTRING_INDEX(full_name, ' ', 1)
WHERE LENGTH(name_last) = 0 or LENGTH(name_last) is null or name_last = ''
SUBSTRING_INDEX didn't work for me in SQL 2018, so I used this:
declare #fullName varchar(50) = 'First Last1 Last2'
declare #first varchar(50)
declare #last varchar(50)
select #last = right(#fullName, len(#fullName)-charindex(' ',#fullName, 1)), #first = left(#fullName, (charindex(' ', #fullName, 1))-1);
Yields #first = 'First', #last = 'Last1 Last2'
I have a field name with the following rows:
`name`
------
John Smith
George Washington
Ash Ketchum
Bill O'Reilly
I would like to reformat the rows on output:
`name`
------
SMITH, JOHN
WASHINGTON, GEORGE
KETCHUM, ASH
O'REILLY BILL
I know I can use upper( x.name ) for casing, but how can I reformat the names?
Assuming your rdbms is mySql, answer will need to be tailored to differenct dialects of sql.
SELECT concat( substring(name, locate(name, ' ')), ', ', substring(name, 0, locate(name, ' ') - 1)) FROM NAMES;
The real problem here however is data integrity. You need to have seperate columns for each peice of the name so that formating in the database is ensured to be consitent. Ideally you would want to be doing this --
Select concat(last_name, ', ', first_name) FROM NAMES;
Allowing
'name'
-------------------
John Smith
Smith, John
J Smith
Smith J
John q Smith
Jhon 'the smithmeister' Smith
All in the same column of a table is a bad thing for a number of reasons so this should be explictly prevented.
Try this:
SELECT SUBSTRING_INDEX( `name` , ' ', -1 ) + ', ' + SUBSTRING_INDEX( `name` , ' ', 1 )
FROM MyTable
#Ben, makes a good point about names with multiple spaces in them. I doubt this would give you the output you want in those cases, but it should get you started.
try this
SELECT Concat(SUBSTRING_INDEX( `name` , ' ', -1 ) , ', ' , SUBSTRING_INDEX( `name` , ' ', 1 )) as name
FROM your_Table