substring_index does not take exact prefix - mysql

I have a table contains strings. Some of them starts with https:// and some starts with http://. I want to extract those that starts with http:// (without s). Please note I do not want to use LIKE statement because of another conflict in my plan to treat this string. So if I have the following items in a column called mycol in mytable:
https://111.com/
https://www.222.com/en-gb/
I make this query:
SELECT `mytable`.`mycol`, substring_index(`mytable`.`mycol`,'http://',-1)
I still get these strings in the results:
https://111.com/
https://www.222.com/en-gb/
Why? since my query is looking for http:// not https://, why do I get results start with https://? In this simple example, it should return nothing as there is no string starts with http://
want to extract the string

use regex. Much more simpler.
SELECT mycol FROM mytable WHERE mycol REGEXP '^http://.+';

You could add check for delimeter:
SELECT `mycol`,
IF(instr(mycol,'http://') > 0, substring_index(`mytable`.`mycol`,'http://',-1),NULL)
FROM mytable;
db<>fiddle demo
When the SUBSTRING_INDEX function cannot find delim string it will return original string instead of NULL.
SELECT substring_index('abc','.',-1)
=>
abc

I want to extract those that starts with http://
If you don't want want to use LIKE you can use left():
select right(columnname, length(columnname) - 7) from tablename
where left(columnname, 7) = 'http://'
You don't need to know the length of the string.
All you want is extract the part of the string after the 1st 7 chars.
The length of this part is:
length(columnname) - 7
So use right().

Related

SQL Data Update Query About

I have a table named testlink and it has url and newtarget columns.
I would like to take the string expressions https://domain1.com/ here in the url column and change all the data in the newtarget column to https://domain1.com/search/?q= pulled string expression.
So briefly;
url columns from https://domain1.com/topic1
will be changed to https://domain1.com/search/?q=topic1 in the newtarget column
There are about 6 thousand different topics (lines) available.
Database: Mysql / Phpmyadmin.
use REPLACE
UPDATE testlink
SET newtarget = REPLACE(url,'https://domain1.com/','https://domain1.com/search/?q=')
MySQL REPLACE() replaces all the occurrences of a substring within a
string.
REPLACE(str, find_string, replace_with)
If you want to conditionally change the value, you can use string manipulations:
update t
set url = concat(left(url, length(url) - length(substring_index(url, '/', -1))), 'q=', substring_index(url, '/', -1))
where url like 'https://domain1.com/%';
This uses substring_index() to get the last part of the string (after the last /). It uses left() to get the first part (based on the length of the last part) and then concatenates the values you want.
Of course, test this logic using a SELECT before implementing an UPDATE.
If you're using MySQL 8, then you'd be able to do that with REGEXP_REPLACE.
For your example, this should work :
SELECT REGEXP_REPLACE('https://domain1.com/topic1','(https:\/\/domain1\.com\/)(.+)','$1search/?q=$2')

Replace a part of a file path in a string field with SQL

Hello I have a table Gallery with a field url_immagine and I would like to use a query to replace all values that look like upload/gallery/311/ge_c1966615153f6b2fcf5d84c1e389eea8.jpg in /ge_c1966615153f6b2fcf5d84c1e389eea8.jpg
Unfortunately the a part of the string, the ID (331) is not always the same and therefore can not understand how ...
I tried the regular expression like this:
UPDATE gallery SET url_immagine = replace(url_immagine, 'upload/gallery/.*/', '/')
but it seem not to work.
Combine CONCAT and SUBSTRING_INDEX since you can use last index of "/"
UPDATE gallery
SET url_immagine = (SELECT CONCAT('/',SUBSTRING_INDEX(url_immagine, '/', -1)));
Try that to confirm it's doing what you want :
SELECT CONCAT('/',SUBSTRING_INDEX(url_immagine, '/', -1))
FROM gallery
You can see documentation for the replace function and all other string functions in the mysql manual:
https://dev.mysql.com/doc/refman/5.7/en/string-functions.html#function_replace
It does not mention that replace handles regular expressions, so we an assume it does not, and it is working verbatim and uses the your * to look for the char *.
You see also that there seem not to be a function that does the whole job for you. So you must somehow combine them. The idea of Mateo is probably the right direction.

How can i separate the data from selected data

I have a column in my table name as URL and it contains Multiple value like "https://www.google.com/#q=how+to+make+a+android+app"
and
http://www.bing.com/search?q=how+to+make+a+android+app&go=&qs=n&form=QBLH&pq=how+to+make+a+android+app&sc=8-15&sp=-1&sk=
I want to get data separately in output like
website = https://www.google.com
Keyword = how to make a android app.
Any Idea Plz, How can i get this in MySql.
you can use substr() function in php to cut string. In your case, you can get the characters up to the end of 'https://www.google.com'.
read more at http://ro1.php.net/substr
in case you want to do it directly in your query, you can use substr() as well. Sql syntax goes like this:
SELECT SUBSTR(column, start_position, desired_length) FROM table_name
*SELECT SUBSTR(column_name, 1, 20) FROM table_name;*

MySQL like reversed

I'm trying to get around pulling all the data from a table, and cycling through it with php. Here's my current Query:
SELECT
*
FROM
ExampleTable
WHERE
StringContains LIKE "%lkjlkjsomeuser#example.comjkjhkjhkjhkjhk,mniu,mk,mkjh%"
ExampleTable.StringContains has values that look like 'someuser#example.com', 'someuser2#example.com', etc.
This doesn't match because LIKE only finds sub strings of the column value, not the other way around. Any ideas on commands to find rows where the table value is a substring of the passed string?
SELECT
*
FROM
ExampleTable
WHERE
'lkjlkjsomeuser#example.comjkjhkjhkjhkjhk,mniu,mk,mkjh' LIKE
CONCAT('%', StringContains, '%')
Try this:
SELECT *
FROM ExampleTable
WHERE "lkjlkjsomeuser#example.comjkjhkjhkjhkjhk,mniu,mk,mkjh" LIKE
CONCAT("%",StringContains,"%")
The key is to recognize that the column variable just represents a string, and the LIKE statement is always comparing two strings in the form
"stringA" LIKE '%stringB%'
Usually people use it to search for a "part" of a string contained in the "whole" database field string, but you can easily switch them. The only extra tool you need is the CONCAT statement, since you want the database field to be the part instead of the whole. The CONCAT statement builds a string with the %'s around the database field string, and the string form of the argument is now equivalent to
"stringB" LIKE "%stringA%"
Just make the LIKE in the opposit order. Since you have to add those % you'll have to concatenate the field first:
SELECT *
FROM ExampleTable
WHERE "lkjlkjsomeuser#example.comjkjhkjhkjhkjhk,mniu,mk,mkjh" LIKE CONCAT('%', StringContains, '%');

Using mysql Regexp in a select statement not the where clause

So what i want to do is use a Regex to select only certain parts of data from a column
Example :
SELECT URL REGEXP 'http://' from websitelist --(website list is a list of URL's)
If I run that against the table it returns 1 foreach row in which 'htt://' was found, what I want is to return the string that matches the regexp
The REGEXP operator performs a match and returns 0 or 1 based on whether the string matched the expression or not. It does not provide a way to extract the matched portion. I don't think that there is any way to extract the matched portion.
You could use just use string functions if it's as simple as your example - just removing http://
SELECT REPLACE(URL, 'http://', '') AS url FROM websitelist;
Probably faster as there is overhead for the REGEX engine.