This question already has answers here:
Locate text position, extract text and insert in new column in MySQL
(2 answers)
Closed 2 years ago.
I have string
good morning what a lovely day today
I need to find the position of 'lovely'. Query should return the result 5.
You can use find_in_set() for this:
select find_in_set('lovely', replace('good morning what a lovely day today', ' ', ','))
Yields:
5
From the documentation:
FIND_IN_SET(str,strlist)
Returns a value in the range of 1 to N if the string str is in the string list strlist consisting of N substrings. A string list is a string composed of substrings separated by , characters.
So basically the logic is to replace space characters with commas, and then use that function.
Related
This question already has answers here:
MySQL query finding values in a comma separated string
(11 answers)
Closed 1 year ago.
I would appreciate help with one regex.
I have a string of ids example "123,55,68,890,456,333,168"
How should the regex look like to find a specific id -- example 68? Bear in mind that 168 shouldn't be returned.
There are four cases where the id could be positioned:
x (only one id in the list/string)
,x, (somewhere in the middle of the string)
x, (at the beginning of the string)
,x (at the end of the string)
I would use this regex as part of the SQL query.
Thanks in advance
/(?:^|,)number(?:$|,)/ should do the trick.
(?:) is a non-capturing group. It looks for the value in the parentheses after the :, but doesn't include it in your result.
So this regular expressions says "Find number that is at the beginning or preceded by a comma, and at the end or followed by a comma."
This question already has an answer here:
How to match a fixed string ending with a number using regex [duplicate]
(1 answer)
Closed 2 years ago.
I'm trying to select, from mysql, some specific tables that matches a pattern.
The specific pattern I'm looking for is like 'my_table_number'. For example, my_table_436814 or my_table_35413. The thing is that there are other tables that looks like my_table_old_14353 or my_table_351434_times. I just want to filter out the ones that are my_table_number.
I have tried different patterns, but failed to get what I really need.
The most closest approach was with this:
select table_name from
information_schema.columns ca
where ca.table_name REGEXP '[0-9]$'
How can I achieve what I'm looking for?
Use
REGEXP '^my_table_[0-9]+$'
See proof
NODE
EXPLANATION
^
the beginning of the string
my_table_
'my_table_'
[0-9]+
any character of: '0' to '9' (1 or more times (matching the most amount possible))
$
before an optional \n, and the end of the string
This question already has answers here:
MySQL Second (or third) Index Of in String
(5 answers)
MySQL: select first element of a comma-separated list
(2 answers)
Closed 4 years ago.
I have comma separated value like 2018-06-20 01:12:53,2018-06-20 01:14:11
But the problem is I need date and time format like day-month-Year H:i when I use DATE_FORMAT(t1.pickup_date_time, "%d-%m-%Y %H:%i")
it could not run because the column(pickup_date_time) has comma separated value.
Your help is appreciated.
Thank you.
Use SUBSTRING_INDEX first to split the column on the comma. Then use STR_TO_DATE to reformat the date. If you have issue with the actual code, let me know.
This question already has answers here:
MySQL Select Query - Get only first 10 characters of a value
(3 answers)
Truncate a string to first n characters of a string and add three dots if any characters are removed
(20 answers)
Closed 6 years ago.
I need to take only three characters from the string stored in MYSQL and output must be the first 3 character and the login date of customer
I am beginner kindly help me to code in PHP.
SELECT LEFT(string , 3) , logindate FROM tbl;
Hope this will help you.
This question already has answers here:
MySQL Query, remove all spaces
(2 answers)
Closed 9 years ago.
I have tried using the TRIM() in MySQL to delete all of the whitespace in every field in a column but it will only remove it from the front. I have long strings that have spaces in the middle next to hyphens and I need the space between the hyphens removed. Is there a way to do this?
UPDATE FOO set FIELD2 = TRIM(FIELD2);
Use the REPLACE() method, which can replace every occurrence of a character by another:
UPDATE foo SET field2 = REPLACE(field2, ' ', '');
This query will remove every spaces in the field2 column.
Definition of the method, from the MySQL doc:
Returns the string str with all occurrences of the string *from_str* replaced by the string *to_str*. REPLACE() performs a case-sensitive match when searching for *from_str*.
mysql> SELECT REPLACE('www.mysql.com', 'w', 'Ww');
-> 'WwWwWw.mysql.com'
This function is multi-byte safe.