I am trying to split a column with over 4 millions rows into 4 new columns, The problem is i haven't a clue where to look or which term I should Google search. (Yes i have searched Google and Stack for a similar question but only found one on stack that might be a good fix but not sure if it would help, Added link further down to the question in question.)
Here is my DB.
ID Security
1 1-5-4-6
2 2-4-06-5
3 1-4-1-2
4 1-4-1-3
5 1-45-5-32
What i am trying to do is the following, The "security" Column need to be split in 4 separate columns (group,lvl,key,code)
ID group lvl key code
1 1 5 4 6
2 2 4 6 5
3 1 4 1 2
4 1 4 1 3
5 1 45 5 32
The problem i am having is finding a suitable solution to split these numbers, one issue is the current security column can have up to 3 characters per - separation.
i could have a row that has 01-45-822-01 or as simply as 1-2-3-4.
One question i did find on stackoverflow
MySQL : how to split text and number with "-"
It does seem to point me in the right direction but still unsure if this would be suitable for such a large data set. over 5.9gb indexed or if there is a way to do this quicker.
You can use SUBSTRING_INDEX to do this all in one query:
UPDATE `table`
SET
`group` = SUBSTRING_INDEX(`Security`, '-', 1),
`lvl` = SUBSTRING_INDEX(SUBSTRING_INDEX(`Security`, '-', 2), '-', -1),
`key` = SUBSTRING_INDEX(SUBSTRING_INDEX(`Security`, '-', 3), '-', -1),
`code` = SUBSTRING_INDEX(`Security`, '-', -1);
And around each of those you'll probably want to wrap a CAST(expr AS UNSIGNED) assuming you're no longer storing numbers as text.
Related
Allowed characters are 0-9 A-Z a-z and .-_
Ich want to search in MySQL Database in one column if there some other characters as allowed and replace them with "-".
Is it possible with SQL?
Something like this ;)
#ARRAY = (0,1,2,3,4,5,6,7,8,9,.,-,_,A-Z,a-z);
UPDATE table SET columnName = replace(columnName, 'CHAR DOES NOT MATCH #ARRAY', '-');
UPDATE:
ID NUMBER
1 1620WGC-2018_3.2
2 70-30-2015
3 PTMMC4450-2017+
4 PE-1013-64/2018
5 1580-2018_3%2
6 PE-1036-68A-2018
7 D10+
In Column NUMBER the ID's 3,4,5 and 7 shoud be corrected to:
ID NUMBER
1 1620WGC-2018_3.2
2 70-30-2015
3 PTMMC4450-2017-
4 PE-1013-64-2018
5 1580-2018_3-2
6 PE-1036-68A-2018
7 D10-
because +,/, and %, for example, are not in Array with allowed charakters.
You might need this.
SELECT * FROM table WHERE replace(replace(columnName, '\'', ''), '-', '') = 'findMyText'
I need to search a database for two specific values, these being C1 and C5 which appear in the notes in each row.
However, some notes contain information such as C128, C523, C100 – I do not want to pull these.
To complicate things even more, some notes contain the C1 and C5 on their own e.g. with a space directly after the number but some notes contain the C1 and C5 with letters directly after them e.g. C1DANNY, C5CLAIRE, C1RUN – I do need to pull these as well as those that only say C1 and C5.
E.g. Database
ID Notes
1 C1
2 C128
3 C5
4 C1
5 C1DANNY
6 C25
7 C1RUN
8 C10
9 C523
10 C5!
So from the above database, I only want to pull rows: 1, 3, 4, 5, 7 and 10.
I don’t want to pull anything that has a number after the second digit such as line 8 above. I do want to pull everything that has any character apart from a number after the second digit such as line 3 and 10.
I should also mention I have been reading up about what wildcards I can use here https://www.w3schools.com/sql/sql_wildcards.asp but I feel I probably need to use an IF statement to loop round and check precisely what is included within each note but the database is quite big (hundreds of thousands of rows).
There is some code already written by a previous worker. The code in question being:
(
esl.log_text LIKE 'C1%' OR
esl.log_text LIKE 'C5%' OR
esl.log_text LIKE '%' + CHAR(10) + 'C1%' OR
esl.log_text LIKE '%' + CHAR(10) + 'C5%' OR
esl.log_text LIKE '%' + CHAR(13) + 'C1%' OR
esl.log_text LIKE '%' + CHAR(13) + 'C5%'
)
Unfortunately, this appears to be pulling lines that include C10 for example.
I am new to SQL and Stack Overflow and any assistance on what code I could use to achieve this would be very much appreciated.
You can try this:
SELECT Notes
FROM dbo.Testing_Code AS T0
WHERE (LEFT(Notes, 2) IN ('C1', 'C5')) AND (ISNUMERIC(SUBSTRING(Notes, 3, 20)) = 0)
I wasn't able to find this anywhere, here's my problem:
I have a string like '1 2 3 4 5' and then I have a mysql table that has a column, let's call it numbers, that look like this:
numbers
1 2 6 8 9 14
3
1 5 3 6 9
7 8 9 23 44
10
I am trying to find the easiest way (hopefully in a single query) to find the rows, where any of the numbers in my search string (1 or 2 or 3 or 4 or 5) is contained in the numbers column. In the give example I am looking for rows with 1,2 and 3 (since they share numbers with my search string).
I am trying to do this with a single query and no loops.
Thanks!
The best solution would be to get rid of the column containing a list of values, and use a schema where each value is in its own row. Then you can use WHERE number IN (1, 2, 3, 4, 5) and join this with the table containing the rest of the data.
But if you can't change the schema, you can use a regular expression.
SELECT *
FROM yourTable
WHERE numbers REGEXP '[[:<:]](1|2|3|4|5)[[:<:]]'
[[:<:]] and [[:<:]] match the beginning and end of words.
Note that this type of search will be very slow if the table is large, because it's not feasible to index it.
Here is a start point (split string function) : http://blog.fedecarg.com/2009/02/22/mysql-split-string-function/ := SplitString(string,delimiter,position)
Create a function so it converts a string to an array := stringSplitted(string,delimiter)
Create a function so it compares two arrays :=arrayIntersect(array1, array2)
SELECT numbers
FROM table
WHERE arrayIntersect(#argument, numbers)
Two function definitions with loops and one single query without any loop
SELECT * FROM MyTable WHERE (numbers LIKE '%1%' OR numbers LIKE '%2%')
or you can also use REGEX something like this
SELECT * FROM events WHERE id REGEXP '5587$'
I am trying to write a pivot function in MySQL workbench and many of the places I've looked have not been super relevant.
I currently have:
order_ID Part Description Order number
1 103 A 1
2 104 B 1
3 103 A 2
4 105 C 3
5 103 A 4
6 105 C 4
7 107 D 4
I would like to create:
Order Part1 Description Part2 Description Part3 Description
1 103 A 104 B
2 103 A
3 105 C
4 103 A 105 C 107 D
I can keep the primary key in the output, but it is not necessary. The problem I am running into is that many pivot functions involve using distinct parts names to move them; however, I have over 500 parts. I also would like to move the description and the part together so they are next to each other--most pivot functions are not powerful enough to address that.
I did write a macro to do this in Excel, but it must be done in a database because of further analysis in R and I am pulling data from a database and I must automate any changes made to the data. As a result, I DO NOT have a choice in how the data is organized and laid out. Please do not mention normalizing data or other database techniques because I am trying to fix the data and how messy it is, but I DO NOT have a choice in how the data is inputted.
Some resources I used to gain experience with pivoting in MySQL, but I have not been able to get any code to work.
MySQL pivot table
mysql pivoting - how can I fetch data from the same table into different columns?
http://en.wikibooks.org/wiki/MySQL/Pivot_table
http://buysql.com/mysql/14-how-to-automate-pivot-tables.html
Select group_concat(Table.column1) as anything,
group_concat(Table.column2 separator ';')
AS Anything2, Table.`column3`
FROM Table
group by Table.column3;
Alter TABLE Table ADD
`newcolumn1` varchar(100) DEFAULT '' after `column3`;
Alter TABLE MB ADD
`newcolumn2` varchar(500) DEFAULT '' after `newcolumn1`;
UPDATE Table SET
`newcolumn1` = IF (
LOCATE(',', column1) >0,
SUBSTRING(column1, 1,LOCATE(',', column1)-1),
column1
),
`newcolumn2` = IF(
LOCATE(',', column1) > 0,
SUBSTRING(column1, LOCATE(',', column1)+1),
'');
UPDATE Table SET
newcolumn2 = SUBSTRING_INDEX(newcolumn2, ',', 1);
UPDATE Table SET
newcolumn3 = SUBSTRING_INDEX(newcolumn3, ',', 1);
This code achieved exactly the format I wanted above.
I have a table which holds a varchar datatype. It holds 128 characters max.
I'm trying to order it alphabetically, and it works out fine, except for one little thing.
When I try to save a mixture of numbers and letters, it returns the 'literal' alphabetical order, which means 11 comes first before 2.
I have read almost all of the answers in the internet, but they are all workarounds that cannot work specifically for my problem.
Examples of values I want to put in order
Apartment
House
Dog
Cat
18 years old
2 years old
1 year old
But I want it to look like this.
1 year old
2 years old
18 years old
Apartment
Cat
Dog
House
It spans on a large database and I can't just split the numerical values apart from the text ones.
Also users who can use the program can modify it with Alphanumeric characters.
Any suggestions about my problem? Thanks.
Here is something I tried in SQL Server. It's neither elegant nor fit for production, but it may give you an idea.
SELECT StringValue,
CAST(SUBSTRING(StringValue, StartPos, EndPos - StartPos) AS INT) AsNumber,
SUBSTRING(StringValue, StartPos, EndPos - StartPos) NumberToken,
SUBSTRING(StringValue, EndPos, 1000) Rest,
StartPos,
EndPos
FROM
(SELECT
StringValue,
PATINDEX('[0-9]%', StringValue) StartPos,
PATINDEX('%[^0-9]%', StringValue) EndPos
FROM
(SELECT 'abc123xyz' StringValue
UNION SELECT '1abc'
UNION SELECT '11abc'
UNION SELECT '2abc'
UNION SELECT '100 zasdfasd') Sub1
) Sub2
ORDER BY AsNumber, Rest
Result:
StringValue AsNumber NumberToken Rest StartPos EndPos
abc123xyz 0 abc123xyz 0 1
1abc 1 1 abc 1 2
2abc 2 2 abc 1 2
11abc 11 11 abc 1 3
100 zasdfasd 100 100 zasdfasd 1 4
I would approach this as follows...
First, write an expression to convert the numeric stuff to integers, something like
select CAST(SUBSTRING(<field>',1,instr(<field>',' ') as INT),<field>
I would then use a UNION ALL statement, something like this
SELECT CAST(SUBSTRING(<field>',1,instr(<field>',' ') as INT),<field>,A.*
FROM <table> A
WHERE <field> LIKE <regular expression to get fields beginning with numbers>
UNION ALL
SELECT 999999,<field>,A.*
FROM <table> A
WHERE <field> NOT LIKE <regular expression to get fields beginning with numbers>
ORDER BY 1,2,3
The numbers will appear first, in numeric order. Sine all of the alpha data has the same numeric key, it will appear sorted alphabetically after the numbers... Just be sure to make the alpha dummy key (999999) is large enough to be after all the numeric ones...
I don't have mySQL on this machine, but hopefully this gives you enough of a start to solve it
you should probably get away by doing something like this:
order by right(replicate(' ',30)+Column_name,30)
Try this order by:
ORDER BY RIGHT(REPLICATE('0',128)+value,128)
My test:
DECLARE #T TABLE
(
value VARCHAR(128)
)
INSERT INTO #T VALUES('Apartment'),
('House'),
('Dog'),
('Cat'),
('18 years old'),
('2 years old'),
('1 year old'),
('12 horses'),
('1 horse')
SELECT * FROM #T
ORDER BY RIGHT(REPLICATE('0',128)+value,128)
RESULTS:
Cat
Dog
House
1 horse
12 horses
Apartment
1 year old
2 years old
18 years old
If you find a case that this doesn't work please post it along with the sort order you would like and I can see if there's a fix.