substring dynamic number of characters - mysql

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()

Related

Pad strings by spaces to certain length

Consider the following two strings,
source
-------
'ADAM' -- 4 chars length
'BOB' -- 3 chars length
I want to concatenate spaces after the strings where the number of spaces + length of string(n) should not exceed a specific number.
So the output should look something like the below where n = 8 in this case.
result
-----------
'ADAM ' -- 8 chars length
'BOB ' -- 8 chars length
How can I do this in mysql dynamically?
I could check the length of the field and use case statements for each scenario but thats not ideal. I am using mysql 8.0.17.
You would use the function rpad():
select rpad(name, 8, ' ')

Split value and place separated value into two different column using mysql

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)

MySQL - Return from a string the leftmost characters from 2 different characters

I have a database with some codes seperated by / or -, I want to show the left side only, this is an example of the data:
45/84
12/753
68-53
15742-845
2/556
So, i want to get this:
45
12
68
15742
2
I tried using LEFT(), but this search for 1 character only, and returns a warning if the character is not found, this is what LEFT(field,'/') returns.
45
12
(WARNING)
(WARNING)
2
So, what about a REGEXP?
an IF?
any way to ignore from the first non numeric character?
I dont' have more ideas...
Thank you!
Try this:
SELECT SUBSTRING_INDEX(SUBSTRING_INDEX(col, '-', 1), '/', 1)
FROM mytable
Demo here
You can do it with this statement. Replace the string '15742/845' with your fieldname
SELECT SUBSTRING_INDEX( REPLACE('15742/845','/','-'), '-', 1)

mysql where string ends with numbers

My table column contains values like:
id | item
-------------
1 | aaaa11a112
2 | aa1112aa2a
3 | aa11aa1a11
4 | aaa2a222aa
I want to select only rows where value of item ends with numbers.
Is there something like this?
select * from table where item like '%number'
You can use REGEXP and character class
select * from table where item REGEXP '[[:digit:]]$'
DEMO
Explanation:
[[:digit:]] >> Match digit characters
$ >> Match at the end of the string
Within a bracket expression (written using [ and ]), [:character_class:] represents a character class that matches all characters belonging to that class.
SIDENOTE:
Other helpful mysql character classes to use with REGEXP, taken from the documentation:
Character Class Name Meaning
alnum Alphanumeric characters
alpha Alphabetic characters
blank Whitespace characters
cntrl Control characters
digit Digit characters
graph Graphic characters
lower Lowercase alphabetic characters
print Graphic or space characters
punct Punctuation characters
space Space, tab, newline, and carriage return
upper Uppercase alphabetic characters
xdigit Hexadecimal digit characters
you can use REGEXP
select * from table where RIGHT(item ,1) REGEXP '^-?[0-9]+$';
Yes you can use like for numbers.
select * from table where item like '%1'
This will work

using regualr expression in mysql to select specific rows

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]