I have a data that have this result:
I'm using the query
select substring_index(descriptn, ' ', -1) from table1
and I get my result along with the dash for example "JACQ-ARMIE"
I want to get only JACQ and ABBY. Can you give me hints on how to get the name? Does this also apply for substring_index?
Just use substring_index() again:
select substring_index(substring_index(descriptn, ' ', -1), '-', 1)
from table1;
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.
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'm migrating some MySQL code to Postgres and having a heck of a time converting the following line:
GROUP_CONCAT(
DISTINCT id,':',foo,':',bar ORDER BY id
) as id,
This results in a comma separated list of strings like:
id:foo:bar,id2:foo2:bar2
The 'DISTINCT' is there to avoid duplicates.
I've read the equivalent of GROUP_CONCAT in Postgres is string_agg, but I can't figure out how to make it work the same way.
Edit: I may have almost answered my own question. I came up with the solution of using CONCAT. The problem with this is that now sorting is done by the concatenated string rather than by the id:
string_agg(DISTINCT CONCAT(
id::text, ':', foo, ':', bar, ':'
), ',') as id
If I try to add 'ORDER BY id' I get an error.
You can do something like below.
select
string_agg(DISTINCT CONCAT(
id::text, ':', foo, ':', bar, ':'
), ',') as id from (select * from table t order by id) as al
I have a table which i am using to query and getting its one column which matches regular expression which is (\/.+\/\?).
Content of the resulted column is like:
/Anything here/?
Example output:
\abc\cdf\?....
\ab\?....
\abc\cdf\?....
\sb\?....
where '....' can be anything
Desired result i want is unique values before \? such that rows with duplicate regexp matched content are shown once only like here (\abc\cdf\?.... showing twice instead of onece)
\abc\cdf\?....
\ab\?....
\sb\?....
OR
\abc\cdf\?
\ab\?
\sb\?
I have looked very much but couldn't find anything there is regexp_substr in oracle but that is not working in SQL.
Please if someone could help me with the sql query that would be awesome.
If you want everything before the last \, then you can use substring_index() and some string manipulation:
select substring_index(col, '\\',
length(col) - length(replace(col, '\\', ''))
) as firstpart,
count(*)
from table t
group by substring_index(col, '\\',
length(col) - length(replace(col, '\\', ''))
);