Take only right word a field column in Teradata - teradata

I want to have only the right word of a string like this in Teradata?
Tim Tom Dave
How to trim Tim and Tom and only get Dave?
substring(ABC, Index(ABC, ' ') -1)
This is always from left side.

instr allows to search backwards:
substr(ABC, instr(ABC, ' ', -1)+1)
-1 = last occurance

You can use regexp_substr:
SELECT REGEXP_SUBSTR('Tim Tom Dave', '[^\s]+$')
The '$' tells us to search backwards from the end of the string.
[^\s] tells us to match any character that isn't a space.

Related

Teradata LIKE for range for values - (regular expression?)

In Teradata I need a condition to select only records:
starting in numbers between 0 and 4
followed by string ABCD
followed by anything
I can use substring and it works. But this is not a nice piece of code.
SELECT
'4ABCDXXX' AS T
, CASE WHEN
Cast (Substring (T, 1,1) AS SMALLINT) BETWEEN 0 AND 4
AND Substring (T, 2,4) = 'ABCD'
THEN 'OK' ELSE 'NOK' END
I tried
LIKE '[0-4]ABCD%'
But this does not seem to be working...
How can this be elegantly achieved?
Thanks.
I don't think that Teradata supports the enhanced LIKE syntax which are you attempting. But, in lieu of this, we can use REGEXP_SIMILAR:
SELECT
'4ABCDXXX' AS T,
CASE WHEN REGEXP_SIMILAR('4ABCDXXX', '^[0-4]ABCD.*$', 'c')
THEN 'OK' ELSE 'NOK' END AS label
FROM yourTable;
I've never been able to make negative lookaheads work in Teradata, so I would use two tests:
select
'4ABCD123' as t,
case when
regexp_similar(t,'^[0-4]ABCD') = 1 -- starts with 0-4 followed by ABCD
and t like '%ABCD' -- does not end with ABCD
then 'nok' else 'ok' end,

Teradata - Removal of multi spaces to single space

I wanted to replace the multi spaces to a single space whenever more than one single space is found in the string. I was using this expression but it is not giving desired results. Is it possible that the data is having some extra non-ascii character than space.
What else am missing
I've tried below solution to update the column with below
TRIM( REGEXP_REPLACE ( 'Report   the value of total personnel expense.', '( )+',' ',1 ,0, 'c' ) ) ;
its not giving the desired result.
Actual result:Report   the value of total personnel expense.
Desired Result: Report the value of total personnel expense.
[ ]{2,} will catch 2 or more space characters, then replace with a single space.
SELECT RegExp_Replace('Report the value of total personnel expense','[ ]{2,}',' ');
Output: Report the value of total personnel expense
You might try to replace consecutive whitespace with a single blank:
REGEXP_REPLACE (x, '\s+',' ')
--'\s' matches blank, new line, tab, etc.
Or remove additional whitespace:
REGEXP_REPLACE (x, '\s\K\s+','') ) ;
-- '\K' drops the previously matched characters from the match
I prefer #1 because all whitespace is replaced by a blank, while #2 will keep tab/new line if it's the 1st char in a match

REGEXP_SUBSTR return all matches (mariaDB)

I need to get all the matches of a regular expression in a text field in a MariaDB table. As far as I know REGEXP_SUBSTR is the way to go to get the value of the match of a regular expression in a text field, but it always returns after the first match and I would like to get all matches.
Is there any way to do this in MariaDB?
An example of the content of the text field would be:
#Generation {
// 1
True =>
`CP?:24658` <= `CPV?:24658=57186`;
//`CP?23432:24658` <= `CPV?:24658=57186`
// 2
`CP?:24658` <> `CPV?:24658=57178` =>
`CP?:24656` <> `CPV?:24656=57169`;
And the select expression that I'm using right now is:
select REGEXP_SUBSTR(textfield,'CP\\?(?:\\d*:)*24658') as my_match
from table
where id = 1243;
Which at the moment returns just the first match:
CP?:24658
And I would like it to return all matches:
CP?:24658
CP?23432:24658
CP?:24658
Use just REGEXP to find the interesting rows. Put those into a temp table
Repeatedly process the temp table -- but remove the SUBSTR as you use it.
What will you be doing with each substr? Maybe that will help us devise a better approach.

how to search for a particular string in the given string in oracle

I have a string with value as '12A,12B,12C,13,14'.
I want to check whether '2A' is available in the above string.
while trying my value '2A' checks in 12A and returns as matched.
Please give me a solution for this.
You can do something like this:
select * from table where ',' || col || ',' like '%,2A,%';
Commas are concatenated to the column to cover the cases where the element is present at the start or end of the string.

How to concat two column with '-' seperator in PL/SQL

I just want to concat two columns with seperator '-'.
These are the two columns, want to concat.
I am using this query to concat them
select concat(amt,endamt)as amount from mstcatrule
and it is giving me result this
But I Want that data of 2 columns should be sepearted by '-'
RESULT I WANT IS :
AMOUNT
0-0
100-99999999999
100-500
Alternative:
select amt || '-' || endamt as amount from mstcatrule;
Do it with two concats:
select concat(concat(amt, '-'), endamt) as amount from mstcatrule;
concat(amt,'-') concatenates the amt with the dash and the resulting string is concatenated with endamt.
Another way is to use double pipe.
select amt || '-' || endamt as amount from mstcatrule;
You may have to convert amt and endamt to varchar
In oracle this works for me! :D
select amt||'-'||endamt as amount from mstcatrule
Alternative you can use under query
select concat(amt,'-',endamt) as amount from mstcatrule;
A generic format for the query
Select concat(column1,'-',column2) as concatedCols from table_Name
For Postgresql only

Resources