I have column contain a string like that :
"https://www.youtube.com/watch?v=R3-kfnKmDVc"
I want to select this column but get string like that "R3-kfnKmDVc".
How do?
If you want the part of the string after the last =, you can do:
select substring_index(col, '=', -1)
In the event that the string may not contain an '=':
select (case when col like '%=%' then substring_index(col, '=', -1) end)
By getting the id only you are not improving the performance, so you can simply fetch it from DB then use regex functions to get the id of the youtube link.
You can use:
SELECT SUBSTRING('https://www.youtube.com/watch?v=R3-kfnKmDVc', (SELECT LOCATE('?v=', 'https://www.youtube.com/watch?v=R3-kfnKmDVc') + 3));
-> usage in select:
select SUBSTRING(url_name, LOCATE('?v=', url_name) + 3) from table;
Thank You
Related
My value is "asdsdf2739173sidfsd"
Here I want to get first and last occurrence of any number position or index in given string, please help me
To find the indices of the first and last occurrence of a number in a string, we can try using REGEXP_REPLACE:
WITH yourTable AS (
SELECT 'asdsdf2739173sidfsd' AS val
)
SELECT
val,
LENGTH(val) - LENGTH(REGEXP_REPLACE(val, '^[^0-9]*', '')) AS idx_first,
LENGTH(REGEXP_REPLACE(val, '[^0-9]*$', '')) - 1 AS idx_last
FROM yourTable;
Demo
I want to trim JAN2018 and get last value like this =>JAN18. how I can ?
you can use Trim,concat and SUBSTR function
SELECT SUBSTR("Tutorial", 5, 3) AS ExtractString; it will return "ria"
For your case
SELECT concat( SUBSTR(Trim( ' JAN2018'),1,3), SUBSTR(Trim( ' JAN2018'),-2));
For more knowledge on Concat, Trim and SUBSTR
SELECT SUBSTRING("JAN2018", -2) AS endtwochar; //if you have one char then will return empty
SELECT right("JAN2018", 2) AS endtwochar; // if you have on char then you will one char
Both should give same result - decide based on output
if you need output JAN18 then try below
SELECT insert("JAN2018", 4,2, '') AS removetwochar;
Hope this query may help you:
SELECT RIGHT(columnname,2) FROM tablename;
Here is a demo
You can use SUBSTR()
SELECT SUBSTR('JAN2018',-2);
For your new requirement(an ugly way):
SELECT REPLACE('JAN2018','20','')
Give this a try, this is the query to find last two digit from date
select substr(to_char(sysdate),length(to_char(sysdate))-1,2) last_two from dual
I have a database that contains a column "Code" where the records have the following format "xx-xxx" and "xx-xx", for the later format i want to add a zero after the "-" to make it "xx-0xx", is there anyway to count the characters after a certain pattern in Mysql
Hmmm. If those are your only two possibilities, you can use case:
select (case when length(code) = 5
then replace(code, '-', '-0')
else code
end) as new_code
If you want to be more general, deconstruct the string and build it back again:
select concat_ws('-', substring_index(code, '-', 1),
lpad(substring_index(code, '-', -1), 3, '0')
)
Yes, you can use the CHAR_LENGTH(str) like this:
SELECT code,CHAR_LENGTH(SUBSTR(code,3))
from table
I have strings which have a JSON-like format, including:
..."id":"500", ..., "id":"600", ...
I need to parse the second id out of the column. I found lots of answers using substring_index, however, I need to get the string after the 2nd (of potentially n) occurrences and not the string before to parse out the ID.
Is there a nice solution?
To find the substring of a column "some_column" occurring after the nth
occurrence of a target string,
SELECT
SUBSTRING(some_column, CHAR_LENGTH(SUBSTRING_INDEX(some_column, <target_string>, <n>)) + <length of target string + 1>)
FROM some_table
-- or if you want to limit the length of your returned substring...
SELECT
SUBSTRING(some_column, CHAR_LENGTH(SUBSTRING_INDEX(some_column, <target_string>, <n>)) + <length of target string + 1>, <desired length>)
FROM some_table
For this question, the form would be:
SELECT
SUBSTRING(col, CHAR_LENGTH(SUBSTRING_INDEX(col, '"id":"', 2)) + 7)
FROM `table`
For now I have:
SELECT substring_index(
substr(col, locate('"id":"', col, locate('"id":"', col) + 6) + 6),
'"',
1)
FROM table
Would love to see a "nicer" answer :-)
In Snowflake, this can be done as follows:
select
, split_part([field_name], '{separator}', {n-counter})
from
[table]
Note: {separator} and {n-counter} are inputs provided by the user. Snowflake requires apostrophes around {separator}.
For e.g. If I have column entry as
'The only verdict is : Vendetta'
and the same column has another entry as
'I believe in : Harvey Dent'
and all I want to select via my query is 'Vendetta' and 'Harvey Dent' i.e. the string just after the : sign, how do I do it?
Can it be restricted to select upto a specific number of characters after the sign?
If you only have one : in your string, you can give a look at SUBSTRING_INDEX:
SELECT
col,
SUBSTRING_INDEX(col, ':', -1)
FROM
tablename
or you can use SUBSTRING with LOCATE:
SELECT
col,
SUBSTRING(col FROM locate(':', col)+1)
FROM
tablename
WHERE
col LIKE '%:%'
(in your example you probably need to substitute ':' with ' : ' and +1 with +3)