Mysql - Remove string pattern from string - mysql

I have some string values inside my Mysql database table. They are in this format:
title1|url1
# or
title1|url1\ntitle2|url2\ntitle3|url3
I want to remove all urls and pipe characters (|) from these values so they should be in this format after the operation:
title1
# or
title1\ntitle2\ntitle3
I tried this query so far (say my table is table1 and the field is values):
UPDATE `table1` SET `values` = SUBSTRING_INDEX(`values`, '|', 1);
For values like title1|url1 it works but for values like title1|url1\ntitle2|url2 it returns title1.
How should I do that?
[UPDATE]
url is any url-like string, such as http://www.example.com/ or www.example.org or http://example.com or anything else.

UPDATE `table1` SET `values` = REPLACE (`values`, '|url', '');

Related

SQL - How to get certain characters from a column?

I have a table as:
id: Int,
URL: string,
//other fields
The URL field contains values like ["nytimes.com/live/", "prisma.io/docs/reference/api-reference/", "stackoverflow.com/questions",...]
How do I query to only get the domains of URLs ["nytimes.com", "prisma.io", "stackoverflow.com"...]
Right now I am doing it by fetching all the records and capturing the domain in server. I was wondering if there is a way to do this in SQL directly?
SELECT SUBSTRING_INDEX(URL, '/', 1) FROM table ;
In SQL Server,
CHARINDEX returns the position of a given character.
SUBSTRING returns the substring from the start of the string until the given index
Hence, combining CHARINDEX and SUBSTRING function, we can get the domain name.
Eg, if the domain name is google.com, the following query will return 'google'
SELECT SUBSTRING(URL, 1, CHARINDEX('.',URL)) FROM table_name
The solution for mysql database given by #ricardo-francois works just as intended.
CREATE TABLE domains (id int auto_increment primary key, url varchar(100) not null);
INSERT INTO domains(url) values('nytimes.com/live/'),('google.com/crawl');
SELECT SUBSTRING_INDEX(URL, "/",1) FROM table_name;
The last parameter 1 in the SUBSTRING function indicates the number of occurrences of the delimiter. Here, it will return all the texts until the first delimiter (which is "/")
Output :
nytimes.com
google.com

SQL Update statement truncated incorrect double value error

I'm trying to update my table column values into a string. My query goes like this
UPDATE tbl_testing
SET result= 'Hey'
WHERE (SELECT (colOne) + '-' + (colTwo) + '-' + (colThree)) = 'r-r-r'
which the columns 'colOne, colTwo and colThree' already contains 'r' but slqyog shows "Truncated incorrect DOUBLE value: 'r-r-r'"
and all of the other result column data became = 'Hey'. What should I do?
You have to decide it is either MySQL or MSSQL.
In MySQL the string concatenation is not + sign, but simply you enumerate the columns separated by comma and the statement is SELECT CONCAT("Field1", "Field2" etc) AS ConcatenatedString); - CONCAT() function.
Try reevaluate your DB engine and adapt the query.
In MSSQL the string concatenation is indeed + sign. Your query works ok in MSSQL and updates the result column with the value you have set.
DDL
CREATE TABLE [dbo].[tbl_testing](
[id] [int] NULL,
[result] [nvarchar](4000) NULL,
[colOne] [nvarchar](4000) NULL,
[colTwo] [nvarchar](4000) NULL,
[colThree] [nvarchar](4000) NULL
) ON [PRIMARY]
GO
INSERT INTO tbl_testing (id, colOne, colTwo, colThree)
VALUES (1, 'r', 'r', 'r')
Update statement
UPDATE tbl_testing
SET result='Hey. I am a concatenated string'
WHERE (SELECT (colOne)+'-'+(colTwo)+'-'+(colThree))='r-r-r'
Output
id result colOne colTwo colThree
1 Hey. I am a concatenated string r r r
Changing comment to answer:
You should avoid doing that statements in that way. By doing that, database use no indexes and also you are giving more computation tasks to database server (server needs to concatenate all values and after concatenations will compare with given string).
Better way is to replace yours where statement with something like:
`WHERE colOne = 'r' AND colTwo = 'r' AND ...`
that will work faster without additional computation need (to concatenate strings).
This solution works much much faster, and looks much much better.

MySQL regular expression to select the last integer in the string

I have a MySQL database table that contains column run that is an integer and another column filename that is a varchar with typical values like RUN0001.FTS or 3DS0231.FTS or 3RUN0010.FTS
I need to check the values in the run and filename columns. The prescription is I extract the last integer (without leading zeroes) before the dot character (.) in the filename column, and compare it to the value of the run column. How do I write a select statement to do this comparison to return the rows that will not have the matching integers?
For example, a regular expression I am trying to build would extract 1 from RUN0001.FTS, or 231 from 3DS0231.FTS, or 10 from 3RUN0010.FTS and then compare it to the value in the run column, and return the primary key if the two don't match.
In Python I would manipulate the variable filename = '3RUN0010.FTS' like so:
import re
filename = '3RUN0010.FTS'
fileRunNumber = re.findall('\d+', filename)
runNumber = int(fileRunNumber[-1])
print "The integer I want is", runNumber
How do I do this in as a MySQL statement?
Thanks,
Aina.
You can try the following query:
SELECT CAST(SUBSTRING(col, INSTR(col, '.') - 4, 4) AS UNSIGNED) AS theNumber
FROM yourTable
Data:
CREATE TABLE yourTable (col varchar(55));
INSERT INTO yourTable (col)
VALUES
('RUN0001.FTS'),
('3DS0231.FTS'),
('3RUN0010.FTS');
Output:
Demo here:
Rextester

MySql Functions: Insert substring into string

I need to know what function to use to insert a substring into a string of characters in MySql.
This is an example of ua field in my database:
https://www.facebook.com/ExamplePage/
I need to insert the substring pg/ and it needs to look like this when completed:
https://www.facebook.com/pg/ExamplePage/
Note: the first part of all the strings in my fields are the same.
You can do this with a simple replacement:
update `table`
set `field` = replace(field, 'https://www.facebook.com/', 'https://www.facebook.com/pg/')
where `field` like 'https://www.facebook.com/%';

sql - remove char from all names of objects in database

I am trying to remove name_ part of each name in database, name_ is mistakenly inserted into db and now in 30 object names. if i remove manuelly from db, it takes me much time.
one example is: name_john. it should be john.
how can i delete this name_ from all names of all objects in db with some sql statement?
If it are column values you can do it with an update statement.
UPDATE table_reference
SET column_reference = SUBSTRING(column_reference, 6)
WHERE column_reference LIKE 'name\_%' ESCAPE '\'
If this is about column values that you need to modify, you could use the REPLACE() function like this:
UPDATE tablename
SET columnname = REPLACE(columnname, 'name_', '')
WHERE columnname LIKE '%name\_%' ESCAPE '\'
;
That would remove all entries of name_ in columnname. If there can be no more than one entry (or if only one needs to be removed) and its position is fixed, you could use the INSERT() function instead, which, despite its name, can also replace and delete substrings. This is how you could use it if the position of name_ was e.g. at the beginning:
UPDATE tablename
SET columnname = INSERT(columnname, 1, 5, '')
WHERE columnname LIKE 'name\_%' ESCAPE '\'
;