I have a column in database and having value like this
course_repeatfkfjkjfjkfer_10_topics_0_presentation_link
course_repeatfkfjfkfkfklfflkflkfs_1_presentation_link
course_repeatfkfjfkfkfklfflkflkfs_2_presentation_link
coursek_epeatfkfjfkfkfklfflkflkfs_10_presentation_link
course_hdhdhhdhdjhdrepeatfkfjfkfkfklfflkflkfs_21_presentation_link
and so on.
I need to pick 0,1,2,10,21, number before _presentation_link , But i need this in mysql as well
i used substr in mysql, but that is not working. Any idea?
Thanks
One option would be to use a combination of SUBSTRING_INDEX() and REPLACE():
SELECT SUBSTRING_INDEX(REPLACE(col, '_presentation_link', ''), '_', -1)
FROM yourTable
Taking course_repeatfkfjkjfjkfer_10_topics_0_presentation_link as an example, after the replacement, this would become:
course_repeatfkfjkjfjkfer_10_topics_0
The call to SUBSTRING_INDEX() then grabs everything appearing after the final underscore, which is the number you want to capture.
Demo here:
SQLFiddle
You can use substring_index twice like this:
select substring_index(substring_index(col, '_', -3), '_', 1)
from t
Demo
Related
I have a column in a mysql table like this:
Testtag
SG_QRA_SGBAKE_0012
SG_QRA_SGBAKE_0013
SG_QRA_SGHAST_0005
SG_QRA_SGHAST_0006
SG_QRA_SGHAST_0007
and I want to change the entire column to like this:
Testtag
SGBAKE.0012
SGBAKE.0013
SGHAST.0005
SGHAST.0006
SGHAST.0007
I am stuck here. Any help is appreciated thanks!
We can handle your requirement using SUBSTRING_INDEX along with REPLACE:
UPDATE yourTable
SET Testtag = REPLACE(SUBSTRING_INDEX(Testtag, '_', -2), '_', '.');
If you just want to view your data this way, then use this query:
SELECT Testtag, REPLACE(SUBSTRING_INDEX(Testtag, '_', -2), '_', '.') AS NewTag
FROM yourTable;
Here is a working demo.
Given data like:
URL
some_url.com
some_url.com
some_url.co.uk
some_other_url.com
some_other_url.co.uk
some_other_url.co.uk
some_other_url.org
is there a way to construct a query that will result in;
some_url 3
some_other_url 4
Currently I'm either using a standard group by url or I query the aggregations one by one using LIKE
Is there a way to do this in one query? (using mysql currently, but will be moving this data over to postgresql)
Would it be better practice to add a column to reflect this grouping (at insert time)? (this feels redundant but would be best performing I guess)
EDIT:
data can contain www and non-www as well as http, https. Also I'll have to do similar thing on other columns that contain (free) text values.
This is ANSI SQL compliant and should probably work with both MySQL and Postgresql:
select url, count(*)
from
(
select substring(url from 1 for position('.' in url) -1) as url
from tablename
) dt
group by url
Using position() to find the first . character. Do substring() and finally GROUP BY the result.
use SUBSTRING_INDEX in mysql which help you substring from a string before a specified number of occurrences of the delimiter.
select count(*) as cnt, SUBSTRING_INDEX(c,'.',1) as val from cte
group by SUBSTRING_INDEX(c,'.',1)
Since the values can have http, https and www, and may be query string too, you will have to clean all such values first before grouping it. Took the reference from here and modified it to match your requirement.
SELECT url,
SUBSTRING_INDEX(
SUBSTRING_INDEX(
SUBSTRING_INDEX(
SUBSTRING_INDEX(
SUBSTRING_INDEX(
SUBSTRING_INDEX(url, '/', 3),
'://', -1),
'/', 1),
'?', 1),
'www.', -1),
'.', 1) AS domain,
COUNT(1)
FROM tblname
GROUP BY domain;
This works in Postgesql:
select split_part(url,'.',1) g,count(*)
from url_table
group by g
order by g;
Best regards,
Bjarni
I want to split a string in CRATE. I tried with substr, but it takes only substr(string,long,long). I want something like a function which can take delimiter string.
Example :
value=1234-5656
select SUBSTR(value, '-',1) as first from XYZ;
I want to split the value into 1234 and 5656 in a SQL query. But CRATE does not support SUBSTR(value, '-',1). So I am looking for an option to split the value in the CRATE query.
Any help?
SUBSTRING_INDEX comes in handy here:
SELECT
SUBSTRING_INDEX('1234-5656', '-', 1) AS first,
SUBSTRING_INDEX('1234-5656', '-', -1) AS second
FROM yourTable;
please try using this query:
SELECT SUBSTRING_INDEX(SUBSTRING_INDEX(name, '_', 1), '_', -1) as beforesplit, SUBSTRING_INDEX(SUBSTRING_INDEX(name, '_', 2), '_', -1) as aftersplit FROM testenter
For CrateDB you probably want to use regex_matches function more info on Create's documentation site
However the following should give you what you're looking for
select regexp_matches(yourColumnName, '([0-9])\w+')[1] from yourTable
Hi I need help in sorting mysql query.
I have a column, where it contains the data like
R_5_TP(6),
R_5_TP(7),
R_5_TP(8),
R_6_TP(1),
R_6_TP(10),
R_6_TP(6),
R_50_TP(1),
R_7_TP(1),
I need to write a select query using order by and get the result as
R_5_TP(6),
R_5_TP(7),
R_5_TP(8),
R_6_TP(1),
R_6_TP(6),
R_6_TP(10),
R_7_TP(1),
R_50_TP(1),
Please help.Thank you.
This is a bit tricky. I think you want:
order by substring_index(substring_index(col, '_', 2), '_', -1) + 0, -- convert middle value to a number
substring_index(col, '(', -1) + 0 -- convert parenthesized value to number
I need to remove the last part of a string in a column where I have a field named "path" that looks like:
images/prop/images/2034/22399_2034.JPG
I need everything after the last "/" to be deleted, in order to have
images/prop/images/2034/
instead of
images/prop/images/2034/22399_2034.JPG
I have no idea if this is possible. Thanks.
You can combine MySQL's TRIM() and SUBSTRING_INDEX() functions:
SELECT TRIM(TRAILING SUBSTRING_INDEX(path, '/', -1) FROM path)
FROM my_table
See it on sqlfiddle.