Using MySQL how to grab first part of the data.postcode field?
For example:
M40 1JY
M8 8NW
I want to display like this:
M40
M8
select substring(postcode, 1, locate (' ', postcode) - 1) from data;
locate (' ', postcode) returns the position of the space (), the first input, out of the second input (your column), you don't want to display the space so subtract 1 from this number.
substring(data, start, length) takes the substring of the data (your column) from the number of start, you want to start at the beginning, so this is 1, and displays the number of letters you enter in length, this is the result of locate.
Use substring and locate:
select substring(postalcode,1,LOCATE(" ",postalcode)) from `data`
locate will find the index of blank and substring gives you the substrung until this Position.
The SUBSTRING_INDEX(str,delim,count) function is what you need. This function return the substring from string str before count occurrences of the delimiter delim. You can go the this link for more details. http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_substring-index
For your problem,
SELECT SUBSTRING_INDEX(postcode, ' ', 1) from YourTable;
will return
M40
M8
Related
For every row in a column, I want to take the portion of the string to the left of a parenthesis and the portion of the string to the right of the parenthesis.
Number indexing will not work here because the next row might have the parenthesis at a different position
In other words I essentially want to eliminate the ( and ) and everything in between them.
FOR EXAMPLE
if the cell contains a string that says:
'I am using MySQL (version) 5.7'
I want it to read:
'I am using MySQL 5.7'
In advance, I appreciate the help. Thank You.
Nick
Use the function Substring_index for this
Update
I think I understand what your problem is now, when there are no parentheses in the string then you get a duplicate output. Try this instead, I use a case to check if there is a right and left parantheses in the string before doing a concat, otherwise I just select the field itself
SELECT
CASE
WHEN INSTR(name, '(') > 0 AND INSTR(name, ')') > 0 THEN CONCAT(SUBSTRING_INDEX(name, '(', 1), SUBSTRING_INDEX(name, ')', -1))
ELSE name
END AS label
FROM first
I thought this was the answer but it isn't. It doesn't show the text after the closing parenthesis
SELECT CONCAT(SUBSTRING_INDEX(field1, '(', 1), SUBSTRING(field1, ')', -1));
As Joakim already mentioned you can use substring_index:
SELECT CONCAT(SUBSTRING_INDEX('I am using MySQL (version) 5.7', '(', 1), SUBSTRING_INDEX('I am using MySQL (version) 5.7', ')', -1));
you can try it in http://sqlfiddle.com/. SCHEMA:
CREATE TABLE IF NOT EXISTS `docs` (
`content` varchar(200) NOT NULL
) DEFAULT CHARSET=utf8;
INSERT INTO `docs` (`content`) VALUES
('I am using MySQL (version) 5.7'),
('The earth is (VERY - VERY) flat and rests on a bull\'s horn');
QUERY:
SELECT CONCAT(SUBSTRING_INDEX(content, '(', 1),
SUBSTRING_INDEX(content, ')', -1))
FROM docs;
OUTPUT:
I am using MySQL 5.7
The earth is flat and rests on a bull's horn
in MySQL 8.0+ you could also use REGEXP_REPLACE function
https://dev.mysql.com/doc/refman/8.0/en/string-functions.html#function_substring-index says:
Returns the substring from string str before count occurrences of the delimiter delim. If count is positive, everything to the left of the final delimiter (counting from the left) is returned. If count is negative, everything to the right of the final delimiter (counting from the right) is returned. SUBSTRING_INDEX() performs a case-sensitive match when searching for delim.
I want to fetch first occurrence of numbers from a table column.
To illustrate:
For column value 'C 62/3,Industrial Area, Phase 2', I should get '62/3'
For column value 'B-5 dummy, (dummy)', I should get 'B-5'
For column value '21 dummy - 2, dummy' I should get '21'.
I tried:
select address,SUBSTRING(address, 1, LOCATE(' ', address) - 1) AS str
from items;
Well, I'm not a MySQL expert but I think you have to use a library like mysql-udf-regexp. Using this you can get the first digit-containing sub string with REGEXP_SUBSTR:
SELECT
address,
REGEXP_SUBSTR(address, '[[^:space:]]*[[:digit:]]+[[^:space:]]*', 1, 1) AS str
FROM items;
This is untested code but should give you an idea how to proceed. It means that you get any positive number of digits ([[:digit:]]+) followed and succeeded by any number of non-white spaces ([[^:space:]]*); starting search from first character and return first occurrence (,1 ,1).
I'd like to extract the number between NUMBER and ;. So far I can extract the data up to the number, but I don't want anything after the number. e.g.,
SELECT
SUBSTRING(field, LOCATE('NUMBER=', rrule) + 7)
FROM table
Data field:
DATA:PASS=X12;NUMBER=331;FIELD=1
DATA:PASS=X12;NUMBER=2;FOO=BAR;FIELD=1
Desired Output:
331
2
You can use a combination of SUBSTRING_INDEX functions:
SELECT
SUBSTRING_INDEX(
SUBSTRING_INDEX(field, 'NUMBER=', -1),
';',
1)
FROM
tablename
Please see an example fiddle here.
The inner SUBSTRING_INDEX will return everything after the NUMBER= string, while the second will return everything before the ; returned by the inner function.
I would like to sort a result from a MySQL query by the number of words in a specified column.
Something like this:
SELECT bar FROM foo ORDER BY WordCountFunction(bar)
Is it possible?
As far as I know, there is no word count function in MySQL, but you can count the number of spaces and add one if your data is formatted properly (space separator for words, no spaces at beginning/end of entry).
Here is the query listing longest words first:
SELECT bar FROM foo ORDER BY (LENGTH(bar) - LENGTH(REPLACE(bar, ' ', ''))+1) DESC
Using this method to count the number of words in a column, your query would look like this:
SELECT bar FROM foo ORDER BY (LENGTH(bar) - LENGTH(REPLACE(bar, ' ', ''))+1) DESC
Yes, sort of. It won't be 100% accurate though:
SELECT SUM(LENGTH(bar) - LENGTH(REPLACE(bar, ' ', ''))+1) as count
FROM table
ORDER BY count DESC
But this assumes the words are separated by a space ' ' and doesn't account for punctuation. You can always replace it with another char and it doesn't account for double spaces or other chars either.
For complete accuracy, you could always pull the result out and word-count in your language of choice - where accurate word-count function do exist!
Hope this helps.
I'm trying to select distinct substring values of a field and count the number of instances of a char in that selection.
I've found this wonderful post which answers half of it.
So, so far, i can count the instances of a char in my field, it works great. Now the even harder part, what if i select a piece of string using :
SELECT DISTINCT SUBSTRING_INDEX(my_field, '-', -1) AS chunk
In this case i'm only selecting the last part of the string (everything after the last'-'). How can i apply this formula to chunk (trying to count the number of instances of '_' in the new string ? :
(LENGTH(chunk) - LENGTH(REPLACE(chunk, '_', ''))) / LENGTH('_')
I know i shoud be using HAVING to make operation on chunk as it's not a real field, but how can i do something like :
SELECT DISTINCT SUBSTRING_INDEX(my_field, '-', -1) AS chunk, (LENGTH(chunk) - LENGTH(REPLACE(chunk, '_', ''))) / LENGTH('_') AS total FROM my_field HAVING total < 2
The problem here is that i can't use 'chunk' in the last part since it's not a field..
The problem here is that i can't use 'chunk' in the last part since it's not a field..
Replace 'chunk' in the last part with
SUBSTRING_INDEX(my_field, '-', -1)
Don't know what's the problem?