I want to split two values which is separated by multiplication symbol for example "12X36" after split it should get 12 in one column 36 in one column i.e 'X' symbol removed from those numbers get get final values in separate column for every record in Mysql
SELECT SUBSTRING_INDEX('12X36','X',2)
but this is eliminating 'X', I want to show 12 in separate column and 36 in separate column but don't know further step
expected output:
1) 12X36
2) 23X40
column1 column2
12 36
23 40
getting output
SUBSTRING_INDEX('12X36','X',2)
12
As mentioned, many ways. Here is one using LEFT and RIGHT
SELECT LEFT('12X36', LOCATE('X', '12X36') - 1), RIGHT('12X36', LOCATE('X', '12X36') - 1)
and one using reg exp, here the substrings are all digits from the start (denoted by ^) up to a non-digit and all digit between the last noon-digit and the end of the string (indented by $)
SELECT REGEXP_SUBSTR('12X36', '^[0-9]*'), REGEXP_SUBSTR('12X36', '[0-9]*$')
There are numerous ways. One is to locate() the 'X' and substring() around it.
SELECT substring('12X36', 1, locate('X', '12X36') - 1),
substring('12X36', locate('X', '12X36') + 1)
Related
I have two columns with characters in each that match until a point and then the remaining characters don't match and the strings are often of unequal length. The position of the character where they no longer match varies but is typically after the 10th character.
I'll be glad to post a small example...is there a way to format it so it's readable on here?
Thanks in advance.
You can use the left function to match the columns.
Build a query containing your two columns and then add expressions to that query that find the matches up to different lengths.
In the SQL add columns;
IIF(Left(Column1, 1) = (Left(Column2, 1), 1, 0) AS [Char1Match],
IIF(Left(Column1, 2) = (Left(Column2, 2), 1, 0) AS [Char2Match],
IIF(Left(Column1, 3) = (Left(Column2, 3), 1, 0) AS [Char3Match] etc.
This will give you a series of columns in the resultant query where 1 indicates a match up to a certain number of characters and 0 no match. The result might look like
Column1 Column2 Char1Match Char2Match Char3Match
abcdef axxx 1 0 0
abcdef abxx 1 1 0
abcdef abcx 1 1 1
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 have a field in the mysql database that contains data like the following:
Q16
Q32
L16
Q4
L32
L64
Q64
Q8
L1
L4
Q1
And so forth. What I'm trying to do is pull out, let's say, all the values that start with Q which is easy:
field_name LIKE 'Q%'
But then I want to filter let's say all the values that have a number higher than 32. As a result I'm supposed to get only 'Q64', however, I also get Q4, Q8 and so for as I'm comparing them as strings so only 3 and the respective digit are compared and the numbers are in general taken as single digits, not as integers.
As this makes perfect sense, I'm struggling to find a solution on how to perform this operation without pulling all the data out of the database, stripping out the Qs and parsing it all to integers.
I did play around with the CAST operator, however, it only works if the value is stored as string AND it contains only digits. The parsing fails if there's another character in there..
Extract the number from the string and cast it to a number with *1 or cast
select * from your_table
where substring(field_name, 1, 1) = 'Q'
and substring(field_name, 2) * 1 > 32
I'm working with 2 sets of data that were merged together, but they're inconsistent in their format. Some are 10 characters, all numbers. Others may have a separator : at position 4. I need to substring the first 4 characters. But if the 4th character is a : substring only the first 3 characters.
Does mysql have an IF functionality to determine the number of characters to substring based on the character in position 4?
select substring(id, 1 , 3/4) from table1
You can treat the field like it's colon separated and do this to select only the first part:
SELECT SUBSTRING_INDEX(id, ':', 1)
See also: SUBSTRING_INDEX()
I'm trying to select a small set of records that match a patten I have a series of numbers in each row such as
1
2
3
some of them have sub numbers
3.1
3.2
4
5
I can select only the whole numbers using
REGEXP '^[0-9]+$'
I can select all rows that have a . in them like 3.1 3.2 etc using
REGEXP '[.]{1}'
but I can't seem to select for example only sub numbers that start with 3 I've tried
REGEXP '[^3.]{1,}'
but that returns all records
Ideally I want to return only records that are in the format of 3.1 I would like to define the start number and the dot so 3. then the second part match against the records
I hope this makes sense
I used '3\.[0-9]{1,}' - it matched.
Yours probably fails because of unescaped dot - ., which matches every character.
Escape characters with \
Format 3.d where d is digit:
3\\.[0-9]